is_tensor(), numel() and device() in PyTorch
Tags: pytorch, istensor, numel, device
Published: 2024-05-24 22:20:20

*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/create-a-tensor-in-pytorch-127g) explains how to create a tensor.
- [My post](https://dev.to/hyperkai/access-a-tensor-in-pytorch-1f4e) explains how to access a tensor.
- [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).

[is_tensor()](https://pytorch.org/docs/stable/generated/torch.is_tensor.html) can check if an object is a PyTorch tensor, getting the scalar of a boolean value as shown below:

*Memos:
- `is_tensor()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) but not with a tensor.
- The 1st argument with `torch` is `obj`(Required-Type:`object`).

```python
import torch
import numpy as np

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

torch.is_tensor(obj=pytorch_tensor) # True

numpy_tensor = np.array([0., 1., 2.])

torch.is_tensor(obj=numpy_tensor) # False

torch.is_tensor(obj=7) # False

torch.is_tensor(obj=7.) # False

torch.is_tensor(obj=7.+0.j) # False

torch.is_tensor(obj=True) # False

torch.is_tensor(obj='Hello') # False
```

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

*Memos:
- `numel()` can be used with `torch` or a tensor.
- The 1st argument with `torch` is `input`(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`).

```python
import torch

my_tensor = torch.tensor(7)

torch.numel(input=my_tensor)
my_tensor.numel()
# 1

my_tensor = torch.tensor([7, 5, 8])

torch.numel(input=my_tensor) # 3

my_tensor = torch.tensor([[7, 5, 8],
                          [3, 1, 6]])
torch.numel(input=my_tensor) # 6

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

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

my_tensor = torch.tensor([[[7.+0.j, -5.+0.j, 8.+0.j],
                           [-3.+0.j, 1.+0.j, 6.+0.j]],
                          [[0.+0.j, 9.+0.j, 2.+0.j],
                           [4.+0.j, -7.+0.j, -9.+0.j]]])
torch.numel(input=my_tensor) # 12

my_tensor = torch.tensor([[[True, False, True], [True, False, True]],
                          [[False, True, False], [False, True, False]]])
torch.numel(input=my_tensor) # 12
```

[device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch-device) can represent a device as shown below:

*Memos:
- `device()` can be used with `torch` but not with a tensor.
- The 1st argument with `torch` is `device`(Required-Type:`str`, `int` or [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch-device)).
- The 1st argument with `torch` is `type`(Required-Type:`str`).
- The 2nd argument with `torch` is `index`(Optional-Type:`int`). *`index` must be used with `type`.
- `cpu`, `cuda`, `ipu`, `xpu`, `mkldnn`, `opengl`, `opencl`, `ideep`, `hip`, `ve`, `fpga`, `ort`, `xla`, `lazy`, `vulkan`, `mps`, `meta`, `hpu`, `mtia` or `privateuseone` can be set to `device` or `type`.
- Setting 0 to `device` uses cuda(GPU). *The number must be zero or positive.

```python
import torch

torch.device(device='cpu')
torch.device(type='cpu')
# device(type='cpu')

torch.device(device='cuda:0')
torch.device(device=0)
torch.device(type='cuda', index=0)
torch.device(device=torch.device(device='cuda:0'))
torch.device(device=torch.device(device=0))
torch.device(device=torch.device(type='cuda', index=0))
# device(type='cuda', index=0)

torch.device(device='cuda')
torch.device(type='cuda')
torch.device(device=torch.device(device='cuda'))
torch.device(device=torch.device(type='cuda'))
# device(type='cuda')
```