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

5 PyTorch functions for Tensor Analyses and Modification

Tensor Modification and Analyses is essetial for Debuging your Neural Network and getting further information about how your Networks analyses and sees the provided data.

  • is_nonzero
  • add
  • as_tensor
  • eye
  • full

Before we begin, let's install and import PyTorch

# Uncomment and run the appropriate command for your operating system, if required

# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# Windows
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# MacOS
# !pip install numpy torch torchvision torchaudio
# Import torch and other required modules
import torch

Function 1 - torch.is_nonzero()

The function is_nonzero() checks if a value in a tensor is not equal to zero and outputs a boolean value.

# Example 1 - working (change this)
tensor_1 = torch.tensor([[1, 2], [3, 4.]])
torch.is_nonzero(tensor_1[1][1])
True