Computes square of x element-wise

I want to use similar kind of square function in tensorflow

In tensorflow tf.square is given. But I cannot find in pytorch. Thanks in advanced.
tf.square

square(
x,
name=None
)

1 Like

Maybe torch.mul(x,x) works for you.

1 Like

Thanks. I need to compute square of x element-wise.

i.e., y=x*x=x^2

Yes, torch.mul() is the element-wise multiplication

x**2 should work as well.

a = torch.randn(2, 2)
print a

>> 0.4616  0.2764
>> -0.4579  0.9157
>> [torch.FloatTensor of size 2x2]

print a**2

>> 0.2131  0.0764
>> 0.2096  0.8384
>> [torch.FloatTensor of size 2x2]

Is this what you are looking for?

As @Xinge mentioned, torch.mul(a, a) gives the same result.

7 Likes

Thank you. I got it.

several ways to do this:

torch.pow(x, 2)
x**2
x*x
torch.mul(x, x)