5 ways of creating Tensors

Ravi Ranjan
4 min readMay 29, 2020

--

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.

Visualization of Tensors (Source:https://static.packt-cdn.com)

In this article I am going to discuss 5 basic ways of creating tensors. The 5 basic ways of creating tensors are as follows:

  1. new_tensor()
  2. new_full()
  3. new_empty()
  4. new_ones()
  5. 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 provided None(Default) it returns the type same as torch.dtype.
  • device( torch.device, optional) — The desired device of the tensors. For default( None) it is same as torch.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 to False.
Example illustrating new_tensor() function

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 passed None i.e, default then the same torch.dtype as this tensor.
  • device (torch.device, optional) – The desired device of returned tensor. With Default None,the same torch.device as this tensor.
  • requires_grad (bool, optional) — If autograd should record operations on the returned tensor. Default: False.
Example Illustrating new_full() function

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 is None and returns same torch.dtype as this tensor.
  • device (torch.device, optional) – The device of our desired tensor is returned. With default argument which is Non returns the same torch.device as this tensor.
  • requires_grad (bool, optional) — If autograd is enabled then it records operations on the returned tensor. Default argument is False.
Example Illustrating new_empty() function

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. For None, same torch.dtype as this tensor.
  • device (torch.device, optional) – The desired device of returned tensor. Default: if None, same torch.device as this tensor.
  • requires_grad (bool, optional) — If autograd is enabled then the function should record operations on the returned tensor. Default value isFalse.
Example Illustrating new_ones() function

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, same torch.dtype as this tensor.
  • device (torch.device, optional) – The desired device of returned tensor. Default: if None, same torch.device as this tensor.
  • requires_grad (bool, optional) — If autograd enabled then it should record operations on the returned tensor. Default argument valueFalse.
Example Illustrating new_zeros() function

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:

--

--

Ravi Ranjan

Full Stack Developer | Node | Nest | React | MongoDB | PostgresSQL | Express