This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
You can set out
with the functions which have out
argument as shown below:
*Memos:
- I selected some popular
keepdim
argument functions such as arange(), rand() add(), mean(), median(), min(), max(), all(), any() and matmul(). -
out
(Optional,tensor
) can output a tensor. *Sometimes,out
(Optional,tuple
(tensor
,tensor
) orlist
(tensor
,tensor
)). - Basically,
out=
is needed. - Sometimes,
out
needs to be used withdim
.
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]))
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]))
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]))
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))
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)))
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)))
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)))
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))
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))
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))
This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito) | Sciencx (2024-06-29T00:19:41+00:00) Set out with out argument functions PyTorch. Retrieved from https://www.scien.cx/2024/06/29/set-out-with-out-argument-functions-pytorch/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.