Python Refresher
Deep Learning & TensorFlow
Deep Learning is a specialized subset of Machine Learning based on Artificial Neural Networks. It is the technology behind voice assistants, image recognition, and the models we deploy to the edge.
What Is a Neural Network?
A neural network is loosely inspired by the human brain. It consists of layers of interconnected nodes (neurons).
- Input Layer: Takes in the raw data (e.g., pixels of an image, or 3-axis accelerometer readings).
- Hidden Layers: The layers in the middle where the "learning" happens. They extract increasingly complex features from the data.
- Output Layer: Produces the final prediction (e.g., "There is a 95% chance this is a cat").
Why "Deep"? A neural network is considered "deep" if it has multiple hidden layers.
TensorFlow and Keras
Building neural networks from scratch using raw math is incredibly difficult. Instead, we use frameworks.
TensorFlow is a massive, open-source machine learning platform developed by Google. Keras is a high-level API built on top of TensorFlow that makes defining and training neural networks very intuitive using Python.
Building a Simple Model with Keras
Here is an example of how you build a standard "Dense" (fully connected) neural network in Keras:
1import tensorflow as tf2from tensorflow import keras34# 1. Define the Architecture5model = keras.Sequential([6 # Input layer expecting 3 values (e.g., X, Y, Z acceleration)7 keras.layers.Dense(16, activation='relu', input_shape=(3,)), 8 9 # Hidden layer10 keras.layers.Dense(8, activation='relu'),11 12 # Output layer with 2 nodes (e.g., "Walking" or "Running")13 keras.layers.Dense(2, activation='softmax')14])1516# 2. Compile the Model (Tell it how to learn)17model.compile(optimizer='adam',18 loss='sparse_categorical_crossentropy',19 metrics=['accuracy'])2021# 3. Train the Model (Assuming 'X_train' is data and 'y_train' are labels)22# We will pass through the data 50 times (epochs)23model.fit(X_train, y_train, epochs=50)Exporting to TFLite (For ESP32)
A standard TensorFlow model is huge—often dozens or hundreds of megabytes. It requires a powerful CPU or GPU to run, making it impossible to run on an ESP32.
To deploy a model to a microcontroller, we must convert it using TensorFlow Lite for Microcontrollers (TFLite Micro).
This conversion process does two things:
- Strips out the training code: The ESP32 only needs to run the model (inference), not train it.
- Quantization (Optional but common): Converts high-precision 32-bit floats into 8-bit integers. This drastically shrinks the model size and makes it run much faster on hardware without floating-point units, at the cost of a tiny bit of accuracy.
1# Example of converting a Keras model to TFLite2converter = tf.lite.TFLiteConverter.from_keras_model(model)3tflite_model = converter.convert()45# Save the model to a file6with open('model.tflite', 'wb') as f:7 f.write(tflite_model)Finally, this .tflite file is converted into a C byte array (xxd -i model.tflite > model.h) so it can be compiled directly into your ESP-IDF firmware!

