Introduction to Keras & TensorFlow

Keras Basics

3 min read

Published Nov 17 2025


11
0
0
0

KerasNeural NetworksPythonTensorFlow

Keras is a high-level deep learning API designed to make building neural networks:

  • Simple
  • Fast
  • Modular
  • Consistent
  • Readable

Originally, Keras was a standalone library that could run on top of multiple backends. Today, Keras is built directly into TensorFlow, and the official version is:

from tensorflow import keras

This means you use:

  • TensorFlow for computation
  • Keras as the high-level interface
  • Both integrated into one workflow

Keras handles:

  • Layers
  • Models
  • Training
  • Losses / metrics
  • Preprocessing
  • Utilities
  • Data pipelines
  • Transfer learning

TensorFlow handles the heavy lifting:

  • GPU acceleration
  • Automatic differentiation
  • Graph optimisation
  • Large-scale data pipelines





Why Keras Exists

Deep learning can be complicated. Keras solves this by providing:

  • A clean, Pythonic API - Keras code is readable, short, and structured.
  • Sensible defaults - You rarely need low-level details unless you choose to.
  • Modular building blocks - Models are assembled from layers exactly like building with Lego bricks.
  • Interoperability with TensorFlow - You can drop down to low-level TF code whenever needed.
  • Fast prototyping - You can create, compile, and train a model in under 10 lines.

Keras is especially helpful when you want to focus on ideas rather than the mechanics of neural network training.






What Keras Is Good At

Keras is well suited for building models in:

  • Image classification (CNNs)
  • Natural language processing (LSTM, GRU, Transformers)
  • Tabular deep learning
  • Time-series forecasting
  • Reinforcement learning (with wrappers)
  • Transfer learning & fine-tuning
  • Autoencoders
  • GANs (generative models)

Keras makes these workflows accessible and beginner-friendly, while still being powerful enough for production.






What Keras Is Not Ideal For

Keras is not the best choice for:

  • Extremely large-scale distributed training (use low-level TF or JAX)
  • Highly experimental architectures requiring custom training loops (although possible in TF)
  • Classical ML tasks like SVMs, decision trees, boosting (use Scikit-learn)





How Keras Fits Into the TensorFlow Ecosystem

TensorFlow provides:

  • Tensors
  • Automatic differentiation
  • Device placement (CPU / GPU / TPU)
  • Training loops (tf.GradientTape)
  • Data loading (tf.data)

Keras provides:

  • High-level model classes (Sequential, Model)
  • High-level layers
  • Fit/evaluate/predict loops
  • Callbacks (EarlyStopping, Checkpointing)
  • Preprocessing utilities
  • Dataset helpers (e.g., keras.datasets)

They work together seamlessly.






The Two Ways to Build Models in Keras

There are two major Keras model-building styles:


1) Sequential API

Best for simple, feed-forward architectures


Example:

from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential([
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])

Easy, readable, limited flexibility.



2) Functional API

Best for flexible or multi-input/multi-output models


Example:

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

model = keras.Model(inputs, outputs)

Needed for:

  • Skip connections
  • Multiple inputs
  • Multiple outputs
  • Custom topologies

Most real-world models use this.






The Core Keras Workflow

Deep learning with Keras always follows the same pattern:

  1. Load and prepare data
  2. Build the model
  3. Compile the model (loss, optimiser, metrics)
  4. Train the model (fit)
  5. Evaluate the model
  6. Use the model (predict)
  7. Save / load the model





Installing Keras

pip install tensorflow

This installs:

  • TensorFlow
  • Keras
  • GPU support if available on your system
  • All basic dependencies




Importing Keras

All imports follow this pattern:

from tensorflow import keras
from tensorflow.keras import layers, models, datasets





Example: Minimal Keras Program (Full Workflow)

We’ll build something simple just to show the shape of a Keras script.

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import datasets

# 1. Load data
(x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()
x_train = x_train.reshape(-1, 28*28) / 255.0
x_test = x_test.reshape(-1, 28*28) / 255.0

# 2. Build model
model = keras.Sequential([
    layers.Dense(128, activation='relu'),
    layers.Dense(10, activation='softmax')
])

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

# 4. Train
model.fit(x_train, y_train, epochs=3, batch_size=32)

# 5. Evaluate
model.evaluate(x_test, y_test)

# 6. Predict
pred = model.predict(x_test[:1])

This short code sample illustrates the entire workflow. We will revisit each part in depth in later sections.

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact
Keras Basics | Introduction to Keras & TensorFlow | SimpleSteps.guide