Stop 07 · Deep Learning FoundationsTopics · 8/11

TensorFlow/Keras Basics

Part of the AI Engineer Roadmap

In short

Keras lets you describe a neural network as a list of layers and train it with one fit() call. It's a quick way to get a working model, and plenty of production systems run on it.

Google's deep learning framework, with Keras as its high-level API — dominant in production/mobile deployment contexts, useful to know even if PyTorch is your primary tool.

The blob stacks the round tiers of a steel tiffin box one on top of another.

Think of it like this

A tiffin subscription

With a tiffin service you pick the menu, agree on the rules (no onion, spice level two) and they deliver every day. In Keras you pick the layers, compile with an optimiser and loss, then fit() handles the daily delivery of batches and updates.

Watch it happen

The Keras workflow

Sequential
compile
fit
evaluate
predict

1/5List the layers in order: an Input with the feature shape, then Dense layers.

The big ideas

What you need to know

Layers stack in order

Sequential suits a straight stack. For branches or multiple inputs, Keras has a functional API.

compile sets the rules

Optimiser, loss and metrics are fixed here. Changing them means compiling again.

fit hides the loop

No zero_grad or backward calls. Use callbacks like EarlyStopping to control training.

Keras 3 is multi-backend

Keras 3 can run on TensorFlow, JAX or PyTorch, so the same model code can move between them.

See it in code

Walk through the code

python
1import numpy as np2from tensorflow import keras3 4X = np.random.rand(500, 4).astype("float32")5y = (X.sum(axis=1) > 2).astype("int32")   # simple rule to learn6 7model = keras.Sequential([8    keras.Input(shape=(4,)),9    keras.layers.Dense(16, activation="relu"),10    keras.layers.Dense(1, activation="sigmoid"),11])12model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])13model.fit(X, y, epochs=20, batch_size=32, validation_split=0.2, verbose=0)14 15print(model.evaluate(X, y, verbose=0))   # [loss, accuracy]

1/4Four input features, one hidden layer of 16 units, and one sigmoid output for yes or no.

A small classifier in about a dozen lines. validation_split holds back 20% of rows to check progress each epoch.

Save yourself the pain

Common mistakes

Oops

Using a sigmoid output with categorical_crossentropy for 10 classes.

Do this instead

For many classes use Dense(10, activation='softmax') with sparse_categorical_crossentropy for integer labels.

Oops

Thinking validation_split shuffles before splitting.

Do this instead

It takes the last part of the data as is. Shuffle your arrays first if they're sorted.

Your mission

Same model, two libraries

~30 min

Quick check

In Keras, which call actually runs the training loop over your data?

Go deeper

  1. 1Build the same small network in Keras that you built in PyTorch, to compare APIs.
  2. 2Learn the Sequential vs. Functional API in Keras.
  3. 3Understand where TensorFlow/Keras is commonly chosen over PyTorch (mobile, TF Serving).

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