This article provides a detailed step-by-step guide on installing PyTorch on Ubuntu, covering both CPU and GPU setups. Learn how to set up your environment, install required dependencies, and run your first PyTorch script, enabling you to harness the power of deep learning for your projects.
9 min
Edited:06-10-2024
PyTorch is an open-source machine learning library widely used for deep learning applications. This guide will walk you through the process of installing PyTorch on Ubuntu and provide an overview of how to use it for your machine learning projects.
we will need Python installed (3.6 or later is recommended), if you did not install it you can follow do that like this:
1. sudo apt update
2. sudo apt install python3 python3-pip
The installation of PyTorch may vary depending on whether you want to use it with a CPU or GPU. The official PyTorch website provides a straightforward installation command generator based on your preferences.
1- Visit the PyTorch website: PyTorch Get Started.
2- Select your preferences and choose your desired configuration:
PyTorch Build: Stable (or Preview if you want the latest features)
Your OS: Linux
Package: Conda (or Pip if you prefer)
Language: Python
Compute Platform: Select CPU or CUDA (if you have an NVIDIA GPU)
3- Copy the installation command provided by the website. It will look something like this:
For CPU:
1. pip3 install torch torchvision torchaudio
For CUDA (GPU):
1. pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
To verify that PyTorch has been installed correctly, you can run the following command in a Python shell:
1. python3
Then, within the Python shell, type:
1. import torch
2. print(torch.__version__)
If you see the version number of PyTorch printed without any errors, your installation was successful!
Now that you have PyTorch installed, let’s create a simple script to test its functionality.
1- Create a new Python file (e.g., test_pytorch.py):
1. nano test_pytorch.py
2- Add the following code to the file:
1. console.log('Hello world !')
1. import torch
2.
3. # Check if GPU is available and use it if possible
4. device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
5.
6. # Create a tensor
7. x = torch.rand(5, 3).to(device)-+ print("Tensor x:")
8. print(x)
9.
10. # Perform a simple operation
11. y = torch.rand(5, 3).to(device)
12. z = x + y
13. print("Result of x + y:")
14. print(z)
3- Run your script:
1. python3 test_pytorch.py
You should see output similar to the following, confirming that PyTorch is working:
1. Tensor x:
2. tensor([[0.1234, 0.5678, 0.9101],
3. [0.2345, 0.6789, 0.0123],
4. [0.3456, 0.7890, 0.1234],
5. [0.4567, 0.8901, 0.2345],
6. [0.5678, 0.9012, 0.3456]])
7. Result of x + y:
8. tensor([[0.5678, 1.2345, 1.0102], [1.2345, 1.5678, 0.3456],
9. ...
10.
You have successfully installed PyTorch on your Ubuntu system and ran a simple script to verify its functionality. PyTorch is a powerful library that enables you to build and train deep learning models efficiently. You can now explore more advanced features and start developing your machine learning projects using PyTorch!
14-10-2024
This article offers a detailed guide on installing and configuring IPTables on an Ubuntu VPS. IPTables is a powerful firewall tool that helps secure your server by controlling inbound and outbound traffic. Learn how to set up rules for traffic filtering, configure basic security policies, and apply custom rules to protect your VPS.
IPtables
security
12 min
This article offers a comprehensive guide on installing and configuring ModSecurity, a powerful web application firewall (WAF), on an Ubuntu VPS. Learn how to secure your server by filtering and monitoring HTTP requests, set up ModSecurity with Nginx or Apache, and apply rules to protect against common web attacks.
Modsecurity
security
10 min
14-10-2024
This article provides a comprehensive guide on installing and configuring PHP-FPM (FastCGI Process Manager) on an Ubuntu VPS. Learn how to optimize PHP performance for your web applications by configuring PHP-FPM with Nginx or Apache, managing pools, and fine-tuning settings for efficient processing of PHP scripts.
PHP-FPM
speed
optimise
12 min