DEV Community

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

Posted on • Edited on

Set `out` argument in PyTorch

Buy Me a Coffee

*Memos:

You can set out as shown below:

*Memos:

  • I selected some popular out argument functions such as arange(), rand() add(), mean(), median(), min(), max(), all(), any() and matmul().
  • Basically, out(Optional-Default-None-Type:tensor) can have a returned tensor. *Sometimes, out(Optional-Default-None-Type:tuple(tensor, tensor) or list(tensor, tensor)).
  • Basically, out can be used with torch but not with a tensor.
  • Basically, out= must be used.
  • Sometimes, out needs to be used with dim.
  • I recommend not to use out argument because it is useless at all.

arange(). *My post explains arange():

import torch

torch.arange(start=5, end=15, step=4)
# tensor([5, 9, 13])

my_tensor = torch.tensor([0, 1, 2])

torch.arange(start=5, end=15, step=4, out=my_tensor)
# tensor([5, 9, 13])

tensor1 = torch.tensor([0, 1, 2])

tensor2 = torch.arange(start=5, end=15, step=4, out=tensor1)

tensor1, tensor2
# (tensor([5, 9, 13]), tensor([5, 9, 13]))
Enter fullscreen mode Exit fullscreen mode

rand(). *My post explains rand():

import torch

tensor1 = torch.tensor([0., 1., 2.])

tensor2 = torch.rand(size=(3,), out=tensor1)

tensor1, tensor2
# (tensor([0.3379, 0.9394, 0.5509]), tensor([0.3379, 0.9394, 0.5509]))
Enter fullscreen mode Exit fullscreen mode

add(). *My post explains add():

import torch

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

tensor4 = torch.add(input=tensor1, other=tensor2, out=tensor3)

tensor1, tensor2, tensor3, tensor4
# (tensor([1, 2, 3]), tensor([4, 5, 6]), tensor([5, 7, 9]), tensor([5, 7, 9]))
Enter fullscreen mode Exit fullscreen mode

mean(). *My post explains mean():

import torch

tensor1 = torch.tensor([5., 4., 7., 7.])
tensor2 = torch.tensor(9.)

tensor3 = torch.mean(input=tensor1, dim=0, out=tensor2)

tensor1, tensor2, tensor3
# (tensor([5., 4., 7., 7.]), tensor(5.7500), tensor(5.7500))
Enter fullscreen mode Exit fullscreen mode

median(). *My post explains median():

import torch

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

tensor4 = torch.median(input=tensor1, dim=0, out=(tensor2, tensor3))

tensor1, tensor2, tensor3, tensor4
# (tensor([5., 4., 7., 7.]),
#  tensor(5.),
#  tensor(0),
#  torch.return_types.median_out(
#  values=tensor(5.),
#  indices=tensor(0)))
Enter fullscreen mode Exit fullscreen mode

min(). *My post explains min():

import torch

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

tensor4 = torch.min(input=tensor1, dim=0, out=(tensor2, tensor3))

tensor1, tensor2, tensor3, tensor4
# (tensor([5, 4, 7, 7]),
#  tensor(4),
#  tensor(1),
#  torch.return_types.min_out(
#  values=tensor(4),
#  indices=tensor(1)))
Enter fullscreen mode Exit fullscreen mode

max(). *My post explains max():

import torch

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

tensor4 = torch.max(input=tensor1, dim=0, out=(tensor2, tensor3))

tensor1, tensor2, tensor3, tensor4
# (tensor([5, 4, 7, 7]),
#  tensor(7),
#  tensor(2),
#  torch.return_types.max_out(
#  values=tensor(7),
#  indices=tensor(2)))
Enter fullscreen mode Exit fullscreen mode

all(). *My post explains all():

import torch

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

tensor3 = torch.all(input=tensor1, out=tensor2)
tensor3 = torch.all(input=tensor1, dim=0, out=tensor2)

tensor1, tensor2, tensor3
# (tensor([True, False, True, False]), tensor(False), tensor(False))
Enter fullscreen mode Exit fullscreen mode

any(). *My post explains any():

import torch

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

tensor3 = torch.any(input=tensor1, out=tensor2)
tensor3 = torch.any(input=tensor1, dim=0, out=tensor2)

tensor1, tensor2, tensor3
# (tensor([True, False, True, False]), tensor(True), tensor(True))
Enter fullscreen mode Exit fullscreen mode

matmul(). *My post explains matmul():

import torch

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

tensor4 = torch.matmul(input=tensor1, other=tensor2, out=tensor3)

tensor1, tensor2, tensor3, tensor4
# (tensor([2, -5, 4]), tensor([3, 6, -1]), tensor(-28), tensor(-28))
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay