In my previous note, I set up Ubuntu with WSL 2, created a Python virtual environment, trained a small XOR neural network with PyTorch, and saved its parameters.
This time, I wanted to make that experiment easier to manage: organize the model files, save the training output, and understand what actually changes when I activate a virtual environment.
This is a hands-on continuation using the same project, ~/research/first-neural-network. You will need the existing train.py, .venv, and xor_model.pth from the previous tutorial.
1. Open Ubuntu and return to the project
Start a new Windows Command Prompt. It may initially look like this:
Enter:
wsl -d Ubuntu
The prompt now belongs to Ubuntu:
Notice that WSL may initially open in a mounted Windows directory, such as /mnt/c/Users/linxi. Navigate to the Linux project explicitly:
cd ~/research/first-neural-network
pwd
ls -la
-
cdchanges the current working directory. -
pwdprints its absolute path. -
ls -lalists the directory contents in detail, including hidden entries such as.venv.
2. Organize the model files
I wanted to separate the Python environment, model parameters, and training logs:
Create two directories and copy the existing model file:
mkdir -p checkpoints logs # Create both directories if they do not exist
cp xor_model.pth checkpoints/ # Copy the saved parameters into checkpoints/
ls checkpoints # List the files in checkpoints/
Seeing xor_model.pth in the output confirms that the file is present in checkpoints/. In this project, it stores the trained model's parameters, including weights and biases, saved by PyTorch. The .pth extension alone does not tell us what every PyTorch file contains; that depends on what the training script saved.
Because I used cp, the original file is still in the project root:
These are two separate files. If train.py saves a new xor_model.pth in the project root later, the copy in checkpoints/ will not update automatically. For now, that copy is a snapshot of the earlier run.
Inspect the file sizes
ls -lh xor_model.pth checkpoints/xor_model.pth
You can write -lh as -l -h:
-
-luses the long listing format, showing permissions, owner, size, modification time, and filename. -
-hdisplays sizes in human-readable units, such as K, M, and G.
The size is one field in a longer line of file information:
Here are the two files side by side:
Matching displayed sizes are a useful quick check, although they do not prove that two files have identical contents.
3. Activate the Python environment
Before running the training script, activate the project's existing environment:
source .venv/bin/activate
source executes the activation script in the current shell. Activation puts .venv/bin at the beginning of the shell's PATH, so commands such as python and pip use the project's environment.
Install NumPy if the project needs it
NumPy is a Python package, not a separate environment. To install it into this virtual environment and check its version:
python -m pip install numpy
python -c "import numpy; print(numpy.__version__)"
The version attribute is __version__, with two underscores on each side. Using python -m pip runs pip with the same Python interpreter selected by python.
Installing NumPy may resolve a missing-NumPy warning from PyTorch, but NumPy is not required to read a text log. We will use Linux commands for that.
4. Save the training output to a log
Run the training script and capture its output:
python train.py > logs/training.log 2>&1
There are two main output streams:
-
Standard output (
stdout, descriptor1): ordinary output, such as printed epoch numbers and losses. -
Standard error (
stderr, descriptor2): errors, warnings, and other diagnostic output.
> logs/training.log sends standard output to the file. Then 2>&1 sends standard error to the same destination as standard output. The order matters: redirect standard output first, then make standard error follow it.
The terminal may appear quiet while training runs because the output is going into the file. The command returns to the shell prompt when the script finishes.
Each run with > overwrites the existing log. To append instead, use:
python train.py >> logs/training.log 2>&1
A log records what the program writes; it does not automatically save the trained model. Model saving still depends on the code in train.py.
5. Read and monitor the training log
After training finishes, display the log:
cat logs/training.log
cat is short for concatenate. It can combine files, and when given a single file, it prints that file's contents directly in the terminal.
For longer logs, these commands are useful:
head logs/training.log # Show the first 10 lines
tail -n 5 logs/training.log # Show the last 5 lines
less logs/training.log # Browse the file interactively; press q to quit
Here is a screenshot from my session, showing a NumPy installation followed by the training log:
Installing a package after a run does not change a warning already recorded in that run's log. To capture a new run with the updated environment, run the training command again.
Follow a longer training run
For a long experiment, use a second terminal to watch new log lines arrive.
In the first terminal, start training with unbuffered Python output:
cd ~/research/first-neural-network
source .venv/bin/activate
python -u train.py > logs/training.log 2>&1
-u makes Python's output unbuffered, which helps printed progress appear promptly in a redirected log.
In a second Windows Command Prompt, enter Ubuntu and follow the file:
wsl -d Ubuntu
cd ~/research/first-neural-network
tail -f logs/training.log
tail -f displays the last 10 lines and continues showing newly appended content. Press Ctrl+C in the monitoring terminal to stop following the log; training in the first terminal continues.
6. Understand the virtual environment and the file system
A Python virtual environment gives a project its own installed packages and access to a Python interpreter through the environment's executables. In this project, .venv is simply a directory inside first-neural-network.
The important paths are:
| Path | Purpose |
|---|---|
.venv/bin/python |
Python interpreter executable, which may be a copy or symbolic link |
.venv/bin/pip |
Python package installer entry point |
.venv/bin/activate |
Shell script used to activate the environment |
.venv/lib/pythonX.Y/site-packages/ |
Installed Python packages; X.Y depends on the Python version |
train.py |
Neural network training script |
xor_model.pth |
Model parameter file originally saved in the project root |
checkpoints/xor_model.pth |
Separate copy of that parameter file |
logs/training.log |
Recorded training output |
Activating .venv does not change your working directory or prevent Python and the shell from accessing the rest of the project. It is not a separate Linux file system or a file-access sandbox.
Experiment: check what activation changes
Start with a fresh Ubuntu shell, or run deactivate first if the environment is already active. Then execute this block:
# Step 1: Navigate to the project directory
cd ~/research/first-neural-network
# Step 2: Check the current directory and Python executable
pwd
which python3
# Step 3: Activate the virtual environment
source .venv/bin/activate
# Step 4: Check the directory, Python executable, and model file again
pwd
which python
ls xor_model.pth
Observations:
-
pwdprints the same path before and after activation, showing that the current working directory has not changed. -
which pythonnow points to the executable inside the project's.venv/bin/directory. -
ls xor_model.pthstill finds the model file in the project root.
Conclusion: Activating a Python virtual environment changes which Python executable and project packages are used. It does not create a separate Linux file system or change the current working directory.
To exit the environment:
deactivate
The project files and saved logs remain where they were.













Top comments (1)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.