I have started reading “Build a large language model from scratch”. Reading through the beginning of the book the author recommends going straight to the Appendix A if you are new to tensors.

Well I am. So I started there.

First step is installing Pytorch. I am running Ubuntu as my desktop environment and it was quite a long time ago since I last time started a new Python project. My initial idea was to use Poetry as I have had good experience with that for Python projects in the past. But I could not get it to install Pytorch as I kept running into dependency version issues. After fighting it for a while I decided to “go back” to the “old” strategy of just using pip and venv.

So this is what I found out and the steps I used to create a Python project with a virtual environment and installing pytorch inside it and finally using Visual Studio Code to write a few lines of code to verify that the installation of pytorch was successful.

First to create the directory where we will house our project use the following command assuming that you are already inside the folder where you want to place the top folder of the project. I have a “Projects” folder with some sub-directories depending on source control (Git vs. BitBucket for instance) so that is where I started from.

mkdir -p /pytorch-python/

cd ./pytorch-python/

Then we create the virtual environment in order to contain the dependencies among other things.

python3 -m venv .venv

Then to activate/use this environment we use this command:

source .venv/bin/activate

You will be able to see this in the command window:

(.venv) user@computer:~/Projects/python-pytorch$

When it starts with “(.venv)” then it is activated. This is important when we use pip to install dependencies, as this will ensure that they are added/installed inside the virtual environment and not globally.

To install pytorch we use this command:

pip install torch

This will download a huge amount of data.

I found out that in order to use pytorch I also needed to install numpy which is done by using this command:

pip install numpy

Following some sort of structure I created a src folder inside my project folder to hold the source files:

mkdir -p /src

Then I changed to that directory and created a init.py file:

cd ./src

touch __init__.py

By this point I switched to Visual Studio Code.

The first thing I did was using Ctrl+Shift+P to bring up the command palette and typed in Python. Then I looked for the “Select Interpreter” command. Visual Studio being somewhat intelligent had the Python interpreter inside the venv folder as recommended - so I selected that one.

Then I wrote two lines of code to test if Pytorch is installed and working correctly:

import torch

print(torch.__version__)

If pytorch is properly installed then it should print the version number - in my case 2.9.0.

I used F5 in Visual Studio Code and it started by asking if I wanted to debug the local file - which I chose to.

Then it printed the correct version number and I was satisfied and ready to actually start learning about tensors.

Maybe I will write about that at a later point.

Now at least I have notes for myself so I can remember how I started.

Writing this I realized that I had not written on this blog for several months and I had completely forgotten how to run it locally. Usually I create a ProjectNotes.txt in the root of a project to remind me about things like that. But in this case I had forgot. So while writing this I also spend like a good hour of work figuring out how to run the site on my local machine. I did find instructions on how to deploy it, so hopefully that will be a more smooth experience.

If you read this, then I will have succeeded.