torch.Tensor行向量转为列向量(unsqueeze)

网友投稿 729 2022-11-17

torch.Tensor行向量转为列向量(unsqueeze)

torch.Tensor行向量转为列向量(unsqueeze)

文章目录

​​一、问题描述​​​​二、解决方案​​

一、问题描述

Traceback (most recent call last): File "beat_deepFM_train.py", line 176, in 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 torchx1 = torch.Tensor([1, 2, 3, 4, 5])x2 = torch.unsqueeze(x1, dim=1)print(x1, "\n")# 打印x1,x2的sizeprint(x1.size()) # torch.Size([5])print(x2.size()) # torch.Size([5, 1])print(x2, "\n")

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:kubernetes之多容器pod以及通信
下一篇:容器快速入门完全指南
相关文章

 发表评论

暂时没有评论,来抢沙发吧~