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

Assigment 1 : Introduction to Tensors

Pytorch Tensor API

An short introduction about PyTorch and about the chosen functions.

  • new_tensor
  • torch.from_numpy
  • T
  • abs(), abs_()
  • apply_()

Create pytorch environment before execute any of the following
It can be done with the following command.

conda create -n pytorch_env -c pytorch pytorch torchvision

We use conda.One can use pip and virtualenv to achieve same.

Jupyter does not recognize the conda environment directly .
You need ipykernel to add kernel to jupyter.

conda activate pytorch_env
conda install ipykernel
python -m ipykernel install --user --name pytorch_env --display-name "Python (pytorch_env)"

For pre-existing notebooks,

go to Kernel -> Change Kernel -> select pytorch_env
## import torch library and other dependencies you need
import torch
import numpy as np

Function 1 - tensor.new_tensor

Returns a new tensor using another tensor object and data.
Can also play with device(cpu/gpu) and autograd functionality of pytorch

# Example 1 - working 
tensor=torch.tensor([[1, 2], [3, 4.]])
data=[[1.,2.],[3.,4.]]
new_tensor=tensor.new_tensor(data)
print(tensor)
print(new_tensor)