Convert Keras model to pytorch

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)

I would recommend to start with this tutorial and try to replace the Keras layers with their PyTorch equivalents:

  • Densenn.Linear
  • Actiation('sigmoid')nn.Sigmoid (or torch.sigmoid in the forward method)
  • Dropoutnn.Dropout
  • Multiplyx = 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