cd /news/neural-networks/training-my-first-neural-network-on-… · home › topics › neural-networks › article
[ARTICLE · art-149016] src=dev.to ↗ pub= topic=neural-networks verified=true sentiment=· neutral

Training My First Neural Network on Windows with WSL 2 and PyTorch

A physics undergraduate documented a step-by-step workflow for training a first neural network on Windows using WSL 2, Ubuntu, a Python virtual environment, and PyTorch. The guide walks through installing Ubuntu via `wsl --install -d Ubuntu`, setting up the project in the Linux home directory, and building a small network for the XOR problem that ends with a saved model file. The author notes a GPU is unnecessary for the four-example experiment and that the focus is on getting the workflow running rather than the underlying mathematics.

by read7 min views2 publishedOct 11, 2026

As a physics undergraduate beginning to explore AI research, I wanted to understand the practical workflow behind a neural network experiment: where the code lives, how the environment works, how to run training, and what gets saved afterward.

This tutorial brings together my setup notes and first PyTorch experiment. We will build a small network for XOR, starting from a Windows computer and ending with a saved model file. The focus is on getting the workflow running; the mathematics can come later.

You need a supported Windows 10 or Windows 11 installation, permission to install WSL, and an internet connection. A GPU is unnecessary for this four-example experiment.

The workflow is Windows terminal → WSL 2 → Ubuntu → project folder → Python virtual environment → PyTorch → training results.

Tool Role in this experiment
CMD / PowerShell Launches WSL from Windows
WSL 2 Runs a Linux kernel using lightweight virtualization
Ubuntu Provides the Linux operating environment
APT Installs Ubuntu system packages
Python Executes our training script
venv Isolates the project's Python dependencies
pip Installs packages inside that environment
PyTorch Provides tensors, neural network layers, and automatic differentiation

Docker is an optional next step for packaging environments. Ordinary Ubuntu and PyTorch development works without Docker Desktop running.

Run these commands in Windows CMD or PowerShell:

wsl -l -v

On my machine, the only listed distribution was initially docker-desktop. That did not mean I already had an Ubuntu development environment.

To install Ubuntu, open PowerShell as administrator and run:

wsl --install -d Ubuntu

Restart Windows if prompted. On Ubuntu's first launch, create a Linux username and password. The password input displays no characters or asterisks.

Check the installation from Windows:

wsl -l -v

Confirm that Ubuntu is listed with VERSION 2. If it uses version 1, run:

wsl --set-version Ubuntu 2

Optionally make Ubuntu the default, then launch it:

wsl --set-default Ubuntu
wsl -d Ubuntu

For installation requirements and troubleshooting, see Microsoft's WSL installation guide.

From this point onward, commands run inside Ubuntu, unless explicitly labeled otherwise.

A terminal prompt might look like:

lawson@computer:~$

lawson is the Linux username, computer is the hostname, and ~ means the user's home directory. Do not copy the prompt itself when running commands.

Your Linux home directory is typically /home/<username>. Windows drives are accessible through paths such as /mnt/c/. For this experiment, keep the project in your Linux home directory.

cd ~
pwd
ls
Command What it does
pwd Prints the current working directory
ls Lists directory contents
cd Changes directories
mkdir Creates directories
touch Creates an empty file or updates its timestamps
cat Displays or combines file contents
sudo Runs a command as another user, usually root

APT manages Ubuntu packages. Refresh its package information and install the tools we need:

sudo apt update
sudo apt install -y git python3 python3-pip python3-venv nano

Then check:

python3 --version
git --version

APT handles system software; pip handles Python packages. In the following steps, pip installs packages into our project environment.

cd ~
mkdir -p research/first-neural-network
cd research/first-neural-network
pwd

The path should end with research/first-neural-network.

The diagram's version numbers are examples: each project can manage its own dependencies independently.

Create and activate the environment:

python3 -m venv .venv
source .venv/bin/activate

Your prompt should now begin with (.venv). Closing the terminal leaves this directory on disk; you will reactivate it in the next session.

With .venv active, install a CPU build of PyTorch and NumPy:

python -m pip install --upgrade pip
python -m pip install torch --index-url https://download.pytorch.org/whl/cpu
python -m pip install numpy

Check PyTorch's official installation selector for supported Python versions or a GPU-specific installation command.

Verify the installation:

python -c "import torch, numpy; print('PyTorch:', torch.__version__); print('NumPy:', numpy.__version__)"
python -c "import torch; print(torch.rand(2, 2))"

The second command should print a random 2 × 2 tensor. In my original setup, tensor creation worked but PyTorch warned that NumPy was missing. Installing NumPy resolved that missing dependency.

Our task is XOR: output 1 when the two inputs differ and 0 when they match.

Input Target
[0, 0] 0
[0, 1] 1
[1, 0] 1
[1, 1] 0

Open a new file:

nano train.py

Paste this code:

import torch
import torch.nn as nn

torch.manual_seed(42)

X = torch.tensor([
    [0., 0.],
    [0., 1.],
    [1., 0.],
    [1., 1.]
])
y = torch.tensor([[0.], [1.], [1.], [0.]])

model = nn.Sequential(
    nn.Linear(2, 4),
    nn.Tanh(),
    nn.Linear(4, 1)
)

criterion = nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.05)

model.train()
for epoch in range(2001):
    optimizer.zero_grad()
    logits = model(X)
    loss = criterion(logits, y)
    loss.backward()
    optimizer.step()

    if epoch % 200 == 0:
        print(f"Epoch {epoch}, Loss: {loss.item():.6f}")

model.eval()
with torch.no_grad():
    probabilities = torch.sigmoid(model(X))
    predicted_labels = (probabilities >= 0.5).int()
    print("\nProbabilities:")
    print(probabilities)
    print("\nPredicted labels:")
    print(predicted_labels)

torch.save(model.state_dict(), "xor_model.pth")
print("\nSaved weights to xor_model.pth")

Save with Ctrl + O, press Enter, and exit with Ctrl + X.

The loop computes predictions, measures the error, computes gradients, and updates parameters. BCEWithLogitsLoss takes the network's raw output, so the sigmoid is applied when inspecting probabilities afterward.

This is a small network with one hidden layer. It is a first neural network training exercise, rather than a large-scale deep learning experiment.

python train.py

One mistake I made was trying torch train.py. PyTorch is a Python library; Python executes the file.

You should see periodic loss reports, followed by probabilities and labels. Check that the loss trends downward and the predicted labels are:

tensor([[0],
        [1],
        [1],
        [0]], dtype=torch.int32)

Exact probabilities and loss values can vary with software versions. These predictions use the same four examples used for training, so they check that the network learned the XOR truth table; they do not measure generalization to a separate dataset.

Check the files:

ls -lh train.py xor_model.pth

xor_model.pth contains the model's parameter state dictionary. It does not contain the training script or a complete experiment checkpoint. To restore these weights later, recreate the same architecture and load the state dictionary; see PyTorch's saving and tutorial.

In Windows CMD or PowerShell:

wsl -d Ubuntu

Inside Ubuntu:

cd ~/research/first-neural-network
source .venv/bin/activate
python train.py

This runs training again from newly initialized weights. It does not resume the previously saved model automatically.

To leave the environment, run deactivate. To leave Ubuntu, run exit. An active foreground process may terminate if its terminal closes, but files already saved remain on disk.

Problem Fix
torch: command not found Run python train.py
No module named 'torch' Activate .venv , then install PyTorch with that environment's Python
No module named 'numpy' Run python -m pip install numpy in.venv
train.py prints nothing Check that the file contains and saves the code
wsl -l -v fails inside Ubuntu Run it from Windows, or use wsl.exe -l -v inside Ubuntu
Docker Desktop is stopped Docker is optional for this experiment
The terminal was closed Reopen Ubuntu, return to the folder, and reactivate .venv

This diagram from my notes shows a possible later setup with Docker and GPU acceleration. Those components are optional extensions beyond the CPU workflow used here.

For larger experiments, I want to connect local development and version control with remote compute, then keep model weights, metrics, figures, and logs together. Access to university computing platforms depends on their own eligibility and allocation rules.

The useful milestone here is modest but concrete: a project directory, isolated dependencies, an executable training script, inspectable predictions, and saved parameters.

My notes and projects: GitHub · Personal website

── more in #neural-networks 4 stories · sorted by recency
── more on @pytorch 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/training-my-first-ne…] indexed:0 read:7min 2026-10-11 · —