Lab 4 ML frameworks for python

Introduction

In Lab3 we introduced the PyEnv tool to allow us to install various versions of python either locally or globally, and we installed the Anaconda distribution of python which includes many of the libraries we will be using in the course.

Installing PyTorch

The core ML framework used in the lectures will be PyTorch, it is easy to install this within the pyenv / anaconda framework by default and as we will be using this often this is the best approach. You can find the commands for your own machine by following this link PyTorch

pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124

We can now test to see if this was successful by running python and executing the following

import torch
print(torch.__version__)
x = torch.rand(5, 3)
print(x)

Now run python and input the following which should return True

import torch
torch.cuda.is_available()

To exit the virtual environment we can use the deactivate command.

Installing Tensorflow

Tensor flow is another machine learning library that you may wish to use. As before we can either install this as a default install in the anaconda environment or to a virtual env.

python -m venv ~/TensorFlow
cd ~/TensorFlow
source bin/activate
pip install tensorflow[and-cuda]

To test we can run python and execute the following

import tensorflow as tf
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))

Which should return Num GPUs Available: 1

To leave this environment use deactivate

Previous