Quick 5 minute intro to PyTorch

Srijan Bhushan
2 min readDec 23, 2023

Embark on your machine learning journey with PyTorch, an open-source library that unlocks a world of possibilities for development. In the realm of machine learning, PyTorch stands out as a versatile tool, empowering you to construct sophisticated models tailored to your unique requirements. PyTorch simplifies the entire process, from effortlessly preparing your data to seamlessly training and predicting with your models.

Get started by installing PyTorch on your mac. To install, execute the following command. This command is for non-gpu version on PyTorch. This usually takes a few minutes to install. Other installation options are described here.

conda install pytorch torchvision -c pytorch

Go to your python environment now by entering the python in the terminal.

Run the following piece of code in your python environment. This is the most basic code in PyTorch. This code creates a Tensor, a unit of data in PyTorch. Tensors are the foundational building blocks in PyTorch.

import torch

# set a see for deteministic tensor i.e. each time you will creat the tensor
# it will remain the same
torch.manual_seed(42)

# this will create a new tensor of 5 x 3 size
x = torch.rand(5, 3)

print(x)

# the result will be as follows
tensor([[0.8823, 0.9150, 0.3829],
[0.9593, 0.3904, 0.6009],
[0.2566, 0.7936, 0.9408],
[0.1332, 0.9346, 0.5936],
[0.7954, 0.2783, 0.8765]])

Tensors are a specialized data structure that are very similar to arrays and matrices. If you have used numpy library before, you would recall numpy arrays, which are very similar to tensors. The key difference is that, tensors can run on GPUs or other hardware accelerators, numpy arrays cannot.

Now that you can create a simple tensor, try adding a value to each of the values of the tensor. For example, the code below adds 20 to each of the tensor values.

print(torch.add(a, 20))

#the result will be as follows
tensor([[20.8823, 20.9150, 20.3829],
[20.9593, 20.3904, 20.6009],
[20.2566, 20.7936, 20.9408],
[20.1332, 20.9346, 20.5936],
[20.7954, 20.2783, 20.8765]])

Next, you can try calculating the sum of two tensors. The following

# this will create a new tensor of 5 x 3 size
y = torch.rand(5, 3)
peint(y)

# the y tensor will be as following
tensor([[0.1461, 0.7659, 0.7948],
[0.3134, 0.1455, 0.3489],
[0.0998, 0.6124, 0.7027],
[0.8783, 0.2584, 0.8082],
[0.3099, 0.8564, 0.8767]])

# the following take the tensor and performs sum of x and y
x_y_sum = x + y

print(x_y_sum)

# following will be the result
tensor([[1.0284, 1.6809, 1.1777],
[1.2727, 0.5359, 0.9497],
[0.3564, 1.4060, 1.6435],
[1.0115, 1.1930, 1.4017],
[1.1052, 1.1347, 1.7532]])

There are numerous operations that can be performed on a tensor. Other operations you can perform with tensors are described here in this document. Remember that PyTorch and machine learning involve a lot matrix manipulation (cross product, addition, subtraction etc). In PyTorch the tensor is the matrix. To get started with PyTorch, learning how to manipulate these tensors will be very valuable and will start your journey the right way.

References:

PyTorch documentation: https://pytorch.org/get-started/locally/

--

--