DEEP LEARNING LESSON 9 BUILDING NEURAL NETWORKS WITH PYTHON

Understand the Python Code

In this lesson, we will put everything from this chapter together and understand how the Python code creates, trains, uses, and evaluates a neural network.

Complete Neural Network Code

First, look at the complete program. Don't worry if it looks complicated. We will break it down step by step.

import tensorflow as tf
import numpy as np


# --------------------------------
# 1. Training data
# --------------------------------

X_train = np.array([
    [1],
    [2],
    [3],
    [4],
    [5],
    [6],
    [7],
    [8]
])

y_train = np.array([
    0,
    0,
    0,
    1,
    1,
    1,
    1,
    1
])


# --------------------------------
# 2. Test data
# --------------------------------

X_test = np.array([
    [2],
    [4],
    [6],
    [8]
])

y_test = np.array([
    0,
    1,
    1,
    1
])


# --------------------------------
# 3. Create neural network
# --------------------------------

model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(1,)),

    tf.keras.layers.Dense(
        8,
        activation="relu"
    ),

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


# --------------------------------
# 4. Compile model
# --------------------------------

model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)


# --------------------------------
# 5. Train model
# --------------------------------

model.fit(
    X_train,
    y_train,
    epochs=20,
    verbose=0
)


# --------------------------------
# 6. Make predictions
# --------------------------------

predictions = model.predict(X_test)

print("Predictions:")
print(predictions)


# --------------------------------
# 7. Convert probabilities
#    into classes
# --------------------------------

classes = (
    predictions >= 0.5
).astype(int)

print("Classes:")
print(classes)


# --------------------------------
# 8. Evaluate model
# --------------------------------

loss, accuracy = model.evaluate(
    X_test,
    y_test,
    verbose=0
)

print("Test Loss:", loss)
print("Test Accuracy:", accuracy)

First Understand the Big Picture

Before understanding individual lines, understand what the program is doing.

Data
 ↓
Create Model
 ↓
Add Layers
 ↓
Compile
 ↓
Train
 ↓
Predict
 ↓
Evaluate

This is the complete neural-network workflow.

Think of it like teaching a student:

Training Data
     ↓
Student learns
     ↓
Give new questions
     ↓
Student answers
     ↓
Check answers
     ↓
Measure performance

A neural network follows the same basic idea.

1. Import TensorFlow

import tensorflow as tf

This imports TensorFlow so that we can use its neural network and machine-learning functionality.

The name tf is simply a short name for TensorFlow.

tf.keras
tf.keras.Sequential
tf.keras.layers.Dense

We use these TensorFlow/Keras classes to construct our neural network.

2. Import NumPy

import numpy as np

NumPy is commonly used for working with numerical data in Python.

Here we use it to create our training and test arrays.

X_train = np.array([
    [1],
    [2],
    [3]
])

This creates a NumPy array containing input values.

3. Understanding X_train

X_train = np.array([
    [1],
    [2],
    [3],
    [4],
    [5],
    [6],
    [7],
    [8]
])

X_train contains the input data used to train the model.

In this example, imagine the number represents the number of hours a student studied.

1 hour
2 hours
3 hours
4 hours
...
8 hours

Each row represents one training example.

4. Understanding y_train

y_train = np.array([
    0,
    0,
    0,
    1,
    1,
    1,
    1,
    1
])

y_train contains the correct answer for each training example.

We can imagine:

0 = Fail
1 = Pass

So the training data represents something like:

Study Hours     Result

1               0
2               0
3               0
4               1
5               1
6               1
7               1
8               1

The model will try to learn the relationship between the study hours and the result.

5. Understanding X_test and y_test

X_test = np.array([
    [2],
    [4],
    [6],
    [8]
])

y_test = np.array([
    0,
    1,
    1,
    1
])

This data is used to check how well the trained model performs.

The important point is that test data is separate from the data used to train the model.

X_train
    ↓
Used for learning


X_test
    ↓
Used for testing

6. Creating the Neural Network

model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(1,)),

    tf.keras.layers.Dense(
        8,
        activation="relu"
    ),

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

This creates our neural network.

We are using a Sequential model, which means the layers are connected one after another.

Input
  ↓
Hidden Layer
  ↓
Output Layer

7. Understanding Input(shape=(1,))

tf.keras.layers.Input(shape=(1,))

This tells the model that each input example contains one feature.

In our example, the single feature is:

Study Hours

So:

[6]

means one example with one feature.

8. Understanding the Hidden Layer

tf.keras.layers.Dense(
    8,
    activation="relu"
)

This creates a dense layer containing 8 neurons.

Input
  ↓
[ Neuron 1 ]
[ Neuron 2 ]
[ Neuron 3 ]
[ Neuron 4 ]
[ Neuron 5 ]
[ Neuron 6 ]
[ Neuron 7 ]
[ Neuron 8 ]
  ↓
Next Layer

The activation function is ReLU:

activation="relu"

ReLU helps the network learn non-linear relationships.

9. Understanding the Output Layer

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

This layer contains one output neuron.

Because this is binary classification, sigmoid is useful because it produces a value between 0 and 1.

0 ─────────────── 1
      Probability

For example:

0.15
0.42
0.87
0.95

These can be interpreted as the model's estimated probabilities for class 1.

10. Compiling the Model

model.compile(
    optimizer="adam",
    loss="binary_crossentropy",
    metrics=["accuracy"]
)

Compiling tells Keras how the model should be trained and what performance information should be calculated.

There are three important parts here.

optimizer
loss
metrics

11. Understanding optimizer="adam"

optimizer="adam"

Adam is an optimizer used to update the neural network's weights during training.

Prediction
    ↓
Calculate Loss
    ↓
Calculate Gradients
    ↓
Adam updates weights
    ↓
Better prediction

The optimizer is part of the learning process.

12. Understanding binary_crossentropy

loss="binary_crossentropy"

Because this is a binary classification problem, we use binary cross-entropy as the loss function.

Correct Answer
      +
Model Prediction
      ↓
Loss
      ↓
How wrong was the prediction?

During training, the optimizer uses the loss and its gradients to adjust the model's weights.

13. Understanding metrics=["accuracy"]

metrics=["accuracy"]

This tells Keras to calculate accuracy while training and evaluating the model.

For example:

Accuracy = 0.90

This means the model correctly classified approximately 90% of the evaluated examples.

14. Training the Model

model.fit(
    X_train,
    y_train,
    epochs=20,
    verbose=0
)

This is where the neural network actually learns from the training data.

X_train
   ↓
Model prediction
   ↓
Compare with y_train
   ↓
Calculate loss
   ↓
Backpropagation
   ↓
Update weights
   ↓
Repeat

The model repeats this process for 20 epochs.

15. What Does epochs=20 Mean?

epochs=20

One epoch means the model has gone through the training dataset once.

Therefore:

epochs=20

means approximately:

Training Dataset
      ↓
1st pass
      ↓
2nd pass
      ↓
...
      ↓
20th pass

More epochs do not automatically mean a better model. Too many epochs can cause overfitting.

16. Making Predictions

predictions = model.predict(X_test)

Now the model has been trained. We give it test data and ask it to make predictions.

X_test
   ↓
Trained Neural Network
   ↓
Predictions

Suppose the output is:

[[0.12]
 [0.68]
 [0.91]
 [0.97]]

These are probability-like outputs from the sigmoid output layer.

17. Converting Probabilities to Classes

classes = (
    predictions >= 0.5
).astype(int)

We use 0.5 as a classification threshold in this example.

0.12 >= 0.5 → 0
0.68 >= 0.5 → 1
0.91 >= 0.5 → 1
0.97 >= 0.5 → 1

So the final classes are:

[[0]
 [1]
 [1]
 [1]]

18. Evaluating the Model

loss, accuracy = model.evaluate(
    X_test,
    y_test,
    verbose=0
)

This checks how well the trained model performs on the test data.

X_test
   +
y_test
   ↓
model.evaluate()
   ↓
Loss + Accuracy

For example:

Test Loss: 0.21
Test Accuracy: 0.90

This tells us how well the model performed on the test examples.

19. Understand the Entire Program

import TensorFlow
        ↓
import NumPy
        ↓
Prepare training data
        ↓
Prepare test data
        ↓
Create neural network
        ↓
Add input layer
        ↓
Add hidden layer
        ↓
Add output layer
        ↓
Compile model
        ↓
Train model
        ↓
Make predictions
        ↓
Convert predictions
        ↓
Evaluate model

This is the part you should remember. You don't need to memorize every line immediately.

First understand what each stage is doing.

Example 1 — Student Pass Prediction

Suppose the input is study hours.

Input:
6 study hours

        ↓

Neural Network

        ↓

Prediction:
0.85

        ↓

0.85 >= 0.5

        ↓

Class:
1 = Pass

The network learned a relationship between study hours and the target during training.

Example 2 — Spam Detection

The same workflow can be used for spam detection.

Email Features
      ↓
Neural Network
      ↓
Prediction = 0.93
      ↓
0.93 >= 0.5
      ↓
1 = Spam

The actual input features would be much more complex than the simple study-hours example, but the neural-network workflow is the same.

20. The Most Important Lines to Remember

If you are learning Keras, these four lines are especially important:

# Create
model = tf.keras.Sequential([...])

# Compile
model.compile(...)

# Train
model.fit(...)

# Predict
model.predict(...)

# Evaluate
model.evaluate(...)

These functions form the basic workflow of building and using a Keras neural network.

21. Common Beginner Mistake

A common mistake is thinking that this:

model = tf.keras.Sequential([...])

means the model has already learned something.

It hasn't.

Create Model
    ↓
Architecture exists


model.fit()
    ↓
Model learns

Creating the network and training the network are two different things.

22. Another Common Mistake

Don't assume that a high training accuracy automatically means the model is good.

Training Accuracy = 99%
Test Accuracy     = 65%

This is a warning sign. The model may have learned the training examples too specifically.

That's why evaluation on unseen data matters.

23. Final Mental Model

DATA
 ↓
What examples do we have?


MODEL
 ↓
What neural-network structure
will learn from the data?


COMPILE
 ↓
How should the model learn?


FIT
 ↓
Learn the patterns


PREDICT
 ↓
What does the model think
about new data?


EVALUATE
 ↓
How well did the model perform?

If you understand this flow, you understand the basic Keras workflow.

Remember This

Data
 ↓
Create Model
 ↓
Compile
 ↓
Fit
 ↓
Predict
 ↓
Evaluate

The important Python functions are:

model.compile()
model.fit()
model.predict()
model.evaluate()

In simple words:

compile()
→ Prepare the learning process

fit()
→ Teach the model

predict()
→ Ask the model for an answer

evaluate()
→ Check how well it performs

Don't memorize the code blindly. Understand this workflow first. Once the workflow is clear, the individual Keras commands become much easier to remember.

QUICK CHECK

Check Your Understanding

What does model.fit() do?
It trains the neural network by learning from the training data and updating its weights.

What does model.predict() do?
It uses the trained model to produce predictions for input data.

What does model.evaluate() do?
It measures the model's performance using input data and the correct target values.

Why do we need an optimizer?
The optimizer helps update the model's weights during training so that the loss can decrease.

Why are we using sigmoid in the output layer?
Because this example is binary classification and sigmoid produces an output between 0 and 1.

Why do we split training and test data?
Training data is used for learning, while separate test data helps measure how well the trained model performs on unseen examples.