PyTorch Installation Guide

in

To set the stage, let’s start with the basics. What is PyTorch? Well, it’s a popular open-source machine learning library for Python that allows you to build and train deep neural networks quickly and easily. It has gained popularity due to its flexibility, ease of use, and support for both CPU and GPU computing.

Now, the installation process. Here are some steps that will help you get started with PyTorch:

Step 1: Make sure your computer meets the requirements. To install PyTorch, you need to have Python installed on your machine (version 3.6 or higher). You also need a C++ compiler and a compatible version of NumPy (version 1.15 or higher) and SciPy (version 0.19 or higher).

Step 2: Download the PyTorch package from their official website (https://pytorch.org/get-started/locally/) and follow the instructions to install it on your machine. If you’re using a Linux distribution, you can use pip to install PyTorch by running this command in your terminal:

# This script installs PyTorch version 1.8.0 and torchaudio version 0.8.0 on a machine using pip3.

# The first command uses pip3 to install torch version 1.8.0 and torchaudio version 0.8.0.
# The -c flag specifies the channel from which to download the packages, in this case, the official PyTorch website.
# The cu111 at the end of the command specifies the CUDA version to use, in this case, CUDA 11.1.
# The === symbol is used to specify the exact version of torchaudio to install.

# To ensure compatibility, it is recommended to use the same version of PyTorch and torchaudio.

# To install PyTorch, follow the instructions on the official PyTorch website (https://pytorch.org/get-started/locally/).
# If using a Linux distribution, pip can be used to install PyTorch by running the following command in the terminal:

pip3 install torch==1.8.0+cu111 torchaudio===0.8.0 -c https://download.pytorch.org/whl/cu111

Step 3: Once PyTorch is installed, you can test it by running a simple example code in your Python environment (you can use Jupyter Notebook or IDLE). Here’s an example code that creates a basic neural network and trains it on the MNIST dataset:



# Import necessary libraries
import torch # Importing PyTorch library
from torch.nn import Linear, ReLU, CrossEntropyLoss # Importing necessary modules from PyTorch
from torch.utils.data import Dataset, DataLoader # Importing necessary modules from PyTorch
import numpy as np # Importing numpy library for data manipulation

# Define a custom dataset class
class MyDataset(Dataset):
    def __init__(self):
        self.X = np.load('mnist_train.npy') # Loading the MNIST training data
        self.Y = np.load('mnist_labels.npy') # Loading the MNIST training labels
        
    def __len__(self):
        return len(self.X) # Returning the length of the dataset
    
    def __getitem__(self, index):
        X = torch.from_numpy(np.expand_dims(self.X[index], axis=0)) # Converting the input data to a PyTorch tensor
        Y = self.Y[index] # Getting the corresponding label for the input data
        return (X, Y) # Returning the input data and label as a tuple
        
# Define a custom model class
class MyModel(torch.nn.Module):
    def __init__(self):
        super().__init__() # Calling the parent class constructor
        self.fc1 = Linear(784, 500) # Defining the first fully connected layer with 784 input features and 500 output features
        self.relu1 = ReLU() # Defining the ReLU activation function
        self.fc2 = Linear(500, 10) # Defining the second fully connected layer with 500 input features and 10 output features
        
    def forward(self, x):
        x = self.fc1(x) # Passing the input data through the first fully connected layer
        x = self.relu1(x) # Applying ReLU activation function
        x = self.fc2(x) # Passing the output of the first layer through the second fully connected layer
        return x # Returning the output of the model
    
model = MyModel() # Creating an instance of the custom model
criterion = CrossEntropyLoss() # Defining the loss function
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9) # Defining the optimizer with a learning rate of 0.01 and momentum of 0.9
train_loader = DataLoader(MyDataset(), batch_size=64, shuffle=True) # Creating a data loader for the training data with a batch size of 64 and shuffling the data
test_loader = DataLoader(MyDataset(), batch_size=1000, shuffle=False) # Creating a data loader for the test data with a batch size of 1000 and not shuffling the data
epochs = 5 # Defining the number of epochs for training

# Training the model
for epoch in range(epochs):
    for i, (X, y) in enumerate(train_loader):
        X = X.view(-1, 784).float() / 255.0 # Reshaping and normalizing the input data
        y = torch.tensor(y).long().to('cpu') # Converting the labels to a PyTorch tensor and moving it to the CPU
        
        output = model(X) # Getting the output of the model
        loss = criterion(output, y) # Calculating the loss
        
        optimizer.zero_grad() # Resetting the gradients
        loss.backward() # Backpropagating the loss
        optimizer.step() # Updating the model parameters
    
    # Evaluating the model on test data after each epoch
    correct = 0 # Initializing a variable to keep track of the number of correct predictions
    total = 0 # Initializing a variable to keep track of the total number of predictions
    for X, y in test_loader:
        X = X.view(-1, 784).float() / 255.0 # Reshaping and normalizing the input data
        output = model(X) # Getting the output of the model
        
        _, predicted = torch.max(output.data, dim=1) # Getting the index of the maximum value in the output
        total += len(y) # Incrementing the total number of predictions
        correct += (predicted == y).sum().item() # Incrementing the number of correct predictions
    
    print('Epoch {}/{}: Loss {:.4f}, Accuracy {:.2f}%'.format(epoch+1, epochs, loss.item(), 100*correct/total)) # Printing the loss and accuracy after each epoch

Step 4: Run the code and wait for it to finish (this may take a few minutes depending on your machine’s performance). If everything goes well, you should see some output that looks like this:

# This script is used to run a code and display its output, which includes the loss and accuracy for each epoch.

# The first line is a shebang, which specifies the interpreter to be used for the script.
#!/bin/bash

# The next line is a comment, which explains the purpose of the script.
# This script is used to run a code and display its output, which includes the loss and accuracy for each epoch.

# The following line is a variable declaration, which sets the number of epochs to 5.
epochs=5

# The next line is a for loop, which will iterate 5 times (the number of epochs).
for ((i=1; i<=epochs; i++))
do
  # The following line is a command, which prints the current epoch number.
  echo "Epoch $i/$epochs:"

  # The next line is a command, which runs the code and redirects its output to a temporary file.
  # The ">>" operator appends the output to the file instead of overwriting it.
  # The "2>&1" redirects the error output to the same file as the standard output.
  # The "&&" operator ensures that the next command is only executed if the previous one was successful.
  python code.py >> temp.txt 2>&1 &&

  # The following line is a command, which extracts the loss and accuracy from the temporary file and displays them.
  # The "grep" command searches for a specific pattern in a file.
  # The "awk" command is used to extract specific columns from the output.
  # The "tail" command is used to get the last line of the output.
  # The "cut" command is used to extract specific columns from the output.
  # The "echo" command is used to display the extracted values.
  echo "Loss $(grep "Loss" temp.txt | awk '{print $2}')" ", Accuracy $(grep "Accuracy" temp.txt | tail -1 | cut -d " " -f 2)%"

  # The next line is a command, which removes the temporary file.
  rm temp.txt
done

# The last line is a comment, which explains the end of the script.
# End of script.

Congratulations! You’ve successfully installed PyTorch and trained a basic neural network on the MNIST dataset using your newfound knowledge. Now go out there and create some amazing AI models with PyTorch!

SICORPS