hi every body. please help me.
in just want to transfer a code from keras to pytorch
first q: is this two models are same? if not what is exact equivalence code in pytorch:)
so:
is this code…?
question = layers.Input(shape=(None,), dtype='int32', name='question')
answer = layers.Input(shape=(None,), dtype='int32', name='answer')
embedding = layers.Embedding(
mask_zero=True,
input_dim=self.vocab_size,
output_dim=self.embedding_size,
weights=[self.w2v_weights],
trainable=True
)
lstm_1 = layers.LSTM(units=80, return_sequences=True)
lstm_2 = layers.LSTM(units=80, return_sequences=False)
question_model = lstm_2(lstm_1(embedding(question)))
answer_model = lstm_2(lstm_1(embedding(answer)))
sim = layers.dot([question_model, answer_model], axes=1, normalize=True)
# sim = layers.Activation(activation='sigmoid')(sim)
qa_similary_model = models.Model(
inputs=[question, answer],
outputs=[sim],
)
qa_similary_model.compile(
loss='mean_squared_error', optimizer='nadam', metrics=['accuracy'])
embedding_model = models.Model(
inputs=[question],
outputs=[question_model]
)
return qa_similary_model, embedding_model
is equivalent to this?
class LSTM_Model(Model):
"""
a class to define multiple models
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
def forward(self, question, answer):
question_embedding = self.embeddings(question)
# print("question embedding shape:", question_embedding.shape)
answer_embedding = self.embeddings(answer)
# print("answer embedding shape:", answer_embedding.shape)
q_output, (qhidden, qcell) = self.lstm(question_embedding)
# print("q_output shape:", q_output.shape)
# print("qhidden shape:", qhidden.shape)
# print("qcell shape:", qcell.shape)
a_output, (ahidden, acell) = self.lstm(answer_embedding)
# print("a_output shape:", a_output.shape)
# print("ahidden shape:", ahidden.shape)
# print("acell shape:", acell.shape)
# qa_similary = torch.mm(qhidden[-1], ahidden[-1])
# qa_similary =torch.matmul((qhidden[-1]), torc.th(ahidden[-1]))
qa_similary = torch.matmul((q_output[-1]), torch.t(a_output[-1]), )
# print("qa_similary shape:", qa_similary.shape)
return qa_similary, qhidden
i need help