Python 从Pytorch到Tensorflow的LSTM模型转换

Python 从Pytorch到Tensorflow的LSTM模型转换,python,tensorflow,keras,pytorch,lstm,Python,Tensorflow,Keras,Pytorch,Lstm,我在Pytorch中有一个现有模型,我正在尝试将其转换为TensorFlow。这是一个具有两个隐藏LSTM层和多输出的神经网络,因为我的目标是进行多类分类。我的输入是一个时间序列,其中每个值代表一个给定的类,我想预测下一个类。我的时间序列已经通过使用一个大小为10的窗口转换为一个向量。 困扰我的是,在pytorch中,我的模型给出了令人敬畏的预测,但在tensorflow中,我的精度下降,损失函数卡住了 我很确定我输入到模型中的数据是它所需要的形状,所以我现在只想发布模型本身 Pytorch代码

我在Pytorch中有一个现有模型,我正在尝试将其转换为TensorFlow。这是一个具有两个隐藏LSTM层和多输出的神经网络,因为我的目标是进行多类分类。我的输入是一个时间序列,其中每个值代表一个给定的类,我想预测下一个类。我的时间序列已经通过使用一个大小为10的窗口转换为一个向量。 困扰我的是,在pytorch中,我的模型给出了令人敬畏的预测,但在tensorflow中,我的精度下降,损失函数卡住了

我很确定我输入到模型中的数据是它所需要的形状,所以我现在只想发布模型本身

Pytorch代码

class Model(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, num_keys):
        super(Model, self).__init__()
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
        self.fc = nn.Linear(hidden_size, num_keys)

    def forward(self, input):
        h0 = torch.zeros(self.num_layers, input.size(0), self.hidden_size).to(device)
        c0 = torch.zeros(self.num_layers, input.size(0), self.hidden_size).to(device)
        out, _ = self.lstm(input, (h0, c0))
        out = self.fc(out[:, -1, :])
        return out
modeltf = keras.models.Sequential([
  keras.layers.LSTM(hidden_size, return_sequences=True,
                         batch_input_shape=(batch_size, 10, 1)),
  keras.layers.LSTM(hidden_size, return_sequences=False),
  keras.layers.Dense(num_classes),
])

modeltf.compile(optimizer=keras.optimizers.Adam(),
              loss='sparse_categorical_crossentropy',
              metrics=['sparse_categorical_accuracy'])
TF代码

class Model(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, num_keys):
        super(Model, self).__init__()
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
        self.fc = nn.Linear(hidden_size, num_keys)

    def forward(self, input):
        h0 = torch.zeros(self.num_layers, input.size(0), self.hidden_size).to(device)
        c0 = torch.zeros(self.num_layers, input.size(0), self.hidden_size).to(device)
        out, _ = self.lstm(input, (h0, c0))
        out = self.fc(out[:, -1, :])
        return out
modeltf = keras.models.Sequential([
  keras.layers.LSTM(hidden_size, return_sequences=True,
                         batch_input_shape=(batch_size, 10, 1)),
  keras.layers.LSTM(hidden_size, return_sequences=False),
  keras.layers.Dense(num_classes),
])

modeltf.compile(optimizer=keras.optimizers.Adam(),
              loss='sparse_categorical_crossentropy',
              metrics=['sparse_categorical_accuracy'])
如果有人能看到差异或有任何建议,我将不胜感激:)