Learn practical skills, build real-world projects, and advance your career
Updated 4 years ago
Fundamental PyTorch operations
A very brief exploration of some of the basic but important PyTorch operations
PyTorch is a machine learning framework that is very intuitive to grasp once one has a strong understanding of the basic concepts. Although PyTorch has a plethora of operations, this notebook will strive to clarify the very basics.
import torch
import numpy as np
Function 1 - torch.tensor
At first sight this might seem to be a very simple operation, but in practise an incomplete understanding of the operation can lead to wastage of memory when dealing with very deep neural networks.
torch.tensor
will create a tensor with any data that is passed to it. But remember that this operation will always create a copy of the data that is passed to it. Therefore:
- If your data is a tensor and you want to avoid making a copy of it, use
torch.as_tensor
- If your data is a
numpy
array, use eithertorch.as_tensor
ortorch.from_numpy
to prevent a copy from being created.
torch.tensor([[10, 20], [30, 40.]])
tensor([[10., 20.],
[30., 40.]])