-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
31 lines (25 loc) · 1.13 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import tensorflow
import tensorflow.python.keras as keras
from keras.layers import Input, Dense, Dropout, LayerNormalization, MultiHeadAttention, Add, GlobalAveragePooling1D
from keras.models import Model
from keras.callbacks import ModelCheckpoint
def transformer_encoder(inputs, head_size, num_heads, ff_dim, dropout=0):
# Attention and Normalization
x = LayerNormalization(epsilon=1e-6)(inputs)
x = MultiHeadAttention(key_dim=head_size, num_heads=num_heads, dropout=dropout)(x, x)
x = Add()([x, inputs])
# Feed Forward Part
y = LayerNormalization(epsilon=1e-6)(x)
y = Dense(ff_dim, activation="relu")(y)
y = Dropout(dropout)(y)
y = Dense(inputs.shape[-1])(y)
return Add()([y, x])
def build_transformer_model(input_shape, head_size, num_heads, ff_dim, num_layers, dropout=0):
inputs = Input(shape=input_shape)
x = inputs
for _ in range(num_layers):
x = transformer_encoder(x, head_size, num_heads, ff_dim, dropout)
x = GlobalAveragePooling1D()(x)
x = LayerNormalization(epsilon=1e-6)(x)
outputs = Dense(1, activation="linear")(x)
return Model(inputs=inputs, outputs=outputs)