---
title: "How to Install PyTorch on Window"
description: "Embark on this journey and unleash PyTorch’s potential — your gateway to machine learning and AI exploration, through this ATA Learning tutorial!"
canonical: "https://adamtheautomator.com/pytorch/"
---

# How to Install PyTorch on Window

> Embark on this journey and unleash PyTorch’s potential — your gateway to machine learning and AI exploration, through this ATA Learning tutorial!

Source: https://adamtheautomator.com/pytorch/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Install PyTorch on Window](https://adamtheautomator.com/wp-content/uploads/2023/11/pytorch.jpg)

# How to Install PyTorch on Window

[![](https://secure.gravatar.com/avatar/d05ab46ae71ee2393867f96cfec69e3a779ade5f81d9a519fc4d4fab1b177298?s=192&d=mm&r=g)Goodness Chris-Ugari](https://adamtheautomator.com/author/goodness-chris-ugari/)24 November 20236 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Python](/tag/python/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Installing PyTorch via pip](#installing-pytorch-via-pip)
*   [Installing PyTorch via Anaconda](#installing-pytorch-via-anaconda)
*   [Building a Neural Network Sample with PyTorch](#building-a-neural-network-sample-with-pytorch)
*   [Uninstalling PyTorch](#uninstalling-pytorch)
*   [Conclusion](#conclusion)

Learning new skills can be daunting; that’s a universal truth. But if you’re on a quest for something transformative, like stepping into the world of machine learning, PyTorch is the answer.

In this hands-on tutorial, you’ll navigate the PyTorch landscape to bring your machine-learning dreams to life.

Embrace the challenge as the portal to knowledge and innovation with PyTorch!

## Prerequisites

Before you begin the installation process, ensure your Windows system meets the following requirements:

*   [Python](https://www.python.org/downloads/windows/) installed – This tutorial uses Python 3.12.

Related:[Python 3.6 Installation Guide for Windows, macOS, and Linux](https://adamtheautomator.com/install-python-36/)

*   pip (installed by default with recent Python versions) or [Anaconda](https://www.anaconda.com/) installed – This tutorial uses pip 23.3.1 and Anaconda 23.7.4
*   CUDA drivers and [toolkit](https://developer.nvidia.com/cuda-downloads) installed if your Windows system or server is GPU-supported.

## Installing PyTorch via pip

PyTorch is your ultimate tool for diving into research and development, and the cool part? There’s no one-size-fits-all for installing it on your Windows system.

You’ll walk through two methods of getting PyTorch on board—but first up, the trusty pip via a command-line interface, like PowerShell.

To install PyTorch via pip, carry out the following:

1\. Open PowerShell as administrator, and execute the commands below to verify you have Python and pip installed.

```powershell
python --version
pip --version
```

![Verifying that Python and pip are installed](https://adamtheautomator.com/wp-content/uploads/2023/11/image-171.png)

Verifying that Python and pip are installed

Related:[Run PowerShell as Administrator: Multiple Methods Explored](https://adamtheautomator.com/powershell-run-as-administrator/)

2\. Next, run the below commands to create a project folder (`mkdir`) called _`pytorch-env`_ (arbitrary) and navigate to that folder.

```powershell
mkdir pytorch-env
cd pytorch-env
```

![Creating a project folder](https://adamtheautomator.com/wp-content/uploads/2023/11/image-170.png)

Creating a project folder

3\. Run each command below to create (`venv`) inside the project folder and `activate` a virtual environment called `pytorch-env`.

With a virtual environment, you isolate your PyTorch installation and its dependencies from other Python projects and the global Python environment. As a result, you avoid conflicts and compatibility issues.

```powershell
python -m venv pytorch-env
pytorch-env\Scripts\activate
```

Once activated, you’ll see the prompt changes to your virtual environment, as shown below.

![Creating and activating a virtual environment](https://adamtheautomator.com/wp-content/uploads/2023/11/image-169.png)

Creating and activating a virtual environment

4\. Now, execute either `pip3` commands below to install PyTorch on your Windows system.

PyTorch offers options for both CPU and GPU. If your system lacks GPU support, you can install PyTorch with CPU support only. But this tutorial focuses on CPU support only.

```powershell
# For Windows with CPU support only
pip3 install torch torchvision torchaudio
# For Windows with GPU support
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
```

![Installing PyTorch via Pip](https://adamtheautomator.com/wp-content/uploads/2023/11/image-168.png)

Installing PyTorch via Pip

5\. Lastly, run the below `python` command to verify the PyTorch version installed.

```powershell
python -c "import torch; print(torch.__version__)"
```

![Verifying PyTorch installation](https://adamtheautomator.com/wp-content/uploads/2023/11/image-167.png)

Verifying PyTorch installation

## Installing PyTorch via Anaconda

Installing PyTorch using pip is comparable to taking the well-worn path of a familiar and reliable main street. But you may want to opt for one that uniquely simplifies the landscape — Anaconda.

Like an ecosystem where data science tools and libraries coexist seamlessly, Anaconda is designed to simplify package and environment management.

To install PyTorch via Anaconda, follow these steps:

1\. Launch **Anaconda Prompt** from the Windows start menu, as shown below.

Anaconda’s prompt is a specialized command line for orchestrating environments, adding a personal touch to your navigation.

![Launching the Anaconda Prompt](https://adamtheautomator.com/wp-content/uploads/2023/11/image-176.png)

Launching the Anaconda Prompt

2\. Next, execute the following `conda` command in the prompt to [`create`](https://docs.conda.io/projects/conda/en/latest/commands/create.html) a virtual environment called `pytorch-env` (arbitrary)

```bash
conda create --name pytorch-env
```

![Creating a virtual environment via Anaconda Prompt](https://adamtheautomator.com/wp-content/uploads/2023/11/image-175.png)

Creating a virtual environment via Anaconda Prompt

3\. Once created, run the below command to `activate` your virtual environment (`pytorch-env`)

```bash
conda activate pytorch-env
```

![Activating the newly created virtual environment](https://adamtheautomator.com/wp-content/uploads/2023/11/image-174.png)

Activating the newly created virtual environment

4\. Subsequently, run either of the following commands to `install` `pytorch`.

```bash
# For Windows with CPU support only
conda install pytorch torchvision torchaudio cpuonly -c pytorch
# For Windows with GPU support (CUDA)
# Replace versions and the channel (nvidia) as needed
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
```

![Installing PyTorch via Anaconda](https://adamtheautomator.com/wp-content/uploads/2023/11/image-173.png)

Installing PyTorch via Anaconda

5\. Now, run the below command to verify your environment’s installed version of PyTorch.

```bash
python -c "import torch; print(torch.__version__)"
```

![Verifying the installed PyTorch version](https://adamtheautomator.com/wp-content/uploads/2023/11/image-172.png)

Verifying the installed PyTorch version

## **Building a Neural Network Sample with PyTorch**

With PyTorch installed, you now have a powerful deep-learning library. But how does PyTorch work exactly? Imagine crafting a digital brain that learns patterns, makes predictions, and evolves through data — diving into the heart of artificial intelligence.

To see how PyTorch works, you’ll build a neural network as follows:

1\. Create a Python script called `basic_demo.py` (arbitrary) in your preferred editor, like Visual Studio Code ([VSCode](https://adamtheautomator.com/visual-studio-code-tutorial/)).

Related:[The Visual Studio Code Tutorial Worth Learning](https://adamtheautomator.com/visual-studio-code-tutorial/)

2\. Add the following import statements to import PyTorch core functionalities and the necessary modules in your script.

```python
# Import PyTorch core functionalities
import torch
# Import PyTorch module (nn) for neural network components
import torch.nn as nn
# Import PyTorch module (optim) for optimization algorithms
import torch.optim as optim
```

3\. Next, populate the following tensors to define your datasets, which are examples your neural network can learn from. The goal is to learn a mapping from inputs (**`x`**) to outputs (**`y`**).

```python
# Represents the input values
x = torch.tensor([[1], [2], [3], [4]], dtype=torch.float32)
# Represents the corresponding output values
y = torch.tensor([[2], [4], [6], [8]], dtype=torch.float32)
```

4\. Now, paste the line below to the file, which defines the `model` as a [`Linear`](https://realpython.com/linear-regression-in-python/) regression model using PyTorch.

```python
model = nn.Linear(1, 1)
```

5\. After defining the model, add the following to train your model, such as teaching the network to recognize the pattern in your data.

How the model works is that you give it examples, then it makes predictions, and you tell it how wrong it is. Based on the result, the model adjusts itself to be better.

```python
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
epochs = 1000

for epoch in range(epochs):
    predictions = model(x)
    loss = criterion(predictions, y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    if (epoch + 1) % 100 == 0:
        print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}')
```

6\. Add the last part of the code for testing the model, and save the file.

In this code, with provided input values (`x` values), the model predicts what the `y` values should be and prints the result.

```python
test_inputs = torch.tensor([[5], [6], [7], [8], [9], [10]], dtype=torch.float32)
predicted_outputs = model(test_inputs)

for i in range(len(test_inputs)):
    print(f'Prediction for input {test_inputs[i].item()}: {predicted_outputs[i].item()}')
```

Following the previous steps should give you the complete code below in your _basic\_demo.py_ file.

```python
# Import PyTorch core functionalities
import torch
# Import PyTorch module (nn) for neural network components
import torch.nn as nn
# Import PyTorch module (optim) for optimization algorithms
import torch.optim as optim

# Define dataset
x = torch.tensor([[1], [2], [3], [4]], dtype=torch.float32)
y = torch.tensor([[2], [4], [6], [8]], dtype=torch.float32)

# Define the model
model = nn.Linear(1, 1)

# Train the model
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

epochs = 1000

for epoch in range(epochs):
    predictions = model(x)
    loss = criterion(predictions, y)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    if (epoch + 1) % 100 == 0:
        print(f'Epoch [{epoch+1}/{epochs}], Loss: {loss.item():.4f}')

# Test the model
test_inputs = torch.tensor([[5], [6], [7], [8], [9], [10]], dtype=torch.float32)
predicted_outputs = model(test_inputs)

# Print predictions for each test input
for i in range(len(test_inputs)):
    print(f'Prediction for input {test_inputs[i].item()}: {predicted_outputs[i].item()}')
```

7\. Lastly, change the directory to where your script is located, then run the `python` command below to execute your script (`basic_demo.py`).

```bash
python basic_demo.py
```

Assuming everything works, you’ll see the predictions for your test inputs, as shown below.

![Testing the neural network](https://adamtheautomator.com/wp-content/uploads/2023/11/image-177.png)

Testing the neural network

## Uninstalling PyTorch

While PyTorch is a powerful library for machine learning and deep learning tasks, there might be instances where you need to uninstall it from your system. Uninstalling PyTorch can be necessary to switch to a different version, free up space, or resolve compatibility issues.

Execute either of the following commands, depending on your environment, to uninstall PyTorch while automatically accepting prompts (`-y`).

```bash
# Uninstall PyTorch via pip
pip3 uninstall torch -y
# Uninstall PyTorch via Anaconda
conda uninstall pytorch -y
```

![Uninstalling PyTorch via pip](https://adamtheautomator.com/wp-content/uploads/2023/11/image-180.png)

Uninstalling PyTorch via pip

![Uninstalling PyTorch via Anaconda](https://adamtheautomator.com/wp-content/uploads/2023/11/image-179.png)

Uninstalling PyTorch via Anaconda

Finally, run the commands below to launch the Python interpreter (`python3`) and attempt to `import` PyTorch (`torch`) into your Python environment.

```bash
python3
import torch
```

If PyTorch is uninstalled, you’ll see an import error, as shown below.

![Verifying PyTorch is uninstalled](https://adamtheautomator.com/wp-content/uploads/2023/11/image-178.png)

Verifying PyTorch is uninstalled

## Conclusion

Throughout this tutorial, you’ve equipped yourself with the fundamental tools to dive into the captivating realm of deep learning. You’ve learned how to set up PyTorch and ventured into constructing a neural network, training it on data, and witnessing its predictive prowess.

From importing core functionalities to making predictions, you’ve traversed the essential steps in the PyTorch landscape. Now, you’re at the gateway to endless possibilities, whether exploring advanced model architectures or tackling real-world datasets. The same exciting potential stands in contributing to the vibrant community shaping the future of artificial intelligence.

Where to from here? Why not explore [transfer learning from pre-trained models](https://towardsdatascience.com/transfer-learning-from-pre-trained-models-f2393f124751) and learn how to adapt them to new tasks? Or discover PyTorch Lightning to simplify the training process and enable scalable machine learning projects?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fpytorch%2F&text=How%20to%20Install%20PyTorch%20on%20Window)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fpytorch%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fpytorch%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/02/featured_image-3.webp)

### [How to Ace the Modern Coding Interview as a SysAdmin](/ace-modern-coding-interview-sysadmin/)

Master coding interviews for sysadmin and DevOps roles with practical preparation strategies. Learn Python, Bash, and platform-specific techniques that translate your operational experience into interview success.

![](https://adamtheautomator.com/wp-content/uploads/2023/08/install-python-macos.jpg)

### [Get Started with Programming and Install Python on macOS](/install-python-on-macos/)

Learn how to get started with programming and automation and install Python on macOS in this ATA Learning tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2022/08/Amazing-Data-Visualization-With-Power-BI-Python.jpg)

### [Amazing Data Visualization With Power BI Python](/power-bi-python/)

Learn how to effectively use Power BI Python data visualizations to enhance your IT systems administration in this ATA Learning tutorial!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
