Create and access a tensor in PyTorch
Tags: pytorch, create, access, tesnor
Published: 2024-06-30 03:51:13

*Memos:
- [My post](https://dev.to/hyperkai/check-pytorch-version-cpu-and-gpucuda-in-pytorch-6jk) explains how to check PyTorch version, CPU and GPU(CUDA).
- [My post](https://dev.to/hyperkai/access-a-tensor-in-pytorch-1f4e) explains how to access a tensor.
- [My post](https://dev.to/hyperkai/istensor-numel-and-device-in-pytorch-2eha) explains [is_tensor()](https://pytorch.org/docs/stable/generated/torch.is_tensor.html), [numel()](https://pytorch.org/docs/stable/generated/torch.numel.html) and [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch-device).
- [My post](https://dev.to/hyperkai/type-conversion-with-type-to-and-a-tensor-in-pytorch-2a0g) explains type conversion with [type()](https://pytorch.org/docs/stable/generated/torch.Tensor.type.html), [to()](https://pytorch.org/docs/stable/generated/torch.Tensor.to.html) and a tensor.
- [My post](https://dev.to/hyperkai/type-promotion-resulttype-promotetypes-and-cancast-in-pytorch-33p8) explains type promotion, [result_type()](https://pytorch.org/docs/stable/generated/torch.result_type.html), [promote_types()](https://pytorch.org/docs/stable/generated/torch.promote_types.html) and [can_cast()](https://pytorch.org/docs/stable/generated/torch.can_cast.html).
- [My post](https://dev.to/hyperkai/device-conversion-fromnumpy-and-numpy-in-pytorch-1iih) explains device conversion, [from_numpy()](https://pytorch.org/docs/stable/generated/torch.from_numpy.html) and [numpy()](https://pytorch.org/docs/stable/generated/torch.Tensor.numpy.html).
- [My post](https://dev.to/hyperkai/setdefaultdtype-setdefaultdevice-and-setprintoptions-in-pytorch-55g8) explains [set_default_dtype()](https://pytorch.org/docs/stable/generated/torch.set_default_dtype.html), [set_default_device()](https://pytorch.org/docs/stable/generated/torch.set_default_device.html) and [set_printoptions()](https://pytorch.org/docs/stable/generated/torch.set_printoptions.html).
- [My post](https://dev.to/hyperkai/manualseed-initialseed-and-seed-in-pytorch-5gm8) explains [manual_seed()](https://pytorch.org/docs/stable/generated/torch.manual_seed.html), [initial_seed()](https://pytorch.org/docs/stable/generated/torch.initial_seed.html) and [seed()](https://pytorch.org/docs/stable/generated/torch.seed.html).

[tensor()](https://pytorch.org/docs/stable/generated/torch.tensor.html) can create the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `tensor()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) but not with a tensor.
- The 1st argument with `torch` is `data`(Required-Type:`int`, `float`, `complex` or `bool` or `tuple` of `int`, `float`, `complex` or `bool` or `list` of `int`, `float`, `complex` or `bool`). *The default type is `float32`.
- 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 `data` 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.
- The one or more floating-point numbers or complex numbers of a tensor are rounded to 4 decimal places by default.

```python
import torch

""" 0D tensor """

my_tensor = torch.tensor(data=-3)

my_tensor
# tensor(-3)

""" 1D tensor """

torch.tensor(data=[3, 7, -5])
# tensor([3, 7, -5])

torch.tensor(data=[3.635251, 7.270649, -5.164872])
# tensor([3.6353, 7.2706, -5.1649])

torch.tensor(data=[3.635251+4.634852, 7.27+2.586449j, -5.164872-3.45])
# tensor([0.9996+0.0000j, 7.2700+2.5864j, -8.6149+0.0000j])

torch.tensor(data=[True, False, True])
# tensor([True, False, True])

""" 2D tensor """

torch.tensor(data=[[3, 7, -5], [-9, 6, 2]])
# tensor([[3, 7, -5], [-9, 6, 2]])

""" 3D tensor """

torch.tensor(data=[[[3, 7, -5], [-9, 6, 2]],
                   [[8, 0, -1], [4, 9, -6]]])
# tensor([[[3, 7, -5], [-9, 6, 2]],
#         [[8, 0, -1], [4, 9, -6]]])
```

In addition, [Tensor()](https://pytorch.org/docs/stable/tensors.html) can create the 1D or more D tensor of zero or more floating-point numbers as shown below: 

*Memos:
- `tensor()` can be used with `torch` but not with a tensor.
- The 1st argument with `torch` is `data`(Required-Type:`tuple` of `int`, `float` or `bool` or `list` of `int`, `float` or `bool`).
- The one or more floating-point numbers or complex numbers of a tensor are rounded to 4 decimal places by default.
 
```python
import torch

torch.Tensor(data=[3., 7., -5.]) # 1D tensor
# tensor([3., 7., -5.])

torch.Tensor(data=[[3., 7., -5.], [-9., 6., 2.]]) # 2D tensor
# tensor([[-3., 7., -5.], [-9., 6., 2.]])

torch.Tensor(data=[[[-3., 7., -5.], [-9., 6., 2.]], # 3D tensor
                   [[8., 0., -1.], [4., 9., -6.]]])
# tensor([[[-3., 7., -5.], [-9., 6., 2.]],
#         [[8., 0., 1.], [4., 9., -6.]]])

torch.Tensor(data=[[[-3., 7., -5.], [-9., 6., 2.]], # 3D tensor
                   [[8., 0., -1], [4., 9., -6.]]])
# tensor([[[-3., 7., -5.], [-9., 6., 2.]],
#         [[8., 0., -1.], [4., 9., -6.]]])

torch.Tensor(data=[[[-3, 7, -5], [-9, 6, 2]], # 3D tensor
                   [[8, 0, -1], [4, 9, -6]]])
# tensor([[[-3., 7., -5.], [-9., 6., 2.]],
#         [[8., 0., -1.], [4., 9., -6.]]])

torch.Tensor(data=[[[True, False, True], [True, False, True]], # 3D tensor
                   [[False, True, False], [False, True, False]]])
# tensor([[[1., 0., 1.], [1., 0., 1.]],
#         [[0., 1., 0.], [0., 1., 0.]]])
```

You can access a 0D or more D tensor with these ways as shown below. *I give much more ways to access a 1D tensor than a 0D, 2D and 3D tensor:

```python
import torch

my_tensor = torch.tensor(3) # 0D tensor

my_tensor
# tensor(3)

my_tensor = torch.tensor([3]) # 1D tensor

my_tensor
# tensor([3])

my_tensor = torch.tensor([3, 7, -5, -9, 6, 2, 8, 0, -1, 4, 9, -6])
                         # 1D tensor
my_tensor[4]
my_tensor[4,]
my_tensor[-10]
my_tensor[-10,]
my_tensor[4:5]
my_tensor[4:5,]
my_tensor[-8:5]
my_tensor[-8:5,]
my_tensor[4:-7]
my_tensor[4:-7,]
my_tensor[-8:-7]
my_tensor[-8:-7,]
# tensor(6)

my_tensor[4:8]
my_tensor[4:8,]
my_tensor[-8:8]
my_tensor[-8:8,]
my_tensor[4:-4]
my_tensor[4:-4,]
my_tensor[-8:-4]
my_tensor[-8:-4,]
# tensor([6, 2, 8, 0])

my_tensor[:7]
my_tensor[:7,]
my_tensor[:-5]
my_tensor[:-5,]
my_tensor[0:7]
my_tensor[0:7,]
my_tensor[-12:7]
my_tensor[-12:7,]
my_tensor[0:-5]
my_tensor[0:-5,]
my_tensor[-12:-5]
my_tensor[-12:-5,]
# tensor([3, 7, -5, -9, 6, 2, 8])

my_tensor[5:]
my_tensor[5:,]
my_tensor[-7:]
my_tensor[-7:,]
my_tensor[5:12]
my_tensor[5:12,]
my_tensor[-7:12]
my_tensor[-7:12,]
# tensor([2, 8, 0, -1, 4, 9, -6])

my_tensor[:]
my_tensor[:,]
my_tensor[0:12]
my_tensor[0:12,]
# tensor([3, 7, -5, -9, 6, 2, 8, 0, -1, 4, 9, -6])

my_tensor = torch.tensor([[3, 7, -5, -9, 6, 2],
                          [8, 0, -1, 4, 9, -6]])
my_tensor[1]             # 2D tensor
my_tensor[:][1]
my_tensor[1, :]
# tensor([8, 0, -1, 4, 9, -6])

my_tensor[1][3]
my_tensor[1, 3]
# tensor(4)

my_tensor[1][:4]
my_tensor[1, :4]
# tensor([8, 0, -1,  4])

my_tensor[1][2:]
my_tensor[1, 2:]
# tensor([-1, 4, 9, -6])

my_tensor[:, 3]
# tensor([-9, 4])

my_tensor[:]
# tensor([[3, 7, -5, -9, 6, 2],
#         [8, 0, -1, 4, 9, -6]])

my_tensor = torch.tensor([[[-3, 7, -5], [-9, 6, 2]],
                          [[8, 0, -1], [4, 9, -6]]])
my_tensor[1]             # 3D tensor
my_tensor[:][1]
my_tensor[1, :]
my_tensor[1][:2]
my_tensor[1, :2]
my_tensor[1][0:]
my_tensor[1, 0:]
# tensor([[8, 0, -1], [4, 9, -6]])

my_tensor[1][0]
# tensor([8, 0, -1])

my_tensor[1][0][2]
my_tensor[1, 0, 2]
# tensor(-1)

my_tensor[1][0][:2]
my_tensor[1, 0, :2]
# tensor([8, 0])

my_tensor[1][0][1:]
my_tensor[1, 0, 1:]
# tensor([0, -1])

my_tensor[:, :, 1]
# tensor([[7, 6], [0, 9]])

my_tensor[:]
# tensor([[[-3, 7, -5], [-9, 6, 2]],
#         [[8, 0, -1], [4, 9, -6]]])
```