Wasi Ahmad
Wasi Ahmad

Reputation: 37691

AttributeError: module 'torch' has no attribute 'cmul'

I was trying to do element-wise multiplication of two tensors using the example provided here.

My code:

import torch

x = torch.Tensor([2, 3])
y = torch.Tensor([2, 1])
z = torch.cmul(x, y)
print(z)

It is giving me the following error.

AttributeError: module 'torch' has no attribute 'cmul'

Can anyone tell me why I am getting this error?

Upvotes: 1

Views: 2983

Answers (3)

Wasi Ahmad
Wasi Ahmad

Reputation: 37691

I got the solution. Instead of using cmul, I need to use mul. The following code worked for me!

import torch

x = torch.Tensor([2, 3])
y = torch.Tensor([2, 1])
z = torch.mul(x, y)
print(z)

PS: I was using pytorch, not lua.

Upvotes: 1

User9123
User9123

Reputation: 606

try:

z = x.cmul(y)

I think cmul is a method of the class Tensor, not a function...

PS: The exemple in the documentation you gave is written in lua, not python.

Upvotes: 0

Denis
Denis

Reputation: 1543

Because Torch doesnt have this method. Cmul is a class on its own which is located at torch.legacy.nn which takes Torch as arguments https://github.com/pytorch/pytorch/blob/master/torch/legacy/nn/CMul.py

Upvotes: 0

Related Questions