4  Pytorch Tutorial

4.1 Installation

The following command can be used to install PyTorch on MacOS:

pip3 install torch torchvision

After installation, try running the following code to verify that PyTorch is installed correctly:

import torch
x = torch.rand(5, 3)
print(x)
tensor([[0.7465, 0.0595, 0.8364],
        [0.6159, 0.3094, 0.3545],
        [0.2987, 0.6285, 0.2690],
        [0.5441, 0.5135, 0.8594],
        [0.6881, 0.6887, 0.2619]])

4.2 Tensors

4.2.1 Initialization of Tensors

Tensors can be initialized from lists, NumPy arrays, or using built-in functions.

4.2.1.1 From a list

# From a list
data_list = [[1, 2], [3, 4]]
tensor_from_list = torch.tensor(data_list)
print(tensor_from_list)
tensor([[1, 2],
        [3, 4]])

4.2.1.2 From a NumPy array

# From a NumPy array
import numpy as np

data_array = np.array([[5, 6], [7, 8]])
tensor_from_array = torch.from_numpy(data_array)
print(tensor_from_array)
tensor([[5, 6],
        [7, 8]])

4.2.1.3 Using built-in functions

# Using built-in functions
tensor_zeros = torch.zeros((2, 3))
print(tensor_zeros)
tensor_ones = torch.ones_like(tensor_from_list)
print(tensor_ones)
tensor([[0., 0., 0.],
        [0., 0., 0.]])
tensor([[1, 1],
        [1, 1]])

4.2.2 Attributes of Tensors

Tensors have attributes such as shape, dtype, and device that provide information about the tensor.

tensor = torch.rand(3, 4)
print("Shape:", tensor.shape)
print("Data type:", tensor.dtype)
print("Device:", tensor.device)
Shape: torch.Size([3, 4])
Data type: torch.float32
Device: cpu

4.2.3 Operations on Tensors

Tensors support a variety of operations, including arithmetic operations, matrix multiplication, and more. These operations can be performed on CPU and accelerator such as CUDA, MPS.

On defult, tensors are created on the CPU.

tensor = torch.rand(2, 3)
print("Device:", tensor.device)
Device: cpu

Using to method, we can move tensors to the accelerator if available.

if torch.accelerator.is_available():
    tensor = tensor.to(torch.accelerator.current_accelerator())
    print("Device:", tensor.device)
Device: mps:0

4.2.3.1 Indexing and slicing

tensor = torch.ones(2, 3)
print(tensor[0])  # First row
print(tensor[:, 1])  # First column
print(tensor[:, -1])  # Last column

tensor[:, 1] = 0  # Set the second column to zero
print(tensor)
tensor([1., 1., 1.])
tensor([1., 1.])
tensor([1., 1.])
tensor([[1., 0., 1.],
        [1., 0., 1.]])

4.2.3.2 Concatenation and stacking

tensor1 = torch.ones((2, 3))
tensor2 = torch.zeros((2, 3))
concatenated_0 = torch.cat((tensor1, tensor2), dim=0)  # Concatenate along rows
print(concatenated_0)
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [0., 0., 0.],
        [0., 0., 0.]])
stacked = torch.stack((tensor1, tensor2), dim=0)  # Stack along a new dimension
print(stacked)
tensor([[[1., 1., 1.],
         [1., 1., 1.]],

        [[0., 0., 0.],
         [0., 0., 0.]]])