Learn practical skills, build real-world projects, and advance your career

Five useful function for tensor manipulation

PyTorch python library for machine learning and Deep learning projects. this library has multiple functions used to manage tensor along any learning project. In this blog, five useful functions are discussed with easy example.

  • As_tensor
  • Arange
  • Cat
  • Permute
  • Vsplit

Before we begin, let's install and import PyTorch

# Import torch and other required modules
import torch
import numpy as np

Function 1 - torch.as_tensor

This torch function convert a the provided data as a torch tensor

this function should be given follwing parameters:

data : the first parameter should be an array_like such as a list, tuple, NumPy ndarray, scalar, and other types.

dtype (optional) :it specifies the desired tensor data type. the default is none it not specified.

device (optional) : if mentioned it specifies the de GPU or CPU should be used for the tensor.the default is None.

# Example 1 - 
n_array = np.array([23, 45, 56, 78])
torch.as_tensor(n_array)
tensor([23, 45, 56, 78])

Explanation about example