PyTorch Introductory Experiments
by Trevor Lee in Design > Software
28 Views, 1 Favorites, 0 Comments
PyTorch Introductory Experiments

In this project, I will revisit some of my previous basic TensorFlow Deep Learning experiments / training exercises
- Trying Out TensorFlow Lite Hello World Model With ESP32 and DumbDisplay
- Mnist Dataset -- From Training to Running With ESP32 / ESP32S3
- Sliding Puzzle 'Next Move' Suggesting Simple DL Model With ESP32 TensorFlow Lite
Nevertheless this time, those experiments will be retried (reimplemented)
- The experiments will be using PyTorch as the deep learning framework (and still be with "dense" layers only)
- They will not be targeted for microcontroller; but still be demonstrable, with the help of DumbDisplay using regular Python and PyTorch
Installation With VSCode
This project is developed with VSCode (with Python extension); here, I will assume VSCode development environment as well.
To try along, please clone this project -- PyTorchIntroductoryExperiments -- from GitHub to your local machine, and open the folder with VSCode.
Create a virtual environment by selecting the command Python: Select Interpreter from the command palette and choose to create a new virtual environment.
In the process, you will be given an option to also install the required dependent packages specified in requirements.txt file. In case you missed installing those dependent package, you still can install them, including MicroPython DumbDisplay Library, with an opened terminal (virtual environment activated) by running pip like
I have tested the Python code of the project to work with Python 3.12.8
For older version of Python, when installing the packages, you might see error like
try upgrade pip like
then run
again.
When open the Jupyter Notebook, the needed components will be installed when necessary.
"Hello World" Deep Learning Model Training


The "Hello World" of Deep Learning here refers to the training of a DL model for the sine Mathematical function -- train_sine.ipynb.
I guess modeling the Mathematical function sine is considered the "Hello World" of Deep Learning because
- The training data is very easy to generate.
- The architecture of the model can simply be just a few "dense" layers.
- When it comes to implementation, the Mathematical function sine might not be as trival as it sounds, and therefore a good demonstration of the Deep Learning "magic".
Moreover, just for fun, I extended the architecture of the sine model to output cosine values at the same time -- train_sine_cosine.ipynb.
Highlights of "Hello World" DL Training
The target of this "Hello World" model is the sine Mathematic function for the input range from 0 to 2π (0° to 360°).
Hence, to create the data for the training the model, can generate randomized data like
Since the default datatype for PyTorch is float32, first convert the data to float32, and reshape them as
Notes:
- x_values is a two-dimensional matrix, with a single column, as the input to the model is a single value (the input angle in radian)
- y_values is also a two-dimensional matrix, with a single column, as the output of the model is also a single value (the sine value of the input angle)
Then, 70% of the data will be treated as "train" data set, and the rest is treated as "test" data set
... please refer to Highlights of train_sine.ipynb and Highlights of train_sine_cosine.ipynb for more
Mnist Dataset DL Training and Demo App

The popular Mnist dataset is also frequently used as introductory demonstration to Deep Learning -- train_mnist.ipynb.
In this project, not only will a simple DL training of Mnist dataset be presented, demonstration UI will be realized wireless on your Android mobile phone with the help of DumbDisplay -- start_dd_mnist.py
UI is coded with (and driven by) Python using MicroPython DumbDisplay Library package; and is realized wirelessly on your Android mobile phone with DumbDisplay Android App
Nowadays, code generation with AI is a common practice. Indeed, I did prompt LLM to generate a Jupyter Notebook to train a PyTorch DL model for the Mnist dataset -- train_mnist_ai.ipynb.
The AI-generated DL model is more complex (and certainly more standard) than the one presented in train_mnist.ipynb. At least, mine basically only involves "dense" layers, while the AI-generated one involves "convolutional" layers.
The actual model architecture (the simple version) is captured by the class MnistModel defined in the file model_mnist.py
Please refer to train_mnist.ipynb for the complete training exercise.
Demo UI for the Trained Mnist DL Model




The Python script start_dd_mnist.py starts the DumbDisplay program which uses MicroPython DumbDisplay Library to drive a wireless UI on your Android mobile phone with DumbDisplay Android App.
After starting the Python script start_dd_mnist.py, you should see from VSCode terminal that it waits for connection from DumbDisplay Android App.
You can open the DumbDisplay Android App installed in your Android phone -- DumbDisplay Android App -- and make connection (as shown by above screen-shots)
When connected, the VSCode should show something like
Demo UI for the Trained Mnist DL Model (CONT)

Once connected, on the [black] canvas of the UI, draw a dight you want the DL model to recognize, like the digit 8, and press the >>> button to trigger inference of the drawn digit data (stored in the memory of the running Python process)
The inference with PyTorch is actually performed by the following Python function mnist_inference
Note that mnist_inference is just a callback function for the DumbDisplay "driver" Python code implemented as an example of MicroPython DumbDisplay Library
Now, draw the digit 9 on the canvas ...
If you want to clear what have drawn, press the clear button.
The center button toggles whether the drawn digit will be auto centered before calling mnist_inference for inference.
It is interesting to see that even without auto-centering, the digit recognition is pretty good, especially with the AI generated model -- start_dd_mnist_ai.py.
Sliding Puzzle DL Training and Demo App

The DL model presented in this project for the classical Sliding Puzzle game is a simple and naive "next move" suggesting model -- train_sliding_puzzle.ipynb
I came up with this simple and naive "next move" suggesting model by referencing to the above-mentioned "Hello World" and Mnist DL models, just for fun.
Say, for a 4x4 board.
- There will be 16 tiles, with the 0th tile being the empty space; e.g. the board "orientation" can be represented as
- With respect to the empty space (i.e. the 0th tile), there can be 4 possible moves (but some might be invalid)
- 0: from left
- 1: from right
- 2: from top
- 3: from bottom
- For example, the above solved board can be randomized by a single step of the move 1 (from right) to
- The "next move" of this randomized board is apparently an "undo" move to undo the randomization step, in this case, the move 0 (from left)
- 1 => undo with 0
- 0 => undo with 1
- 2 => undo with 3
- 3 => undo with 2
- In other words, the "undo" moves are the "next moves" toward solving a randomized board
- The way to capture the "undo" moves can be as easy as
- during a randomization step, a valid "move" is select
- the "undo" move is recorded as the "next move" for the randomized board "orientation"
- so, for a board randomized by 5 steps, there will be 5 "next moves" recorded -- one for each board "orientation"
- The board "orientations" is the input to the DL model. The "undo" moves is the output of the DL model.
The actual model architecture is captured by the class SlidingPuzzleModel defined in the file model_sliding_puzzle.py
Please refer to train_sliding_puzzle.ipynb for the complete training exercise, which is a bit more involving, mostly because generation of the training data.
Demo UI for the Trained Sliding Puzzle Model

The demo UI Python script this time is start_dd_sliding_puzzle.py, which is mostly based on the example of MicroPython DumbDisplay Library
After starting the Python script start_dd_sliding_puzzle.py, you should see from VSCode terminal that it waits for connections from the DumbDisplay Android App.
You can open the DumbDisplay Android App and make connection like previously.
Once connect, double-press the sliding puzzle game board to randomize it by 5 steps. You can try to solve the puzzle manually by moving / sliding the appropriate tile to the empty space. If stuck, press the suggest button to have the DL model suggest the "next move" for you.
When "next move" is needed, the following Python function suggest_next_move will be called
Messed up or not, double-press the 🔄 Reset 🔄 button to reset the game; but this time, press the continuous button to have "next move" suggested [and made] continuously.
If things go well, the game should be solved in 5 suggested "next moves".
Once solved, double-press the board to randomize it again; but this time, it will be randomized by 10 steps (5 more than last time).
Once solved again, double-press the board again ...
It is interesting to see how randomized the board is, the DL model can still suggest the correct "next moves" to solve it. My experience is, around 15 to 20 randomize steps.
If you are interested, try tuning the model to see if it can achieve more randomize steps!
Have Fun!
Peace be with you! May God bless you! Jesus loves you! Amazing Grace!