songyouwei / ABSA-PyTorch

Aspect Based Sentiment Analysis, PyTorch Implementations. 基于方面的情感分析,使用PyTorch实现。
MIT License
2k stars 524 forks source link

求助,复现TNet模型时遇到了这个报错。 #140

Open Gaoshuang77 opened 4 years ago

Gaoshuang77 commented 4 years ago

您好,请问如何解决呢?谢谢。 epoch: 0 Traceback (most recent call last): File "train.py", line 294, in main() File "train.py", line 290, in main ins.run() File "train.py", line 170, in run best_model_path = self._train(criterion, optimizer, train_data_loader, val_data_loader) File "train.py", line 105, in _train outputs = self.model(inputs) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 550, in call result = self.forward(*input, **kwargs) File "/content/gdrive/Shared drives/ABSA-PyTorch-master/models/tnet_lf.py", line 67, in forward a = torch.bmm(e.transpose(1, 2), v) RuntimeError: expected scalar type Float but found Double

Gaoshuang77 commented 4 years ago

我尝试在报错这行“a = torch.bmm(e.transpose(1, 2), v)”之后加入“v = torch.tensor(v, dtype=torch.float32)”将v转换成float向量,但是遇到了另一个报错: Traceback (most recent call last): File "train.py", line 294, in main() File "train.py", line 290, in main ins.run() File "train.py", line 170, in run best_model_path = self._train(criterion, optimizer, train_data_loader, val_data_loader) File "train.py", line 105, in _train outputs = self.model(inputs) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 550, in call result = self.forward(*input, kwargs) File "/content/gdrive/Shared drives/ABSA-PyTorch-master/models/tnet_lf.py", line 76, in forward z = F.relu(self.convs3(v)) # [(N,Co,L), ...]len(Ks) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 550, in call result = self.forward(input, kwargs) File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py", line 208, in forward self.padding, self.dilation, self.groups) RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #3 'mat1' in call to _thaddmm 请问到底应该从哪里改呢?

Gaoshuang77 commented 4 years ago

@songyouwei 目前还是没有解决,请问您知道如何修改吗?

songyouwei commented 4 years ago

可能和版本有关,有没有安装requirements.txt呢?

Gaoshuang77 commented 4 years ago

嗯嗯,安装了requirements.txt,除了用了较小的glove以外,别的地方没有改动。

gpf951101 commented 4 years ago

v = torch.tensor(v, dtype=torch.float32).to(self.opt.device) 我再for循环的第一行添加了上面这行代码

Gaoshuang77 commented 4 years ago

@gpf951101 您加了这行是可以运行的吗?我刚刚去试了一下,但是报错“RuntimeError: Expected object of scalar type Double but got scalar type Float for argument #3 'mat1' in call to _thaddmm

GeneZC commented 4 years ago

@Gaoshuang77 你是在cpu上跑的吧

Gaoshuang77 commented 4 years ago

@GeneZC 是的,在CPU上跑的。

GeneZC commented 4 years ago

@Gaoshuang77 那就检查一下你在哪里引入了double(float64)类型的变量,按理说这份代码都应该是float(float32)类型

dongyang12138 commented 4 years ago

请问您解决了您的问题吗,我也遇到了同样的问题

GeneZC commented 4 years ago

@dongyang12138 @Gaoshuang77 try removing line 27 in tfnet_lf.py: pos_inx = pos_inx.cpu().numpy()

GeneZC commented 4 years ago

Or downgrade Pytorch version <= 1.4.0

GeneZC commented 4 years ago

And the best solution may be change weight = torch.tensor(weight) to weight = torch.tensor(weight).float()

Ottehack commented 4 years ago

我在别的模型上也出现了Float和Double类型的问题,后来我去降级了pytorch,从1.5.1降到1.4就没问题了