我有一些keras代码需要转换为Pytorch。我做了一些研究,但到目前为止,我无法重现我从keras得到的结果。我花了很多时间在这方面,任何提示或帮助都非常感谢。
这是我正在处理的keras代码。输入形状是(None105768(,其中None是批大小,我想将Conv1D应用于输入。以keras为单位的期望输出为(无,105(
x = tf.keras.layers.Dropout(0.2)(input)
x = tf.keras.layers.Conv1D(1,1)(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Activation('softmax')(x)
我尝试过,但结果更糟:
self.conv1d = nn.Conv1d(768, 1, 1)
self.dropout = nn.Dropout(0.2)
self.softmax = nn.Softmax()
def forward(self, input):
x = self.dropout(input)
x = x.view(x.shape[0],x.shape[2],x.shape[1])
x = self.conv1d(x)
x = torch.squeeze(x, 1)
x = self.softmax(x)
罪魁祸首是您试图交换输入的维度,因为Keras和PyTorch对维度顺序有不同的约定。
x = x.view(x.shape[0],x.shape[2],x.shape[1])
.view()
不交换维度,而是更改数据的哪一部分是给定维度的一部分。您可以将其视为一个1D阵列,然后决定要采取多少步骤来覆盖该维度。一个例子使它更容易理解。
# Let's start with a 1D tensor
# That's how the underlying data looks in memory.
x = torch.arange(6)
# => tensor([0, 1, 2, 3, 4, 5])
# How the tensor looks when using Keras' convention (expected input)
keras_version = x.view(2, 3)
# => tensor([[0, 1, 2],
# [3, 4, 5]])
# Vertical isn't swapped with horizontal, but the data is arranged differently
# The numbers are still incrementing from left to right
incorrect_pytorch_version = keras_version.view(3, 2)
# => tensor([[0, 1],
# [2, 3],
# [4, 5]])
若要交换尺寸,需要使用torch.transpose
。
correct_pytorch_version = keras_version.transpose(0, 1)
# => tensor([[0, 3],
# [1, 4],
# [2, 5]])