Machine Learning 101: A Hands-On TensorFlow Guide
Not sure where to start when it comes to ML? I got ya. Machine Learning 101 - Lesson 1
Introduction: AI Without the Doom and Gloom
Ah, TensorFlow! The name alone sounds like something that would either make me a genius overnight or fry my laptop into oblivion. For years, I avoided it, mostly because every tutorial was either written for PhD candidates or complete beginners who think Python is a kind of snake. Which it is, but you know what I mean. Where was the middle ground? Well, turns out, it's here.
Today, I’m going to walk you through TensorFlow in a way that is both useful and mildly entertaining. If you've ever wanted to dip your toes into machine learning without drowning in jargon, buckle up.
Step 1: Install TensorFlow
First things first, let’s get TensorFlow installed. The easiest way to install it is via pip. You will need at least Python 3.9 and ideally Python 3.12. Open your terminal (or command prompt if you're feeling nostalgic) and run:
pip install tensorflow
If you want GPU acceleration, and trust me, you do if you don’t want your CPU weeping in a corner, make sure you have CUDA installed. If that last sentence scared you, stick to CPU for now. Baby steps.
Step 2: The Obligatory "Hello, TensorFlow!"
Most tutorials start with Hello, World! but we’re cooler than that. Let’s do something actually useful: creating a simple neural network that predicts a number pattern. First, let’s check if TensorFlow is working.
import tensorflow as tf
print(tf.__version__)
If this prints a version number instead of an error message, congrats! You’re officially a machine learning engineer. Okay, not really, but let’s pretend.
Step 3: Build a Simple Model
Let’s say we have a sequence like this:
1, 2, 3, 4... what comes next?
You probably know what comes next. But let’s teach a machine to do it using a neural network.
import numpy as np
from tensorflow import keras
# Training data
xs = np.array([1, 2, 3, 4], dtype=float)
ys = np.array([2, 3, 4, 5], dtype=float)
# Define the model
model = keras.Sequential([
keras.layers.Dense(units=1, input_shape=[1])
])
# Compile the model
model.compile(optimizer='sgd', loss='mean_squared_error')
# Train the model
model.fit(xs, ys, epochs=500)
# Predict something
print(model.predict([10]))
You’ve just built a basic neural network! It might not land you a job at OpenAI yet, but hey, it’s a start.
Step 4: Going a Step Further
Alright, so predicting simple sequences is fun, but what if we want something fancier? Let’s build a basic image classifier using TensorFlow’s tf.keras
module and the famous MNIST dataset, aka the "Hello World" of deep learning.
from keras import datasets, layers, models
import matplotlib.pyplot as plt
# Load the dataset
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
# Normalize pixel values
train_images, test_images = train_images / 255.0, test_images / 255.0
# Define the model
model = models.Sequential([
layers.Flatten(input_shape=(28, 28)),
layers.Dense(128, activation='relu'),
layers.Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(train_images, train_labels, epochs=5)
# Evaluate the model
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Accuracy: {test_acc:.4f}')
What’s Happening Here?
Defining the Model
This part constructs the neural network architecture. Here’s what each layer does:
layers.Flatten(input_shape=(28, 28))
The input is a 28x28 grayscale image (like the MNIST digits).
This layer flattens it into a 1D array of 784 values (28×28=784 pixels).
No neurons, no weights—just reshaping the input so that it can be fed into the next layer.
layers.Dense(128, activation='relu')
A fully connected (dense) layer with 128 neurons.
Each neuron is connected to all 784 input values from the previous layer.
Uses ReLU (Rectified Linear Unit) activation, which helps the model learn complex patterns by introducing non-linearity.
layers.Dense(10, activation='softmax')
Another fully connected layer, but with 10 neurons, one for each digit (0-9).
Uses softmax activation, which converts raw scores into probabilities, ensuring they sum up to 1.
This helps classify the image into one of 10 categories (digits 0-9).
Compiling the Model
This step sets up how the model will learn.
optimizer='adam'
Adam
(Adaptive Moment Estimation) is an optimization algorithm.It adjusts learning rates automatically, helping the model converge faster and avoid bad updates.
It’s widely used because it balances speed and accuracy.
loss='sparse_categorical_crossentropy'
Measures how far the model’s predictions are from the actual labels.
Since we have 10 possible classes (digits 0-9), we use categorical crossentropy.
The sparse part means the labels are given as integers (0-9), rather than one-hot encoded vectors.
metrics=['accuracy']
This tells TensorFlow to track accuracy during training.
It’s useful for evaluating how well the model is classifying digits.
Boom. You just built an AI that can recognize handwritten digits! Hopefully. If you train it enough, it might even recognize my terrible handwriting. Just text me and ask for the dataset.
Keep Experimenting!
TensorFlow isn’t scary once you get past the intimidating documentation and the occasional cryptic error message. It’s like training a dog—frustrating at first, but incredibly rewarding when it finally does what you want.
So, what next? Try tweaking the number of layers, changing activation functions, or even feeding it weird data (like different font styles) to see how well it adapts. Machine learning is all about experimenting and breaking things until they work. What isn’t, afterall?
And remember: as always, if you ever feel stuck, just type the error message into a chatbot prompt, or Google. Chances are, someone else has already suffered through it and found a solution.
Happy coding!
What’s next?
I wanted to start with a quick hands-on experience. But the second lesson of 101 will slowly ease you into the world of ML and AI.