mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-13 19:08:06 +00:00
feat(Core): added the first classification model & the feature necessary (#4)
* feat(Core): added the first classification model & the feature necessary * feat(Models): added basic transformers model
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
from keras import layers
|
||||
import keras
|
||||
|
||||
def transformer_encoder(inputs, head_size, num_heads, ff_dim, dropout=0):
|
||||
# Normalization and Attention
|
||||
x = layers.LayerNormalization(epsilon=1e-6)(inputs)
|
||||
x = layers.MultiHeadAttention(
|
||||
key_dim=head_size, num_heads=num_heads, dropout=dropout
|
||||
)(x, x)
|
||||
x = layers.Dropout(dropout)(x)
|
||||
res = x + inputs
|
||||
|
||||
# Feed Forward Part
|
||||
x = layers.LayerNormalization(epsilon=1e-6)(res)
|
||||
x = layers.Conv1D(filters=ff_dim, kernel_size=1, activation="relu")(x)
|
||||
x = layers.Dropout(dropout)(x)
|
||||
x = layers.Conv1D(filters=inputs.shape[-1], kernel_size=1)(x)
|
||||
return x + res
|
||||
|
||||
def create_basic_transformer_model(
|
||||
input_shape,
|
||||
n_classes,
|
||||
head_size,
|
||||
num_heads,
|
||||
ff_dim,
|
||||
num_transformer_blocks,
|
||||
mlp_units,
|
||||
dropout=0,
|
||||
mlp_dropout=0,
|
||||
):
|
||||
inputs = keras.Input(shape=input_shape)
|
||||
x = inputs
|
||||
for _ in range(num_transformer_blocks):
|
||||
x = transformer_encoder(x, head_size, num_heads, ff_dim, dropout)
|
||||
|
||||
x = layers.GlobalAveragePooling1D(data_format="channels_first")(x)
|
||||
for dim in mlp_units:
|
||||
x = layers.Dense(dim, activation="relu")(x)
|
||||
x = layers.Dropout(mlp_dropout)(x)
|
||||
outputs = layers.Dense(n_classes, activation="softmax")(x)
|
||||
return keras.Model(inputs, outputs)
|
||||
Reference in New Issue
Block a user