Home > Backend Development > Python Tutorial > square in PyTorch

square in PyTorch

Barbara Streisand
Release: 2025-01-03 03:58:38
Original
414 people have browsed it

square in PyTorch

Buy Me a Coffee☕

*Memos:

  • My post explains pow().
  • My post explains float_power().
  • My post explains abs() and sqrt().
  • My post explains gcd() and lcm().
  • My post explains trace(), reciprocal() and rsqrt().

square() can get the 0D or more D tensor of squared zero or more elements, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • square() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int, float, complex or bool).
  • There is out argument with torch(Optional-Default:None-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
import torch

my_tensor = torch.tensor(-3)

torch.square(input=my_tensor)
my_tensor.square()
# tensor(9)

my_tensor = torch.tensor([-3, 1, -2, 3, 5, -5, 0, -4])

torch.square(input=my_tensor)
# tensor([9, 1, 4, 9, 25, 25, 0, 16])

my_tensor = torch.tensor([[-3, 1, -2, 3],
                          [5, -5, 0, -4]])
torch.square(input=my_tensor)
# tensor([[9, 1, 4, 9],
#         [25, 25, 0, 16]])

my_tensor = torch.tensor([[[-3, 1], [-2, 3]],
                          [[5, -5], [0, -4]]])
torch.square(input=my_tensor)
# tensor([[[9, 1], [4, 9]],
#         [[25, 25], [0, 16]]])

my_tensor = torch.tensor([[[-3., 1.], [-2., 3.]],
                          [[5., -5.], [0., -4.]]])
torch.square(input=my_tensor)
# tensor([[[9., 1.], [4., 9.]],
#         [[25., 25.], [0., 16.]]])

my_tensor = torch.tensor([[[-3.+0.j, 1.+0.j], [-2.+0.j, 3.+0.j]],
                          [[5.+0.j, -5.+0.j], [0.+0.j, -4.+0.j]]])
torch.square(input=my_tensor)
# tensor([[[9.-0.j, 1.+0.j], [4.-0.j, 9.+0.j]],
#         [[25.+0.j, 25.-0.j], [0.+0.j, 16.-0.j]]])

my_tensor = torch.tensor([[[True, False], [True, False]],
                          [[False, True], [False, True]]])
torch.square(input=my_tensor)
# tensor([[[1, 0], [1, 0]],
#         [[0, 1], [0, 1]]])
Copy after login

The above is the detailed content of square in PyTorch. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template