TensorFlow Keras Intro

Keras is the official high-level API for TensorFlow. It acts as a friendly interface that sits on top of TensorFlow's complex mathematical machinery. Instead of writing hundreds of lines of low-level code to build a neural network, Keras lets you describe a model in just a few lines. Since TensorFlow 2.0, Keras is fully built into TensorFlow — you do not install it separately.

The Restaurant Analogy

Think of TensorFlow as a fully equipped professional kitchen with every tool imaginable — industrial mixers, specialty ovens, precise temperature gauges. Keras is the experienced head chef who knows how to use every tool efficiently. You (the programmer) tell Keras what dish you want to cook, and Keras handles the kitchen details for you.

What Keras Gives You

Ready-Made Building Blocks

Keras provides pre-built layers, activation functions, loss functions, and optimizers. You assemble these building blocks like LEGO pieces to create your model.

Three Ways to Build Models

Keras offers three approaches to building neural networks, each suited to different levels of complexity:

Building Approach      Best For
─────────────────────────────────────────────────────
Sequential API       → Simple, straight-line models
Functional API       → Models with branches and merges
Subclassing API      → Full custom control over layers

The Sequential API — Simplest Approach

The Sequential model stacks layers one after another in a single straight line. Data enters the first layer, passes through each layer in order, and exits the last layer as a prediction.

import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Dense(128, activation='relu', input_shape=(20,)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])
Diagram — Sequential Model:

Input (20 features)
        |
        v
  Dense Layer (128 neurons) — learns complex patterns
        |
        v
  Dense Layer (64 neurons)  — refines the patterns
        |
        v
  Dense Layer (10 neurons)  — one score per class
        |
        v
Output (10 class probabilities)

The Functional API — More Flexible

The Functional API allows models where layers share inputs, merge outputs, or branch into multiple paths. You use this when your model has a more complex structure than a straight line.

inputs = tf.keras.Input(shape=(20,))
x = tf.keras.layers.Dense(128, activation='relu')(inputs)
x = tf.keras.layers.Dense(64, activation='relu')(x)
outputs = tf.keras.layers.Dense(10, activation='softmax')(x)

model = tf.keras.Model(inputs=inputs, outputs=outputs)

The result is identical to the Sequential version above, but the Functional API makes it possible to connect layers in ways the Sequential API cannot handle, such as connecting one input to multiple branches.

Model Summary

After building a model, you can print a summary that shows every layer, its output shape, and how many trainable parameters it contains:

model.summary()

Example output:

Model: "sequential"
_________________________________________________________________
 Layer (type)           Output Shape         Param #
=================================================================
 dense (Dense)          (None, 128)          2688
 dense_1 (Dense)        (None, 64)           8256
 dense_2 (Dense)        (None, 10)           650
=================================================================
Total params: 11,594
Trainable params: 11,594
Non-trainable params: 0

The first dense layer has 2,688 parameters: 20 inputs × 128 neurons = 2,560 weights, plus 128 bias values = 2,688 total. These are all the numbers that TensorFlow adjusts during training.

Key Keras Components

Layers

Layers are the core building blocks. Each layer receives a tensor, transforms it, and passes a new tensor to the next layer. The most common layers are:

  • Dense — fully connected layer for general-purpose learning
  • Conv2D — extracts features from images
  • LSTM — processes sequences like text or time-series data
  • Dropout — randomly turns off neurons to prevent overfitting
  • BatchNormalization — stabilizes training by normalizing layer outputs

Activation Functions

An activation function decides whether a neuron's output should be passed forward and in what form. Without activation functions, a neural network is just a linear equation, no matter how many layers it has.

  • ReLU — outputs the input if positive, otherwise outputs 0. Works well in hidden layers.
  • Sigmoid — squashes output to a value between 0 and 1. Used for binary yes/no predictions.
  • Softmax — converts outputs into probabilities that sum to 1. Used in the final layer for multi-class problems.
  • Tanh — squashes output to a value between -1 and 1. Often used in RNN hidden layers.

Loss Functions

The loss function measures how wrong the model's prediction is compared to the correct answer. Keras provides many built-in loss functions:

  • mean_squared_error — for regression (predicting a number like price or temperature)
  • binary_crossentropy — for binary classification (yes or no)
  • categorical_crossentropy — for multi-class classification (picking one category from many)
  • sparse_categorical_crossentropy — same as above but labels are integers instead of one-hot vectors

Optimizers

Optimizers use the loss value to update the model's weights. Keras includes all major optimizers:

  • Adam — the most popular choice; adapts the learning rate automatically
  • SGD — Stochastic Gradient Descent; simple and effective with a tuned learning rate
  • RMSprop — works well for recurrent neural networks
  • Adagrad — adjusts learning rates individually for each parameter

Metrics: Measuring Model Performance

Beyond loss, you can track additional metrics during training. For classification, accuracy is the most common metric: it reports the percentage of predictions the model got right.

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

Keras vs. Low-Level TensorFlow

Task                  Low-Level TensorFlow    Keras
──────────────────────────────────────────────────────────────
Define a layer        ~30 lines of code       1 line
Forward pass          Write manually          Automatic
Backpropagation       Write manually          Automatic
Track metrics         Write manually          Automatic
Save model            Write manually          model.save()
Load model            Write manually          keras.models.load_model()

Keras handles everything that would otherwise require writing raw TensorFlow operations. For most practical projects, Keras is all you need. Low-level TensorFlow becomes relevant only when you need to build custom training loops or non-standard architectures, which this course covers in later topics.

A Complete Mini Example

import tensorflow as tf
import numpy as np

# Create fake data: 100 samples, 10 features each
X = np.random.random((100, 10)).astype('float32')
y = np.random.randint(0, 3, size=(100,))  # 3 classes

# Build model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(32, activation='relu', input_shape=(10,)),
    tf.keras.layers.Dense(16, activation='relu'),
    tf.keras.layers.Dense(3, activation='softmax')
])

# Compile
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Train
model.fit(X, y, epochs=10, batch_size=16)

In under 15 lines of code, you built, compiled, and trained a three-class neural network. Keras made all the complex machinery invisible so you could focus on the model design. The next topic explores the Sequential model in more detail, including how to add, remove, and inspect layers.

Leave a Comment

Your email address will not be published. Required fields are marked *