Skip to content

Instantly share code, notes, and snippets.

@JanSchm
Created June 25, 2020 13:30
Show Gist options
  • Save JanSchm/a02f3cee6a2805833cd706c04edaa37b to your computer and use it in GitHub Desktop.
Save JanSchm/a02f3cee6a2805833cd706c04edaa37b to your computer and use it in GitHub Desktop.
def create_model():
'''Initialize time and transformer layers'''
time_embedding = Time2Vector(seq_len)
attn_layer1 = TransformerEncoder(d_k, d_v, n_heads, ff_dim)
attn_layer2 = TransformerEncoder(d_k, d_v, n_heads, ff_dim)
attn_layer3 = TransformerEncoder(d_k, d_v, n_heads, ff_dim)
'''Construct model'''
in_seq = Input(shape=(seq_len, 5))
x = time_embedding(in_seq)
x = Concatenate(axis=-1)([in_seq, x])
x = attn_layer1((x, x, x))
x = attn_layer2((x, x, x))
x = attn_layer3((x, x, x))
x = GlobalAveragePooling1D(data_format='channels_first')(x)
x = Dropout(0.1)(x)
x = Dense(64, activation='relu')(x)
x = Dropout(0.1)(x)
out = Dense(1, activation='linear')(x)
model = Model(inputs=in_seq, outputs=out)
model.compile(loss='mse', optimizer='adam', metrics=['mae', 'mape'])
return model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment