Stop 09 · Deep Learning FundamentalsTopics · 4/10

Building Networks With TensorFlow / Keras

Part of the Data Science Roadmap

In short

Keras lets you build a working image classifier in about fifteen lines. It's the quickest way to go from 'I understand neurons' to 'I trained a real network'.

Google's deep learning framework — Keras (now built into TensorFlow) offers a high-level, approachable API for defining, training and evaluating neural networks.

The mascot assembles a flat-pack wooden bookshelf with a screwdriver, spare panels leaning nearby.

Think of it like this

Flat-pack furniture

A flat-pack bookshelf comes with ready-made panels and a clear manual. You stack the pieces in order, tighten a few screws and you're done. You don't saw the wood yourself. Keras gives you ready-made layers, and you just stack them.

Watch it happen

Two ways to define a Keras model

Sequential

  • A plain list of layers
  • One input, one output
  • Best for simple models

Functional API

  • Connect layers like a graph
  • Multiple inputs or outputs
  • Needed for transfer learning tricks

Start with Sequential. Switch to the Functional API when layers need to branch, merge or reuse a pretrained base.

The big ideas

What you need to know

Stack layers

Dense, Conv2D, LSTM and Dropout are ready-made. You choose sizes and activations.

Compile picks the rules

compile() sets the optimizer, like Adam, the loss to minimise, and the metrics to report.

fit() trains

fit() runs the whole training loop for you, with validation scores printed after each epoch.

Save in one line

model.save('model.keras') stores architecture and weights together for later use.

See it in code

Walk through the code

python
1import keras2from keras import layers3 4(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()5x_train, x_test = x_train / 255.0, x_test / 255.06 7model = keras.Sequential([8    keras.Input(shape=(28, 28)),9    layers.Flatten(),10    layers.Dense(128, activation="relu"),11    layers.Dense(10, activation="softmax"),12])13model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])14model.fit(x_train, y_train, epochs=3, validation_split=0.1)15print(model.evaluate(x_test, y_test))

1/4Load 28 by 28 digit images and scale pixels to 0 to 1.

A complete handwritten-digit classifier. Install with pip install tensorflow, which includes Keras.

Save yourself the pain

Common mistakes

Oops

Using categorical_crossentropy with integer labels like 3 and 7.

Do this instead

Use sparse_categorical_crossentropy for integer labels, or one-hot encode labels for categorical_crossentropy.

Oops

Forgetting to scale pixel values from 0-255.

Do this instead

Divide by 255 or add a Rescaling layer. Unscaled inputs make training slow and unstable.

Your mission

Train MNIST in Colab

~25 min

Quick check

What does model.compile() do in Keras?

Go deeper

  1. 1Build and train a simple feedforward network in Keras on a small dataset.
  2. 2Learn the standard workflow: define model, compile, fit, evaluate.
  3. 3Compare training curves (loss/accuracy) between training and validation sets.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge