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

Assignment 01 - Deep Learning with PyTorch: Zero to GANs

Tensor Operations by Ian McDowell

PyTorch is a Python library used to make machine learning applications using tensors. The following tensor operations I chose as they seemed interesting and something I would like to learn about.

  • torch.Tensor.log_normal_
  • torch.diag
  • torch.cumsum
  • torch.Tensor.repeat
  • torch.Tensor.reshape
# Import torch and other required modules
import torch

Function 1 - torch.Tensor.log_normal_

Fills the tensor with values along the log normal distribution with a default mean of 1 and default standard deviation (σ\sigma) of 2. The function chooses an arbitrary value of x, plugs it into the following equation, then puts the corresponding f(x)f(x) value in the place of a value in the original tensor.

f(x)=1xσ2πe(lnxμ)22σ2f(x)=\frac{1}{x\sigma\sqrt{2\pi}}e^{-\frac{(\ln{x}-\mu)^2}{2\sigma^2}}

# Example 1 - Using default values for median and sigma
t1 = torch.tensor([[1, 2], [3, 4.]])
print(t1)
print("------------------------")
print(t1.log_normal_())

This example shows the result when you give no inputs for the median or sigma values, so they use their respective default values. Notice that if the code is run multiple times, the t1.log_normal_() will get different values for each place in the tensor.