python - 将 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
解决方案
您需要转置权重和偏差:
weights = torch.from_numpy(weights).T
biases = torch.from_numpy(biases).T
weights2 = torch.from_numpy(weights2).T
biases2 = torch.from_numpy(biases2).T
推荐阅读
- sql - db2 sqlcode -20186
- javascript - 如何访问 Angular Material 表的特定行中的模板变量引用?(角度 7)
- php - 我怎样才能做下一个或预览按钮
- swift - 从不调用函数的完成处理程序有什么影响?
- c# - Service Fabric:如何使用 ServiceFabricIntegrationOptions.UseUniqueServiceUrl 调用服务?
- python - Selenium:硒结果和浏览器之间的url内容不同
- javascript - 如何使用 vue.js 和 vue 路由器实现当前路由的子菜单的子菜单
- vba - 未送达时打印原始电子邮件
- linux - 通过 JNI 从其他共享库加载依赖的 .so
- android - Google Play 管理中心阻止上传或推广符合敏感权限政策的新应用版本