5 ways of creating Tensors
PyTorch is an open source machine learning library based on the Torch library.It is used for applications such as computer vision and natural language processing, primarily developed by Facebook’s AI Research lab.It is free and open-source software released under the Modified BSD license.
Tensors are generalization of vectors and matrices and can be easily understood as a multidimensional array. For example, scalars are rank-0 tensor or 0-d tensor, vectors are 1-d or rank-1 tensor, and matrices are 2-d or rank-2 tensor. A visualization of tensors is shown in the following figure.
In this article I am going to discuss 5 basic ways of creating tensors. The 5 basic ways of creating tensors are as follows:
new_tensor()
new_full()
new_empty()
new_ones()
new_zeros()
Function — 1
new_tensor(data, dtype=None, device=None, requires_grad=False) →Tensor
This function returns a new Tensor in which data
is the tensor data and by default it has the same torch.dtype
and torch.device
as the tensor.
Parameters
- data(array like) — The returned tensor copies
data
. - dtype(
torch.dtype
, optional) — It is the desired type of the returned tensor. If providedNone
(Default) it returns the type same astorch.dtype
. - device(
torch.device
, optional) — The desired device of the tensors. For default(None
) it is same astorch.device
as this tensor. - requires_grad(
bool
, optional) —It signifies whether the autograd should record operations on the returned tensor. The default argument is assigned toFalse
.
Function — 2
new_full(size, fill_value, dtype=None, device=None,requires_grad=False) →Tensor
This function returns a Tensor of size size
filled with fill_value
. By default, returned tensor has the same torch.dtype
and torch.device
as this sensor.
Parameters
- fill_value (scalar) — It is the number by which we have to fill the output tensor with.
- dtype (
torch.dtype
, optional) – The desired type of returned tensor. If it is passedNone
i.e, default then the sametorch.dtype
as this tensor. - device (
torch.device
, optional) – The desired device of returned tensor. With DefaultNone
,the sametorch.device
as this tensor. - requires_grad (bool, optional) — If autograd should record operations on the returned tensor. Default:
False
.
Function — 3
new_empty(size, dtype=None, device=None, requires_grad=False) → Tensor
It returns a Tensor of size size
filled with uninitialized data with datatype as torch.dtype
and tensor as torch.device
.
- dtype (
torch.dtype
, optional) – The type of the tensor as required by us is returned. The default type isNone
and returns sametorch.dtype
as this tensor. - device (
torch.device
, optional) – The device of our desired tensor is returned. With default argument which isNon
returns the sametorch.device
as this tensor. - requires_grad (bool, optional) — If autograd is enabled then it records operations on the returned tensor. Default argument is
False
.
Function — 4
new_ones(size, dtype=None, device=None, requires_grad=False) → Tensor
It creates or Returns a Tensor of size size
containing only 1s. The returned Tensor has the same datatype as tensor.dtype
and tensor.device
as the tensor.
Parameters
- size (int…) — It defines a list, a tuple, or
torch.Size
of integers defining the shape of the output tensor. - dtype (
torch.dtype
, optional) – The desired type of returned tensor. ForNone
, sametorch.dtype
as this tensor. - device (
torch.device
, optional) – The desired device of returned tensor. Default: if None, sametorch.device
as this tensor. - requires_grad (bool, optional) — If autograd is enabled then the function should record operations on the returned tensor. Default value is
False
.
Function — 5
new_zeros(size, dtype=None, device=None, requires_grad=False) → Tensor
This function returns a tensor with the size provided by the argument size
which is filled with 0s and by default the returned tensor has the same torch.dtype
and torch.device
as the tensor.
Parameters
- size (int…) —This argument signifies a list, a tuple, or
torch.Size
of integers defining the shape of the output tensor. - dtype (
torch.dtype
, optional) – The desired type of returned tensor. Default: if None, sametorch.dtype
as this tensor. - device (
torch.device
, optional) – The desired device of returned tensor. Default: if None, sametorch.device
as this tensor. - requires_grad (bool, optional) — If autograd enabled then it should record operations on the returned tensor. Default argument value
False
.
Conclusion
In this article we have explored the five basic ways by which we can create tensors. As we have seen we have created tensors with values provided, tensors with only zeros, tensors with uninitialized values, etc. These are the basic functions in PyTorch.Tensor library.
References
Official PyTorch Documentation:
Official Tensor Documentation: