Skip to content
CampusEduX

All Topics · Lesson 70 of 77

TensorFlow / Keras

Complete learning notes

7 min read

01Introduction

Now that you understand what a neural network IS conceptually (Topic 2), let's look at how you'd actually BUILD one in code. TensorFlow (with its user-friendly Keras interface) is one of the two dominant frameworks for this (the other being PyTorch, Topic 4). This topic gives you a first, gentle hands-on look.


02What are TensorFlow and Keras?

Term: TensorFlow / Keras

Simple definition: TensorFlow is a powerful, low-level library for building and training neural networks; Keras is a simpler, more beginner-friendly interface built on top of it, making common neural network tasks much easier to write.

In simple words: If TensorFlow is like a fully-equipped workshop with every possible tool, Keras is like a well-organized toolkit that hands you exactly the right tool for the most common jobs, without you needing to dig through the whole workshop.

Technical explanation: TensorFlow is an open-source Deep Learning framework developed by Google, providing tensor-based computation (multi-dimensional arrays, similar in spirit to NumPy arrays but capable of running on GPUs) and automatic differentiation for training neural networks; Keras is TensorFlow's official high-level API, offering a simple, readable syntax (Sequential models, .compile(), .fit()) for defining and training networks without manually implementing backpropagation.


03Why is it Important?

  • It's one of the two most widely used Deep Learning frameworks in both industry and research (alongside PyTorch, Topic 4).
  • Keras's simple API lets you build and train a working neural network in just a few lines of code, directly building on the neuron/layer concepts from Topic 2.
  • Understanding this basic workflow prepares you to read and adapt countless existing Deep Learning tutorials, tools, and pretrained models.

04Prerequisites

Comfort with Neural Networks (Topic 2) and the general Scikit-learn .fit()/.predict() workflow (Module 8, Topic 1), which Keras's API deliberately echoes.


05Core Concepts

  1. Tensors — the basic data structure
  2. Building a model with Sequential and Dense layers
  3. Compiling a model (loss function, optimizer)
  4. Training (.fit()) and predicting (.predict())

06Detailed Explanation

a) Tensors

Term: Tensor

Simple definition: A tensor is simply a multi-dimensional array of numbers — conceptually very similar to a NumPy array (Module 1, Topic 5), but designed to also run efficiently on GPUs.

In simple words: A single number is a tensor with 0 dimensions; a list of numbers is a 1D tensor (like a NumPy array); a table of numbers is a 2D tensor (like a matrix); an image with color channels might be a 3D tensor.

b) Building a Model with `Sequential` and `Dense` Layers

Keras's Sequential model lets you stack layers one after another, in order — mirroring the input → hidden → output layer structure from Topic 2. A Dense layer means every neuron in that layer connects to every neuron in the previous layer (the standard, "fully connected" layer type).

c) Compiling a Model

Before training, you .compile() the model, specifying: the loss function (how to measure prediction error, Topic 2), the optimizer (the specific algorithm used to perform backpropagation's weight updates — e.g., "adam" is a very common, effective default choice), and any metrics you want tracked during training (e.g., accuracy).

d) Training and Predicting

.fit(X_train, y_train, epochs=...) trains the model — running forward propagation, calculating loss, and backpropagating, repeated for the specified number of "epochs" (full passes through the training data). .predict(X_new) then generates predictions on new data, exactly mirroring Scikit-learn's familiar pattern.


07How It Works

  1. Define the network's architecture using Sequential([...]), stacking Dense layers with chosen activation functions.
  2. .compile() the model with a loss function, optimizer, and metrics.
  3. .fit() the model on training data for a chosen number of epochs.
  4. .predict() on new data to generate predictions.
  5. .evaluate() on test data to check final performance (connecting to Module 6's evaluation concepts).

08Real-World Example

A simple Keras network to classify handwritten digits (0-9) from images might have: an input layer matching the image's pixel count, one or two hidden Dense layers with ReLU activation (Topic 2) to learn intermediate patterns, and a final output layer with 10 neurons (one per digit) using Softmax activation (Topic 2) to output a probability for each possible digit.


09Python Example

python
import tensorflow as tf from tensorflow import keras from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split # Generating a small synthetic dataset (as in Module 8, Topic 1) X, y = make_classification(n_samples=200, n_features=4, random_state=42) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Building a simple neural network model = keras.Sequential([ keras.layers.Dense(8, activation="relu", input_shape=(4,)), keras.layers.Dense(4, activation="relu"), keras.layers.Dense(1, activation="sigmoid") ]) # Compiling the model model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"]) # Training the model model.fit(X_train, y_train, epochs=20, verbose=0) # Evaluating on test data loss, accuracy = model.evaluate(X_test, y_test, verbose=0) print("Test accuracy:", accuracy)

Expected Output (approximate):

text
Test accuracy: 0.875

10Code Explanation

  • keras.Sequential([...]) stacks three Dense layers: an input-connected hidden layer with 8 neurons, another hidden layer with 4 neurons (both using ReLU, per Topic 2's recommendation for hidden layers), and a final single-neuron output layer using Sigmoid (appropriate for binary classification, echoing Logistic Regression, Module 4, Topic 3).
  • model.compile(optimizer="adam", loss="binary_crossentropy", ...) configures HOW the network will learn — "adam" is the optimization algorithm, and "binary_crossentropy" is the loss function appropriate for binary classification problems.
  • model.fit(X_train, y_train, epochs=20, ...) trains the network for 20 full passes through the training data.
  • model.evaluate(X_test, y_test, ...) reports the model's loss and accuracy on the held-out test set — directly connecting to Module 6's evaluation concepts.

11Advantages

  • Keras's simple, readable syntax makes building neural networks approachable, even for beginners.
  • Backed by Google, with a huge community, extensive documentation, and production-ready deployment tools.
  • Scales from simple, small networks (like this example) up to enormous, state-of-the-art models.

12Limitations

  • Still requires understanding of the underlying Deep Learning concepts (Topics 1-2) to use effectively and troubleshoot.
  • Training more complex models remains computationally demanding, often requiring GPU access for practical training times.
  • Some advanced, highly custom research architectures may require dropping down from Keras's simplicity into lower-level TensorFlow code.

13Common Mistakes

  • Forgetting to match the output layer's activation function and neuron count to the specific task (e.g., Sigmoid + 1 neuron for binary classification, Softmax + N neurons for N-class classification).
  • Choosing too few epochs (undertrained model) or too many (risking overfitting, Module 7, Topic 1) without monitoring validation performance.
  • Not scaling/normalizing input features before training, similar to the scaling considerations in Module 3, Topic 7.

14Best Practices

  • Start with a simple architecture (few layers, few neurons) and increase complexity only if needed.
  • Monitor training vs validation loss/accuracy during .fit() to watch for overfitting.
  • Scale numeric input features before training, just as with KNN/SVM in traditional ML.
  • Use "adam" as a strong, sensible default optimizer choice for most problems.

15Real-World Applications

  • Building and training custom neural networks for the specialized applications in this module (Computer Vision, NLP).
  • Rapid prototyping of Deep Learning ideas before scaling to production.
  • Educational and research settings, given Keras's approachable, readable syntax.

16Interview-Oriented Points

  • Be ready to explain the basic Keras workflow: define architecture → compile → fit → evaluate/predict.
  • Understand what a tensor is and its relationship to NumPy arrays.
  • Be able to explain why the output layer's activation function must match the task (binary vs multi-class classification vs regression).

17Exam-Oriented Points

  • TensorFlow is a Deep Learning framework; Keras is its high-level, simplified API.
  • Sequential stacks layers in order; Dense layers are fully connected.
  • .compile() sets the loss function and optimizer; .fit() trains; .predict()/.evaluate() generate predictions/assess performance.

18Comparison Table — TensorFlow/Keras vs Scikit-learn

AspectScikit-learn (Modules 3-7)TensorFlow/Keras
Primary focusTraditional ML algorithmsDeep Learning / neural networks
API pattern.fit() / .predict().compile() → .fit() → .predict()/.evaluate()
Data structureNumPy arrays / Pandas DataFramesTensors (GPU-capable multi-dimensional arrays)
Best suited forStructured/tabular dataUnstructured data (images, audio, text) at scale

19Quick Revision

  • TensorFlow is a powerful Deep Learning framework; Keras is its simple, high-level interface built on top.
  • Tensors are multi-dimensional arrays (like NumPy arrays) capable of running on GPUs.
  • Basic workflow: build architecture with Sequential/Dense layers → .compile() (loss, optimizer) → .fit() (train) → .predict()/.evaluate().
  • Match the output layer's activation function to the task: Sigmoid for binary classification, Softmax for multi-class, none/linear for regression.

Mock Test

  • TensorFlow / Keras — Quick Test

    A 10-question multiple-choice check on TensorFlow / Keras.

    10 questions · 10 min · Easy
    Start Mock Test