一、问题描述
Traceback (most recent call last):
File "beat_deepFM_train.py", line 176, in <module>
train(ep)
File "beat_deepFM_train.py", line 40, in train
out = model(xi, xv)
File "/home/andy/.conda/envs/work2/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
return forward_call(*input, **kwargs)
File "/home/andy/deepFM_CTR_beat/model_train/model/sing_deepFM_model.py", line 82, in forward
fm_1st_dense_res = self.fm_1st_order_dense(xi)
File "/home/andy/.conda/envs/fun/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1110, in _call_impl
return forward_call(*input, **kwargs)
File "/home/andy/.conda/envs/fun/lib/python3.8/site-packages/torch/nn/modules/linear.py", line 103, in forward
return F.linear(input, self.weight, self.bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x16 and 1x1)
二、解决方案
报错是矩阵相乘的维度出错问题,在线性层linear的输入时,input的shape的应该是[[16]],而不是[16],通过xi = torch.unsqueeze(xi, dim=1)
将行向量转为列向量,举例:
import torch
x1 = torch.Tensor([1, 2, 3, 4, 5])
x2 = torch.unsqueeze(x1, dim=1)
print(x1, "\n")
# 打印x1,x2的size
print(x1.size()) # torch.Size([5])
print(x2.size()) # torch.Size([5, 1])
print(x2, "\n")