Caffe2 - Installation
PyTorch - Installation
Section titled “PyTorch - Installation”Now that you understand the basics of PyTorch, it’s time to install it on your system. PyTorch offers flexible installation options for various operating systems (Linux, macOS, Windows) and package managers (conda, pip). You can also choose between CPU-only or GPU-enabled (CUDA) versions.
The recommended way to install PyTorch is via the official website’s configuration tool, which generates the correct command for your specific setup.
Using the Official PyTorch Website
Section titled “Using the Official PyTorch Website”-
Go to the PyTorch official website: https://pytorch.org/
-
Locate the ‘Get Started’ or ‘Install’ section.
-
Use the interactive tool to select your preferences:
- PyTorch Build: Stable (Recommended) or Preview (Nightly).
- Your OS: Linux, Mac, or Windows.
- Package: Conda or Pip (choose based on your Python environment management).
- Language: Python.
- Compute Platform: CPU or specific CUDA versions (if you have an NVIDIA GPU and want GPU acceleration). Select the CUDA version compatible with your GPU driver.
- The tool will generate the exact command to run in your terminal. Copy and execute this command.
Example Installation Commands (Check official site for latest!)
Section titled “Example Installation Commands (Check official site for latest!)”Using Conda (Recommended if you use Anaconda/Miniconda)
Section titled “Using Conda (Recommended if you use Anaconda/Miniconda)”For CPU-only version on Linux/macOS:
conda install pytorch torchvision torchaudio cpuonly -c pytorchFor CUDA-enabled version (e.g., CUDA 11.8) on Linux/Windows:
# Make sure your NVIDIA driver supports CUDA 11.8conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidiaUsing Pip
Section titled “Using Pip”For CPU-only version on Linux/macOS/Windows:
pip install torch torchvision torchaudioFor CUDA-enabled version (e.g., CUDA 11.8) on Linux/Windows:
# Make sure your NVIDIA driver supports CUDA 11.8pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118Note: The exact pip command (especially the --index-url or --extra-index-url) might vary based on CUDA version and OS. Always refer to the official website.
Installing Additional Libraries
Section titled “Installing Additional Libraries”For the examples in this tutorial, you will likely need other common libraries. Install them using pip or conda:
# Using pippip install numpy matplotlib scikit-image requests
# Or using conda (if you installed PyTorch with conda)conda install numpy matplotlib scikit-image requests -c conda-forgeLibraries:
- numpy: Fundamental package for scientific computing.
- matplotlib: For plotting graphs and displaying images.
- scikit-image: For image loading and processing (alternatively, Pillow
pip install Pillow). - requests: For downloading files from URLs (e.g., class labels).
Testing Installation
Section titled “Testing Installation”To verify your PyTorch installation, run the following Python script:
import torchimport numpy as np
print(f"PyTorch version: {torch.__version__}")
# Test basic tensor creationx = torch.rand(3, 2)print("\nCreated a random tensor:")print(x)
# Test NumPy bridgenumpy_array = np.array([1, 2, 3])torch_tensor = torch.from_numpy(numpy_array)print("\nConverted NumPy array to tensor:")print(torch_tensor)
# Test CUDA availability (if you installed GPU version)if torch.cuda.is_available(): print(f"\nCUDA is available. GPU: {torch.cuda.get_device_name(0)}") # Example: Move tensor to GPU device = torch.device("cuda") y = torch.ones_like(x, device=device) # Create tensor on GPU x_gpu = x.to(device) # Move existing tensor to GPU z = x_gpu + y print("Performed addition on GPU:") print(z) # Move result back to CPU for printing (if needed) # print(z.to("cpu", torch.double))else: print("\nCUDA is not available. Running on CPU.")If the script runs without errors and shows your PyTorch version, the basic installation is successful. If you installed the GPU version, it should also indicate whether CUDA is available.
Now that PyTorch is installed, you can proceed to explore its features, starting with using pre-trained models.