首页 > 解决方案 > 将 TensorFlow 模型转换为 Pytorch 时出现大小不匹配错误

问题描述

我正在尝试将 TensorFlow 模型转换为 Pytorch,但陷入了这个错误。谁能帮我?

#getting weights and biases from tensorflow model
weights, biases = model.layers[0].get_weights()
#[1] is the dropout layer
weights2, biases2 = model.layers[2].get_weights()

#initializing pytorch
class TwoLayerNet(torch.nn.Module):
def __init__(self, weights, biases, weights2, biases2):

    super(TwoLayerNet, self).__init__()
    #created the model in the same dimensions as tensorflow´s model
    self.linear1 = torch.nn.Linear(9, 2048)
    self.hidden1 = nn.Dropout(0.2)
    self.linear2 = torch.nn.Linear(2048,5)

    weights = torch.from_numpy(weights)
    biases = torch.from_numpy(biases)
    weights2 = torch.from_numpy(weights2)
    biases2 = torch.from_numpy(biases2)

    self.linear1.weight = torch.nn.Parameter(weights)
    self.linear1.bias = torch.nn.Parameter(biases)
    self.linear2.weight.data = weights2
    self.linear2.bias.data = biases2
    #in this print the dimensions are ok (Linear(in_features=9, out_features=2048, bias=True))
    print(self.linear1)

def forward(self, x):
  print(self.linear1)
  x = self.linear1(x)
  x = self.hidden1(x)
  x = self.linear2(x)
  return x

model_pytorch = TwoLayerNet(weights, biases, weights2, biases2)

model_pytorch.eval()
exemplo_input_torch = torch.from_numpy(exemplo_input)
exemplo_input_torch = exemplo_input_torch.float()
print(exemplo_input_torch)
result = model_pytorch(exemplo_input_torch)

错误是:

RuntimeError:大小不匹配,m1:[1 x 9],m2:[2048 x 9] 在 /pytorch/aten/src/TH/generic/THTensorMath.cpp:41

标签: pythonpytorch

解决方案


您需要转置权重和偏差:

weights = torch.from_numpy(weights).T
biases = torch.from_numpy(biases).T
weights2 = torch.from_numpy(weights2).T
biases2 = torch.from_numpy(biases2).T

推荐阅读