arange(), linspace(), logspace() and normal() in PyTorch
Tags: pytorch, arange, linspace, logspace
Published: 2024-04-10 20:27:27

[arange()](https://pytorch.org/docs/stable/generated/torch.arange.html) can create the 1D tensor of zero or integers or floating-point numbers as shown below:

*Memos:
- `arange()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) but not with a tensor.
- The 1st argument with `torch` is `start`(Optional-Default:`0`-Type:`int`, `float`, `complex` or `bool`):
  *Memos
  - It must be lower than or equal to `end`. 
  - A 0D tensor of `int`, `float`, `complex` or `bool` also works.
- The 2nd argument with `torch` is `end`(Required-Type:`int`, `float`, `complex` or `bool`):
  *Memos:
  - It must be greater than or equal to `start`.
  - A 0D tensor of `int`, `float`, `complex` or `bool` also works.
- The 3rd argument with `torch` is `step`(Optional-Default:`1`-Type:`int`, `float`, `complex` or `bool`):
  *Memos:
  - It must be greater than 0.
  - A 0D tensor of `int`, `float`, `complex` or `bool` also works.
- There is `dtype` argument with `torch`(Optional-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)):
  *Memos:
  - If `dtype` is not given, `dtype` is inferred from `start`, `end` or `step` or `dtype` of [set_default_dtype()](https://pytorch.org/docs/stable/generated/torch.set_default_dtype.html) is used for floating-point numbers.
  - `dtype=` must be used.
  - [My post](https://dev.to/hyperkai/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch-13h2) explains `dtype` argument.
- There is `device` argument with `torch`(Optional-Type:`str`, `int` or [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch.device)):
  *Memos:
  - `device=` must be used.
  - [My post](https://dev.to/hyperkai/set-device-with-device-argument-functions-and-get-it-in-pytorch-1o2p) explains `device` argument.
- There is `requires_grad` argument with `torch`(Optional-Type:`bool`):
  *Memos:
  - `requires_grad=` must be used.
  - [My post](https://dev.to/hyperkai/set-requiresgrad-with-requiresgrad-argument-functions-and-get-it-in-pytorch-39c3) explains `requires_grad` argument.
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.
- There is [range()](https://pytorch.org/docs/stable/generated/torch.range.html) which is similar to `arange()` but `range()` is deprecated.

```python
import torch

torch.arange(end=5)
# tensor([0, 1, 2, 3, 4])

torch.arange(start=5, end=15)
# tensor([5, 6, 7, 8, 9, 10, 11, 12, 13, 14])

torch.arange(start=5, end=15, step=3)
# tensor([5, 8, 11, 14])

torch.arange(start=-5, end=5)
# tensor([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4])

torch.arange(start=-5, end=5, step=3)
torch.arange(start=torch.tensor(-5),
             end=torch.tensor(5),
             step=torch.tensor(3))
# tensor([-5, -2, 1, 4])

torch.arange(start=-5., end=5., step=3.)
torch.arange(start=torch.tensor(-5.),
             end=torch.tensor(5.),
             step=torch.tensor(3.))
# tensor([-5., -2., 1., 4.])

torch.arange(start=-5.+0.j, end=5.+0.j, step=3.+0.j)
torch.arange(start=torch.tensor(-5.+0.j),
             end=torch.tensor(5.+0.j),
             step=torch.tensor(3.+0.j))
# tensor([-5., -2., 1., 4.])

torch.arange(start=False, end=True, step=True)
torch.arange(start=torch.tensor(False),
             end=torch.tensor(True),
             step=torch.tensor(True))
# tensor([0])
```

[linspace()](https://pytorch.org/docs/stable/generated/torch.linspace.html)
can create the 1D tensor of the zero or more integers, floating-point numbers or complex numbers evenly spaced as shown below:

*Memos:
- `linspace()` can be used with `torch` but not with a tensor.
- The 1st argument with `torch` is `start`(Required-Type:`int`, `float`, `complex` or `bool`). *A 0D tensor of `int`, `float`, `complex` or `bool` also works.
- The 2nd argument with `torch` is `end`(Required-Type:`int`, `float`, `complex` or `bool`). *A 0D tensor of `int`, `float`, `complex` or `bool` also works.
- The 3rd argument with `torch` is `steps`(Required-Type:`int`). *A 0D tensor of `int` also works.
- There is `dtype` argument with `torch`(Optional-Type:[dtype](https://pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)):
  *Memos:
  - If `dtype` is not given, `dtype` is inferred from `start`, `end` or `step` or `dtype` of [set_default_dtype()](https://pytorch.org/docs/stable/generated/torch.set_default_dtype.html) is used for floating-point numbers.
  - Setting `start` and `end` of integer type is not enough to create the 1D tensor of integer type so integer type with `dtype` must be set.
  - The 1D tensor of boolean type cannot be created.
  - `dtype=` must be used.
  - [My post](https://dev.to/hyperkai/set-dtype-with-dtype-argument-functions-and-get-it-in-pytorch-13h2) explains `dtype` argument.
- There is `device` argument with `torch`(Optional-Type:`str`, `int` or [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch.device)):
  *Memos:
  - `device=` must be used.
  - [My post](https://dev.to/hyperkai/set-device-with-device-argument-functions-and-get-it-in-pytorch-1o2p) explains `device` argument.
- There is `requires_grad` argument with `torch`(Optional-Type:`bool`):
  *Memos:
  - `requires_grad=` must be used.
  - [My post](https://dev.to/hyperkai/set-requiresgrad-with-requiresgrad-argument-functions-and-get-it-in-pytorch-39c3) explains `requires_grad` argument.
- There is `out` argument with `torch`(Optional-Type:`tensor`):
  *Memos:
  - `out=` must be used.
  - [My post](https://dev.to/hyperkai/set-out-with-out-argument-functions-pytorch-3ee) explains `out` argument.

```python
import torch

torch.linspace(start=10, end=20, steps=0)
torch.linspace(start=20, end=10, steps=0)
# tensor([])

torch.linspace(start=10., end=20., steps=1)
tensor([10.])

torch.linspace(start=20, end=10, steps=1)
# tensor([20.])

torch.linspace(start=10., end=20., steps=2)
# tensor([10., 20.])

torch.linspace(start=20, end=10, steps=2)
# tensor([20., 10.])

torch.linspace(start=10., end=20., steps=3)
# tensor([10., 15., 20.])

torch.linspace(start=20, end=10, steps=3)
# tensor([20., 15., 10.])

torch.linspace(start=10., end=20., steps=4)
# tensor([10.0000, 13.3333, 16.6667, 20.0000])

torch.linspace(start=20., end=10., steps=4)
# tensor([20.0000, 16.6667, 13.3333, 10.0000])

torch.linspace(start=10, end=20, steps=4, dtype=torch.int64)
torch.linspace(start=torch.tensor(10),
               end=torch.tensor(20),
               steps=torch.tensor(4),
               dtype=torch.int64)
# tensor([10.0000, 13.3333, 16.6667, 20.0000])

torch.linspace(start=10.+6.j, end=20.+3.j, steps=4)
torch.linspace(start=torch.tensor(10.+6.j),
               end=torch.tensor(20.+3.j),
               steps=torch.tensor(4))
# tensor([10.0000+6.j, 13.3333+5.j, 16.6667+4.j, 20.0000+3.j])

torch.linspace(start=False, end=True, steps=4)
torch.linspace(start=torch.tensor(True),
               end=torch.tensor(False),
               steps=torch.tensor(4))
# tensor([0.0000, 0.3333, 0.6667, 1.0000])

torch.linspace(start=10, end=20, steps=4, dtype=torch.int64)
torch.linspace(start=torch.tensor(10),
               end=torch.tensor(20),
               steps=torch.tensor(4), dtype=torch.int64)
# tensor([10.0000, 13.3333, 16.6667, 20.0000])
```

[logspace()](https://pytorch.org/docs/stable/generated/torch.logspace.html)
can basically create the 1D tensor of the zero or more floating-point numbers, integers or complex numbers which are evenly spaced from <code>base<sup>start</sup></code> to <code>base<sup>end</sup></code> as shown below:

*Memos:
- `logspace()` can be used only from `torch` but not from a tensor.
- The 1st argument(Required) is `start`.
- The 2nd argument(Required) is `end`. 
- The 3rd argument(Required) is `steps`.
- The 4th argument(Optional) is `base` which is `10.0` by default. 
- The exponential parts <code><sup>start</sup></code> and <code><sup>end</sup></code> are evenly spaced.
- Setting `start` and `end` of integer type is not enough to get the tensor of integer type so we must set integer type to `dtype`.

```python
import torch

torch.logspace(10, 20, 0)
torch.logspace(10., 20., 0)
# tensor([])
torch.logspace(10, 20, 1)
torch.logspace(10., 20., 1)
# tensor([1.0000e+10])
torch.logspace(10, 20, 2)
torch.logspace(10., 20., 2)
# tensor([1.0000e+10, 1.0000e+20])
torch.logspace(10, 20, 3)
torch.logspace(10., 20., 3)
# tensor([1.0000e+10, 1.0000e+15, 1.0000e+20])
torch.logspace(10, 20, 4)
torch.logspace(10., 20., 4)
# tensor([1.0000e+10, 2.1544e+13, 4.6416e+16, 1.0000e+20])
torch.logspace(10, 20, 4, 100)
torch.logspace(10., 20., 4, 100)
# tensor([1.0000e+20, 4.6416e+26, 2.1544e+33, inf])

torch.logspace(10, 20, 4, 100, dtype=torch.int64)
# tensor([-9223372036854775808,
#         -9223372036854775808, 
#         -9223372036854775808,
#         -9223372036854775808])

torch.logspace(10+6j, 20+3j, 4, 100)
# tensor([-8.0012e+19+5.9985e+19j,
#         -2.3708e+26-3.9904e+26j,
#         1.9593e+33-8.9592e+32j,
#         inf+infj])
```

[normal()](https://pytorch.org/docs/stable/generated/torch.normal.html) can create the 1D or more D tensor of zero or more random floating-point numbers(Default) or complex numbers from normal distribution as shown below:

*Memos:
- `normal()` can be used only from `torch` but not from a tensor.
- The default type is `float32`.
- The 1st argument is `mean`.
- The 2nd argument is `std`(Standard deviation).
- The 3rd argument is `size`.

```python
import torch

torch.normal(0, 1, (0,))
# tensor([])
torch.normal(0, 1, (3,))
# tensor([2.4737, 1.0925, -0.2984])
torch.normal(0, 1, (3, 2))
# tensor([[ 1.5716,  0.2892], [-2.7665, -1.1040], [-0.8145, -0.0520]])
torch.normal(0, 1, (3, 2, 4))
# tensor([[[0.4032, -1.1011, -0.2356, -2.3010],
#          [0.3675, -0.1115, -0.9581, -1.3232]],
#         [[1.0273, 0.9534, 1.2640, -0.2971],
#          [2.5688, -0.0851, 0.7737, 1.2036]],
#         [[0.7446, -0.2630, -1.4919, -0.7096],
#          [-0.3706, 0.0073, 0.8246, 0.9313]]])
torch.normal(0, 1, (3, 2, 4), dtype=torch.complex64)
# tensor([[[0.1640-0.8105j, -0.0654+0.2241j,
#           0.1629-0.8095j, -0.2853+1.0529j],
#          [-0.4795-0.5646j, -0.6884-0.1562j,
#           -0.6333+0.7238j, 0.8710-1.1705j]],
#         [[-1.5067+1.2063j, 0.6296-0.0607j,
#           0.7197-0.4113j, 0.1742+0.3301j],
#          [-0.4388+1.0028j, 0.0297+0.2668j,
#           -0.3234+0.7390j,  0.0336+0.5532j]],
#         [[0.1296+0.9593j, -0.1632+0.3848j,
#           -0.6574-0.0247j, -0.6997-0.0548j],
#          [0.5176+1.0693j, 0.6728+1.2304j,
#           -0.6519+0.0970j, -0.5081-0.4026j]]])

torch.normal(5, 4, (0,))
# tensor([])
torch.normal(5, 4, (3,))
# tensor([7.2012, -1.6016, 5.2472])
torch.normal(5, 4, (3, 2))
# tensor([[9.6180, 12.7688], [-1.8151, 3.9245], [0.3551, 4.5507]])
torch.normal(5, 4, (3, 2, 4))
# tensor([[[9.7707+5.7214j, 4.7577+12.6819j,
#           7.8147+9.3258j, 4.5580+9.2139j],
#          [1.6257+3.8480j, 1.3294+4.7965j,
#           2.8914+4.5585j, 5.9573+4.1559j]],
#         [[8.0873+4.8930j, 2.9459+4.8999j,
#           6.6957+3.7738j, 1.4319+7.7348j],
#          [5.5551+5.8145j, 1.9599+8.7604j,
#           6.1416+5.7876j, 2.5204+3.0414j]],
#         [[4.5839+6.1299j, 3.1245+3.8965j,
#           9.1091+6.3294j, 2.8272+8.0571j],
#          [2.5482+3.1353j, 3.4173+2.0660j,
#           9.1401+5.8971j, 4.5078+4.3233j]]])
```