我正试图将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)
错误是:
运行时错误:大小不匹配,m1:[1 x 9],m2:[2048 x 9]at/pytorch/aten/src/TH/generic/THTensorMath.cpp:41
您需要转换权重和偏差:
相关问题 更多 >
编程相关推荐