Building First Neural Network
Building Your First Neural Network
A complete beginner-friendly guide to designing, training, and evaluating your very first Deep Learning model.
Introduction
Building your first Neural Network is one of the most exciting steps in your Deep Learning journey. In this tutorial, you'll learn how to create, train, and evaluate a Neural Network using Keras and TensorFlow.
Project Goal
We will create a Neural Network that learns to classify whether a number is even or odd based on input data. This simple example demonstrates the full Deep Learning pipeline.
- Input → Single numeric feature
- Output → 0 (even) or 1 (odd)
- Goal → Train a model to predict correctly
Prerequisites
- Python 3.x
- NumPy
- TensorFlow / Keras
- Jupyter Notebook or any Python IDE
pip install tensorflow numpy
Neural Network Building Workflow
Steps
- Import libraries
- Prepare the dataset
- Build the model architecture
- Compile the model
- Train the model
- Evaluate the performance
- Make predictions
Step 1 — Import Required Libraries
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
Step 2 — Prepare the Dataset
We'll create a small dataset to predict even (0) and odd (1) numbers.
X = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10, 11], dtype=float)
y = np.array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1], dtype=float)
print("Features:", X)
print("Labels:", y)
Step 3 — Build the Neural Network
Now we'll build a simple Neural Network with two layers:
model = Sequential([
Dense(8, activation="relu", input_shape=(1,)),
Dense(1, activation="sigmoid")
])
model.summary()
Layer 1
- 8 neurons
- ReLU activation
- Input shape: 1 feature
Output Layer
- 1 neuron
- Sigmoid activation
- Predicts probability
Step 4 — Compile the Model
The compile step prepares the model for training.
model.compile(
optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"]
)
Step 5 — Train the Model
model.fit(X, y, epochs=200, verbose=0)
print("Training Complete!")
Step 6 — Evaluate the Model
loss, accuracy = model.evaluate(X, y, verbose=0)
print(f"Accuracy: {accuracy * 100:.2f}%")
Step 7 — Make Predictions
predictions = model.predict(np.array([3, 8, 15]))
print(predictions)
- Values close to 1 → Odd
- Values close to 0 → Even
Full Code — Building Your First Neural Network
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Step 1: Prepare data
X = np.array([2, 3, 4, 5, 6, 7, 8, 9, 10, 11], dtype=float)
y = np.array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1], dtype=float)
# Step 2: Build model
model = Sequential([
Dense(8, activation="relu", input_shape=(1,)),
Dense(1, activation="sigmoid")
])
# Step 3: Compile
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
# Step 4: Train
model.fit(X, y, epochs=200, verbose=0)
# Step 5: Evaluate
loss, accuracy = model.evaluate(X, y, verbose=0)
print(f"Accuracy: {accuracy * 100:.2f}%")
# Step 6: Predict
predictions = model.predict(np.array([3, 8, 15]))
print("Predictions:", predictions)
Save and Load Your Model
model.save("first_neural_network.h5")
from tensorflow.keras.models import load_model
loaded_model = load_model("first_neural_network.h5")
Behind the Scenes — What Happens?
| Step | What Happens |
|---|---|
| Forward Pass | Predict output using current weights |
| Loss Calculation | Measure error between prediction & truth |
| Backward Pass | Adjust weights using backpropagation |
| Update Weights | Optimizer updates parameters |
| Repeat | Continues until model becomes accurate |
Visual Idea
Input
- Numbers
Hidden Layer
- 8 neurons
- ReLU activation
Output
- 0 or 1
Real-Life Analogy
Neural Network = Student Learning Math
Think of your model as a student. At first, it guesses randomly. After many practice problems (epochs), the student improves and learns the patterns — just like your model.
When to Add More Layers?
- When the problem becomes complex.
- When patterns are non-linear.
- When working with images, audio, or text.
- When more data is available.
Real-World Use Cases of Neural Networks
Computer Vision
- Face detection
- Image classification
Speech Recognition
- Siri, Alexa
- Voice typing
Natural Language Processing
- Chatbots
- Translation
Healthcare
- Disease prediction
- Medical imaging
Finance
- Stock prediction
- Fraud detection
Self-Driving Cars
- Object detection
- Lane recognition
Advantages of This Approach
- Easy to build with Keras.
- Trains quickly on simple data.
- Helps you understand Deep Learning basics.
- Reusable in larger projects.
- Strong foundation for future models.
Common Mistakes to Avoid
Best Practices
Quick Tips
- Start with simple problems.
- Use small architectures first.
- Normalize input data.
- Monitor accuracy & loss.
- Use GPU when datasets grow.
- Always test with new data.
Importance of Building Your First Neural Network
Strong Foundation
- Builds understanding
- Required for advanced models
Quick Start
- Fast to implement
- No advanced math needed
Confidence Booster
- Practical Deep Learning
- Step into AI world
Career Impact
- Helps in AI job interviews
- Industry-relevant skill
Golden Rule
Key Takeaway
Congratulations! You've built your first Neural Network using Keras and TensorFlow. This simple example introduces the key building blocks of Deep Learning — data preparation, model design, training, and evaluation. With these fundamentals, you're ready to dive deeper into more advanced AI models like CNNs, RNNs, and Transformers.