我试图使用此处提供的示例对两个张量进行逐元素乘法。
我的代码:
import torch
x = torch.Tensor([2, 3])
y = torch.Tensor([2, 1])
z = torch.cmul(x, y)
print(z)
它给了我以下错误。
AttributeError: module 'torch' has no attribute 'cmul'
谁能告诉我为什么我会收到这个错误?
我得到了解决方案。而不是使用cmul
,我需要使用mul
。以下代码对我有用!
import torch
x = torch.Tensor([2, 3])
y = torch.Tensor([2, 1])
z = torch.mul(x, y)
print(z)
PS:我使用的是pytorch,而不是lua。
尝试:
z = x.cmul(y)
我认为cmul
是类的方法Tensor
,而不是函数......
PS:您提供的文档中的示例是用lua编写的,而不是python。
因为 Torch 没有这个方法。Cmul 是一个独立的类,它位于torch.legacy.nn
将 Torch 作为参数
https://github.com/pytorch/pytorch/blob/master/torch/legacy/nn/CMul.py