DEEP LEARNING LESSON 9 BUILDING NEURAL NETWORKS WITH PYTHON

Introduction to TensorFlow

TensorFlow is a Python-based machine learning framework that provides tools for building, training, and running neural networks. Instead of manually calculating every weight, gradient, and update, TensorFlow can perform these operations for us.

What Is TensorFlow?

TensorFlow is an open-source machine learning framework developed by Google.

It provides Python tools for working with numerical data and building machine learning models, including neural networks.

Think of TensorFlow as a collection of tools that helps Python perform the mathematical operations required by machine learning.

Python
  ↓
TensorFlow
  ↓
Mathematical Operations
  ↓
Neural Network
  ↓
Training
  ↓
Prediction

Why Do We Need TensorFlow?

Earlier, we manually learned concepts such as:

Neuron
Weights
Bias
Activation Function
Forward Propagation
Loss
Gradients
Backpropagation
Optimizer

In a real neural network, these calculations can become extremely large.

Imagine a network with thousands or millions of weights. Manually calculating every gradient and updating every weight would be impractical.

TensorFlow handles these mathematical operations for us.

Without TensorFlow

You
 ↓
Calculate everything manually
 ↓
Calculate gradients
 ↓
Update thousands of weights
 ↓
Repeat thousands of times


With TensorFlow

You
 ↓
Define the model
 ↓
Train the model
 ↓
TensorFlow handles the calculations

What Is a Tensor?

The name TensorFlow comes from the word "tensor".

A tensor is a data structure used to store numerical data in different dimensions.

You can think of tensors as an extension of familiar Python data structures such as numbers, lists, and matrices.

Number
   ↓
0-dimensional tensor


List
   ↓
1-dimensional tensor


Matrix
   ↓
2-dimensional tensor


Multiple matrices
   ↓
3-dimensional or higher tensor

Simple Tensor Example

TensorFlow can create a tensor like this:

import tensorflow as tf

x = tf.constant([10, 20, 30])

print(x)

The tensor contains:

[10, 20, 30]

We can also perform mathematical operations on tensors.

import tensorflow as tf

x = tf.constant([10, 20, 30])
y = tf.constant([1, 2, 3])

result = x + y

print(result)

Output:

[11 22 33]

TensorFlow performs the operation element by element.

10 + 1 = 11
20 + 2 = 22
30 + 3 = 33

Why Are Tensors Important for Neural Networks?

Neural networks work with numerical data.

For example, suppose we have information about houses:

House 1
Size       = 1200
Bedrooms   = 3
Bathrooms  = 2


House 2
Size       = 1800
Bedrooms   = 4
Bathrooms  = 3

We can represent this data numerically:

[
    [1200, 3, 2],
    [1800, 4, 3]
]

TensorFlow can store and process this numerical data as a tensor.

Installing TensorFlow

TensorFlow can be installed with pip.

pip install tensorflow

Then verify the installation:

import tensorflow as tf

print(tf.__version__)

If TensorFlow is installed correctly, Python will print the installed TensorFlow version.

Your First TensorFlow Program

Let's start with something simple.

import tensorflow as tf

x = tf.constant(10)
y = tf.constant(20)

result = x + y

print(result)

Output:

tf.Tensor(30, shape=(), dtype=int32)

The important result is:

10 + 20 = 30

TensorFlow represents the value as a tensor instead of an ordinary Python integer.

TensorFlow Can Perform Mathematical Operations

TensorFlow provides many mathematical operations.

import tensorflow as tf

x = tf.constant(10)
y = tf.constant(3)

print(tf.add(x, y))
print(tf.subtract(x, y))
print(tf.multiply(x, y))
print(tf.divide(x, y))

These perform:

Addition
10 + 3

Subtraction
10 - 3

Multiplication
10 × 3

Division
10 ÷ 3

These basic operations may look simple, but neural networks perform enormous numbers of similar mathematical operations.

TensorFlow Can Calculate Gradients

This is one of the most important reasons TensorFlow is useful for neural networks.

Earlier, we learned that backpropagation requires gradients.

Loss
 ↓
Gradient
 ↓
Optimizer
 ↓
Updated Weight

TensorFlow provides GradientTape to calculate gradients automatically.

import tensorflow as tf

x = tf.Variable(3.0)

with tf.GradientTape() as tape:

    y = x ** 2

gradient = tape.gradient(y, x)

print(gradient)

Mathematically:

y = x²

dy/dx = 2x

x = 3

gradient = 2 × 3

gradient = 6

TensorFlow calculates this gradient automatically.

TensorFlow and Neural Networks

TensorFlow provides the lower-level mathematical infrastructure needed for machine learning.

For example, TensorFlow can handle:

Tensors
Mathematical Operations
Gradients
Automatic Differentiation
GPU / Hardware Acceleration
Neural Network Operations

However, building a complete neural network directly with these low-level operations can require a lot of code.

That is where Keras becomes useful.

TensorFlow
    ↓
Machine Learning Framework


Keras
    ↓
High-Level Neural Network API


Together
    ↓
Easy Neural Network Development

Building a Neural Network With TensorFlow

TensorFlow includes Keras, which makes neural network creation much simpler.

import tensorflow as tf

model = tf.keras.Sequential([
    tf.keras.layers.Dense(
        8,
        activation="relu",
        input_shape=(2,)
    ),

    tf.keras.layers.Dense(
        1,
        activation="sigmoid"
    )
])

This creates:

Input
  ↓
Dense Layer
8 neurons
ReLU
  ↓
Dense Layer
1 neuron
Sigmoid
  ↓
Output

We will learn exactly how to create and configure these layers in the next topics of this lesson.

Example: Predicting Whether an Email Is Spam

Suppose we want to build a model that predicts whether an email is spam.

Email Data
    ↓
Convert information to numbers
    ↓
TensorFlow processes the data
    ↓
Neural Network
    ↓
Prediction
    ↓
Spam / Not Spam

For example:

Input:

Contains "FREE"       = 1
Contains many links   = 1
Unknown sender        = 1


Output:

0.94

If the model is designed so that values near 1 mean "spam", then:

0.94 → likely spam

TensorFlow can perform the mathematical calculations required to produce that prediction.

Important: TensorFlow Does Not Automatically Make a Good Model

TensorFlow is a tool. It does not magically solve the machine learning problem for you.

Bad Data
    +
Bad Model
    +
Bad Training Configuration
    ↓
Bad Results

You still need to understand:

Data
Model Architecture
Activation Functions
Loss Function
Optimizer
Learning Rate
Epochs
Batch Size
Validation

TensorFlow simply provides the tools to implement and train these systems efficiently.

Python vs TensorFlow

It is important not to think of TensorFlow as a replacement for Python.

Python
    ↓
Programming Language


TensorFlow
    ↓
Machine Learning Framework
built for Python and other environments

You write Python code that uses TensorFlow's APIs.

Python Code
     ↓
TensorFlow API
     ↓
Tensor Operations
     ↓
Machine Learning Model

Understand the Python Code

Consider this simple example:

import tensorflow as tf

x = tf.constant([10, 20, 30])
y = tf.constant([1, 2, 3])

result = x + y

print(result)

Let's understand it line by line.

import tensorflow as tf

This imports TensorFlow and gives it the shorter name tf.

x = tf.constant([10, 20, 30])

This creates a TensorFlow tensor containing three values.

y = tf.constant([1, 2, 3])

This creates another tensor.

result = x + y

TensorFlow adds the corresponding elements:

10 + 1 = 11
20 + 2 = 22
30 + 3 = 33
print(result)

This displays the resulting tensor.

The Main Idea

TensorFlow
    ↓
Works with numerical data
    ↓
Performs mathematical operations
    ↓
Calculates gradients
    ↓
Supports neural networks
    ↓
Helps train models
    ↓
Helps make predictions

The most important thing to understand is that TensorFlow takes care of the difficult mathematical machinery so you can focus more on defining and training the model.

Remember This

TensorFlow = Machine Learning Framework

Tensor = Numerical data structure

tf.constant()
→ Creates a tensor

tf.Variable()
→ Creates a trainable/changeable value

tf.GradientTape()
→ Calculates gradients

tf.keras
→ Provides tools for building neural networks

In the next topic, we will look at Keras, the high-level API that makes building neural networks much easier.

QUICK CHECK

Check Your Understanding

What is TensorFlow?
A machine learning framework that provides tools for numerical computation, gradients, and building and training machine learning models.

What is a tensor?
A data structure used to represent numerical data in one or more dimensions.

Why are tensors important?
Neural networks perform mathematical operations on numerical data, and tensors provide a convenient way to represent that data.

What does GradientTape do?
It can automatically calculate gradients needed during training.

Is TensorFlow a programming language?
No. Python is the programming language here; TensorFlow is the machine learning framework being used from Python.

What is tf.keras?
It is TensorFlow's integration of the Keras high-level API for building and training neural networks.