Day 38 of 50 Days of Python: Deep Learning Basics with TensorFlow
Part of Week 6: Advanced Topics
Welcome back to 50 Days of Python! In Day 37 we covered data preprocessing and modeling with scikit‑learn. Today we’ll take our first steps into deep learning using the industry‑standard TensorFlow high‑level API. By the end of this article you will have trained your own neural‑network that recognises handwritten digits, all in under 30 lines of code.
What We’ll Cover
I understand that deep learning is a subject people are scared of, mainly because they think you need a degree or some sort of certification but the fact is, its not too difficult once you understand a use case. I’ll breakdown the content into sections so you can better understand what we are covering and when:
Overall theme: Why deep learning ≠ “magic”, it’s just differentiable functions.
Key TensorFlow concepts: tensors, computational graph, layers & models.
The typical training workflow: build → compile → fit → evaluate.
Prerequisites
Python
Basic numpy knowledge
the TensorFlow package:
pip install tensorflow
Core Concepts of TensorFlow
→ Tensor: A Multi-dimensional array with a data-type & shape.
→ Layer: Function that transforms an input tensor and has trainable weights.
→ Model: Directed graph of layers which are callable like a regular Python function.
→ Loss: A Scalar measuring “how wrong” the model is. An example of this is cross-entropy.
→ Optimizer: Algorithm that teaks weights to minimise the loss. Example for this is Adam.
Hands-On: Build a Digit Classifier
We’ll tackle the canonical “Hello, World!” of deep learning: MNIST handwritten digits (70 000 28×28 grayscale images).
import tensorflow as tf
from tensorflow.keras import layers, models
# 1 – Data
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
X_train = X_train.reshape(-1, 28, 28, 1).astype("float32") / 255
X_test = X_test.reshape(-1, 28, 28, 1).astype("float32") / 255
# 2 – Model
model = models.Sequential([
layers.Input(shape=(28, 28, 1)),
layers.Flatten(),
layers.Dense(128, activation="relu"),
layers.Dropout(0.2),
layers.Dense(10, activation="softmax")
])
# 3 – Compile
model.compile(optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"])
# 4 – Train
history = model.fit(X_train, y_train,
epochs=5,
batch_size=128,
validation_split=0.1)
# 5 – Evaluate
loss, acc = model.evaluate(X_test, y_test, verbose=0)
print(f"Test accuracy: {acc:.3f}")
The Code Explained
Load & Preprocess: Keras is inbuilt with the MNIST dataset. We reshape to (batch, 28, 28, 1) and scale to [0, 1].
Define the network: Sequential is a stack of layers; our network is Flatten → Dense(128 ReLU) → Dropout → Dense(10 softmax).
Compile: We specify what to optimise (loss) and how (Adam).
Fit: Keras trains and returns a History object for plotting.
Evaluate: Our tiny network scores ≈ 0.97 accuracy after 5 epochs on CPU!
If you want to train it using GPU instead I’d advise loading the code into a google collab space and switching the compute to the free GPU.
Digging Deeper: Making Sense of the Numbers
Accuracy vs Loss: Accuracy is easy to grok, but watch the loss curve for over‑fitting.
Dropout randomly disables neurons during training to improve generalisation.
Softmax converts raw logits to probabilities that sum to 1, perfect for multi‑class tasks.
GPU Speed‑Up. Installing tensorflow‑mac‑metal (Mac) or enabling CUDA (Linux / Windows + NVIDIA) will slash training time.
Next Up: Day 39 - Building a Simple Neural Network in TensorFlow.
Just like it says on the tin, we’ll be building a simple neural network to enhance what we’ve covered today. We’ll also utilise Scikit-Learn for a few different purposes.
See you for the next one… Happy Coding!!