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

Open In Colab

JovianML - ZerotoGAN - Assignment 1

Some useful methods from Pytorch tensor class

Here I will be discussing some of the useful pytorch methods that will be useful for your datascience projects.

  • torch.add
  • torch.cat
  • apply_
  • torch.abs
  • torch.arange
# Import torch and other required modules
import torch

torch.tensor() always copies data. If you have a Tensor data and just want to change its requires_grad flag, use requires_grad_() or detach() to avoid a copy. If you have a numpy array and want to avoid a copy, use torch.as_tensor().

Function 1 - torch.add

Add a scalar or tensor to self tensor. If both alpha and other are specified, each element of other is scaled by alpha before being used.

When other is a tensor, the shape of other must be broadcastable with the shape of the underlying tensor