← Back to list

Keras MNIST model creation flow and how neurons/layers fit together.

1. Load MNIST

Ravindra Pratap Singh · 2026-05-15 16:10 · 1 claps · 1.5 min read
#keras #machine-learning #mnist #ai-model-creation
Open on Medium ↗
Wiki topics: ML · Machine Learning NEU · Neuroscience EDU · Education & Learning

Keras MNIST model creation flow and how neurons/layers fit together.

1. Load MNIST

MNIST has handwritten digit images, each 28×28 pixels, labeled from 0 to 9.

from tensorflow import keras
from tensorflow.keras import layers
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

2. Normalize the images

Pixel values are from 0 to 255. Neural networks train better when values are small, usually 0 to 1.

x_train = x_train / 255.0
x_test = x_test / 255.0

3. Build the model

A simple neural network:

model = keras.Sequential([
    layers.Flatten(input_shape=(28, 28)),
    layers.Dense(128, activation="relu"),
    layers.Dense(10, activation="softmax")
])

What each layer does

Flatten layer

layers.Flatten(input_shape=(28, 28))

Converts each image from:

28 × 28

into:

784 values

So the model can process it like a list of numbers.

Dense hidden layer

layers.Dense(128, activation="relu")

This creates 128 neurons.

Each neuron receives all 784 input values and learns patterns such as edges, curves, loops, or strokes.

A neuron does roughly this:

output = activation(weighted_sum_of_inputs + bias)

So each neuron has:

inputs → weights → sum → bias → activation → output

Output layer

layers.Dense(10, activation="softmax")

This creates 10 neurons, one for each digit:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

softmax converts outputs into probabilities.

Example:

[0.01, 0.02, 0.90, 0.01, ...]

The model predicts the digit with the highest probability.

4. Compile the model

model.compile(
    optimizer="adam",
    loss="sparse_categorical_crossentropy",
    metrics=["accuracy"]
)

Meaning:

optimizer = how the model updates weights
loss = how wrong the model is
accuracy = how we measure performance

5. Train the model

model.fit(x_train, y_train, epochs=5)

During training:

image → model predicts → compare with true label → calculate error → update weights

This repeats many times.

6. Evaluate the model

test_loss, test_acc = model.evaluate(x_test, y_test)
print("Test accuracy:", test_acc)

This checks performance on unseen test images.

How neurons and layers work together

Think of the model like a pipeline:

Image pixels
   ↓
Flatten layer
   ↓
Hidden Dense layer
   ↓
Output layer
   ↓
Predicted digit

Each neuron learns a small pattern.

A layer contains many neurons, so it learns many patterns.

Multiple layers combine simple patterns into more meaningful understanding.

For MNIST:

pixels → edges/strokes → digit-like shapes → final digit prediction

메타데이터
post_id
76ef22daeea9
slug
keras-mnist-model-creation-flow-and-how-neurons-layers-fit-together-76ef22daeea9
url
https://medium.com/@ravindragsingh/keras-mnist-model-creation-flow-and-how-neurons-layers-fit-together-76ef22daeea9
canonical_url
https://medium.com/@ravindragsingh/keras-mnist-model-creation-flow-and-how-neurons-layers-fit-together-76ef22daeea9
author_url
https://medium.com/@ravindragsingh
status
ok
fetched_at
2026-06-09 15:37:30