Deep Learning Building Your First Neural Network

Now that you understand neurons and data preparation, it is time to build something. This topic walks you through designing, coding, and training a simple neural network — step by step, with no experience required.

The Problem: Predicting House Prices

You will build a neural network that predicts whether a house is expensive or affordable, based on two inputs: the number of rooms and the size in square feet. This is a real task that mortgage companies and real estate platforms solve using similar models.

The Dataset

RoomsSize (sq ft)Price Category
2800Affordable
41800Expensive
31200Affordable
52500Expensive

Step 1: Design the Network Architecture

You decide how many layers and how many neurons per layer before writing any code. This design is called the network's architecture.

Your Network Design

INPUT LAYER        HIDDEN LAYER        OUTPUT LAYER
  2 neurons           4 neurons           1 neuron

[Rooms]  ─────┬──→ [H1] ──┬──→ [Output]
              │            │    (0=Affordable
[Size]   ─────┼──→ [H2] ──┤     1=Expensive)
              │            │
              ├──→ [H3] ──┤
              │            │
              └──→ [H4] ──┘
  • 2 input neurons — one for rooms, one for size
  • 4 hidden neurons — to learn patterns in the data
  • 1 output neuron — to give a prediction (0 or 1)

Step 2: Write the Code

Python and Keras make it straightforward to build this in just a few lines. You do not need to calculate weights manually — the library handles all the math.

import numpy as np
from tensorflow import keras
from tensorflow.keras import layers

# Step 1: Prepare data
X = np.array([[2, 800], [4, 1800], [3, 1200], [5, 2500]])
y = np.array([0, 1, 0, 1])  # 0=Affordable, 1=Expensive

# Normalize inputs to 0–1 range
X = X / X.max(axis=0)

# Step 2: Build the model
model = keras.Sequential([
    layers.Dense(4, activation='relu', input_shape=(2,)),  # Hidden layer
    layers.Dense(1, activation='sigmoid')                   # Output layer
])

# Step 3: Compile the model
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Step 4: Train the model
model.fit(X, y, epochs=100, verbose=0)

# Step 5: Make a prediction
new_house = np.array([[3, 1500]]) / X.max(axis=0)
prediction = model.predict(new_house)
print("Prediction score:", prediction[0][0])

Step 3: Understand Each Part

keras.Sequential

Sequential means layers stack one after another — the output of layer 1 feeds into layer 2. It is like a factory assembly line where each station does one job before passing the product forward.

Dense Layer

A Dense layer connects every neuron in the current layer to every neuron in the next layer. "Dense" means fully connected — no shortcuts.

activation='relu'

ReLU stands for Rectified Linear Unit. It is the most common activation function used in hidden layers. You will learn more about it in the Activation Functions topic.

activation='sigmoid'

Sigmoid squishes any number into a value between 0 and 1 — perfect for outputting a probability. A result of 0.85 means the model is 85% confident the house is expensive.

model.compile

This step configures how the model trains. You tell it three things:

  • Optimizer — the strategy used to improve weights (adam is a popular, reliable choice)
  • Loss — the formula that measures how wrong the model is
  • Metrics — what score to track (here, accuracy)

model.fit

This runs the actual training. Epochs = how many times the model sees the entire dataset. 100 epochs means it looks at the data 100 times, improving a little each pass.

Step 4: Read the Output

Training Progress (sample output)

Epoch 1/100  – loss: 0.72  – accuracy: 0.50
Epoch 50/100 – loss: 0.41  – accuracy: 0.75
Epoch 100/100 – loss: 0.18 – accuracy: 1.00

The loss goes down. The accuracy goes up. That is the model learning. A loss near zero means the model's predictions are very close to the correct answers.

Prediction Output

Input: 3 rooms, 1500 sq ft
Prediction score: 0.38
→ Score below 0.5 → Classified as "Affordable"

What the Model Learned

The model discovered — on its own — that larger size and more rooms push the score above 0.5, meaning "Expensive." Nobody told the model this rule. It found the pattern by adjusting weights over 100 training epochs.

Weight Adjustment Diagram

Epoch 1:   Weights are random → Prediction is often wrong
Epoch 50:  Weights improve → Prediction is mostly right
Epoch 100: Weights are well-tuned → Prediction is accurate

Common First-Time Mistakes

  • Forgetting to normalize — large numbers in one column dominate all others
  • Too few epochs — model does not have enough time to learn
  • Too many epochs — model memorizes the training data instead of learning patterns
  • Wrong loss function — different problems need different loss formulas

Key Terms

  • Architecture — the number and arrangement of layers and neurons
  • Dense Layer — a fully connected layer where every neuron links to every other
  • Epoch — one complete pass through the entire training dataset
  • Compile — the setup step that tells the model how to train
  • Loss — a measurement of how wrong the model's predictions are

Leave a Comment

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