DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

mul in PyTorch

Buy Me a Coffee

*Memos:

mul() can do multiplication with two of the 0D or more D tensors of zero or more elements or scalars or the 0D or more D tensor of zero or more elements and a scalar. getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • mul() can be used with torch or a tensor.
  • The 1st argument(input) with torch(Type:tensor or scalar of int, float, complex or bool) or using a tensor(Type:tensor of int, float, complex or bool)(Required).
  • The 2nd argument with torch or the 1st argument with a tensor is other(Required-Type:tensor or scalar 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.
  • multiply() is the alias of mul().
import torch

tensor1 = torch.tensor([9, 7, 6])
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.mul(input=tensor1, other=tensor2)
tensor1.mul(other=tensor2)
# tensor([[36, -28, 18], [-18, 35, -30]])

torch.mul(input=9, other=tensor2)
# tensor([[36, -36, 27], [-18, 45, -45]])

torch.mul(input=tensor1, other=4)
# tensor([36, 28, 24])

torch.mul(input=9, other=4)
# tensor(36)

tensor1 = torch.tensor([9., 7., 6.])
tensor2 = torch.tensor([[4., -4., 3.], [-2., 5., -5.]])

torch.mul(input=tensor1, other=tensor2)
# tensor([[36., -28., 18.], [-18., 35., -30.]])

torch.mul(input=9., other=tensor2)
# tensor([[36., -36., 27.], [-18., 45., -45.]])

torch.mul(input=tensor1, other=4.)
# tensor([36., 28., 24.])

torch.mul(input=9., other=4.)
# tensor(36.)

tensor1 = torch.tensor([9.+0.j, 7.+0.j, 6.+0.j])
tensor2 = torch.tensor([[4.+0.j, -4.+0.j, 3.+0.j],
                        [-2.+0.j, 5.+0.j, -5.+0.j]])
torch.mul(input=tensor1, other=tensor2)
# tensor([[36.+0.j, -28.+0.j, 18.+0.j],
#         [-18.+0.j, 35.+0.j, -30.+0.j]])

torch.mul(input=9.+0.j, other=tensor2)
# tensor([[36.+0.j, -36.+0.j, 27.+0.j],
#         [-18.+0.j, 45.+0.j, -45.+0.j]])

torch.mul(input=tensor1, other=4.+0.j)
# tensor([36.+0.j, 28.+0.j, 24.+0.j])

torch.mul(input=9.+0.j, other=4.+0.j)
# tensor(36.+0.j)

tensor1 = torch.tensor([True, False, True])
tensor2 = torch.tensor([[False, True, False], [True, False, True]])

torch.mul(input=tensor1, other=tensor2)
# tensor([[False, False, False],
#         [True, False, True]])

torch.mul(input=True, other=tensor2)
# tensor([[False, True, False], [True, False, True]])

torch.mul(input=tensor1, other=False)
# tensor([False, False, False])

torch.mul(input=True, other=False)
# tensor(False)
Enter fullscreen mode Exit fullscreen mode

Billboard image

Monitoring as code

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay