Set out with out argument functions PyTorch
Tags: pytorch, out, argument, function
Published: 2024-06-29 00:19:41

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()](https://pytorch.org/docs/stable/generated/torch.arange.html),
[rand()](https://pytorch.org/docs/stable/generated/torch.rand.html)
[add()](https://pytorch.org/docs/stable/generated/torch.add.html),
[mean()](https://pytorch.org/docs/stable/generated/torch.mean.html),
[median()](https://pytorch.org/docs/stable/generated/torch.median.html), [min()](https://pytorch.org/docs/stable/generated/torch.min.html), 
[max()](https://pytorch.org/docs/stable/generated/torch.max.html),
[all()](https://pytorch.org/docs/stable/generated/torch.all.html),
[any()](https://pytorch.org/docs/stable/generated/torch.any.html) and [matmul()](https://pytorch.org/docs/stable/generated/torch.matmul.html).
- `out`(Optional-Type:`tensor`) can have a returned tensor. *Sometimes, `out`(Optional-Type:`tuple`(`tensor`, `tensor`) or `list`(`tensor`, `tensor`)).
- Basically, `out` can be used with [torch](https://pytorch.org/docs/stable/torch.html) but not with a tensor.
- Basically, `out=` is needed.
- Sometimes, `out` needs to be used with `dim`.
- I recommend not to use `out` argument because it is useless at all.

`arange()`. *[My post](https://dev.to/hyperkai/arange-linspace-logspace-and-normal-in-pytorch-a87) explains `arange()`:

```python
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](https://dev.to/hyperkai/rand-randlike-randn-randnlike-randint-and-randperm-in-pytorch-31nc) explains `rand()`:

```python
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](https://dev.to/hyperkai/add-sub-and-mul-in-pytorch-1m1f) explains `add()`:

```python
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](https://dev.to/hyperkai/mean-and-median-in-pytorch-23h7) explains `mean()`:

```python
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](https://dev.to/hyperkai/mean-and-median-in-pytorch-23h7) explains `median()`:

```python
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](https://dev.to/hyperkai/min-max-argmin-and-argmax-in-pytorch-2d6) explains `min()`:

```python
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](https://dev.to/hyperkai/min-max-argmin-and-argmax-in-pytorch-2d6) explains `max()`:

```python
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](https://dev.to/hyperkai/min-max-argmin-and-argmax-in-pytorch-2d6) explains `all()`:

```python
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](https://dev.to/hyperkai/min-max-argmin-and-argmax-in-pytorch-2d6) explains `any()`:

```python
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](https://dev.to/hyperkai/matmul-and-dot-in-pytorch-4e8p) explains `matmul()`:

```python
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))
```