Aash
(Ayesha)
1
Hello, Can anyone help me to convert this Keras model to pytorch.
model_in =Input(shape=(None,1024))
x = Dense(512, kernel_initializer='glorot_uniform', bias_initializer='zeros')(model_in)
x = Activation('sigmoid')(x)
x = Dropout(0.8)(x)
x = Dense(1, kernel_initializer='glorot_uniform', bias_initializer='zeros')(x)
att_weights = Activation('sigmoid')(x)
att_mull = Multiply()([model_in,att_weights])
mean = Lambda(lambda x:K.mean(x,axis=1))(att_mull)
ptrblck
2
I would recommend to start with this tutorial and try to replace the Keras layers with their PyTorch equivalents:
-
Dense
→ nn.Linear
-
Actiation('sigmoid')
→ nn.Sigmoid
(or torch.sigmoid
in the forward
method)
-
Dropout
→ nn.Dropout
-
Multiply
→ x = x * att_weights
in the forward
method (or a custom nn.Module
, which doesn’t seem to be necessary)
-
Lambda
→ I guess you could just use x = x.mean(dim=1)
in the forward
method