Last modified: Jan 07, 2026 By Alexander Williams
Getting Started with Neural Networks in Python
Neural networks power modern AI. They are inspired by the human brain.
You can build them in Python. This guide will show you how.
We will use TensorFlow and Keras. These are popular libraries.
What is a Neural Network?
A neural network is a series of algorithms. It finds patterns in data.
It consists of layers of nodes. These nodes are called neurons.
Data flows from input to output layers. Hidden layers do the processing.
Setting Up Your Python Environment
First, you need Python installed. Version 3.8 or newer is best.
Create a virtual environment. This keeps your project tidy.
Then, install the necessary libraries. Use pip for installation.
# Install TensorFlow and Keras
pip install tensorflow
TensorFlow includes Keras. Keras makes building models simple.
For a broader context, see our Deep Learning with Python Guide.
Your First Neural Network
We will solve a classic problem. We will classify handwritten digits.
We use the MNIST dataset. It has 70,000 images of digits.
Our goal is to build a model. It will recognize these digits.
Step 1: Import Libraries
Start by importing the modules you need. We import from TensorFlow.
import tensorflow as tf
from tensorflow import keras
import numpy as np
Step 2: Load and Prepare Data
Keras has built-in datasets. Load the MNIST data easily.
We split it into training and testing sets. Then we normalize the data.
# Load the MNIST dataset
mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Normalize pixel values to be between 0 and 1
train_images = train_images / 255.0
test_images = test_images / 255.0
Step 3: Build the Model Architecture
Now, define the layers of your network. We use the Sequential model.
We add a Flatten layer first. It converts the 2D image to 1D.
Then we add Dense layers. These are fully connected layers.
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)), # Input layer
keras.layers.Dense(128, activation='relu'), # Hidden layer
keras.layers.Dense(10, activation='softmax') # Output layer
])
The Dense function creates a layer. The 'relu' activation is common.
The output layer has 10 neurons. They correspond to digits 0-9.
Step 4: Compile the Model
Before training, you must compile the model. This configures the learning process.
You specify an optimizer, a loss function, and metrics.
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Step 5: Train the Model
Training is done with the fit method. The model learns from the data.
We train for 5 epochs. An epoch is one pass through the data.
model.fit(train_images, train_labels, epochs=5)
You will see output like this during training.
Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2580 - accuracy: 0.9265
Epoch 2/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.1136 - accuracy: 0.9666
Epoch 3/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0776 - accuracy: 0.9766
Epoch 4/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0585 - accuracy: 0.9822
Epoch 5/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0452 - accuracy: 0.9860
Step 6: Evaluate the Model
Check how well the model performs. Use the test dataset for this.
The evaluate function returns the loss and accuracy.
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(f'\nTest accuracy: {test_acc}')
313/313 - 0s - loss: 0.0760 - accuracy: 0.9761
Test accuracy: 0.9761000275611877
Our simple model achieves over 97% accuracy. That's a great start.
Key Concepts to Understand
Layers are the building blocks. They transform input data.
Activation Functions like 'relu' add non-linearity. This is crucial.
Optimizers like 'adam' adjust model weights. They reduce loss.
Loss Functions measure model error. The model tries to minimize this.
Epochs are training iterations. More epochs can mean better learning.
Next Steps and Further Learning
You built a basic neural network. This is just the beginning.
You can explore more complex architectures. Convolutional networks are next.
For a detailed framework tutorial, read our Intro to Deep Learning with TensorFlow Keras.
You can apply these concepts to other fields. For example, chemistry uses tools like DeepChem.
Common Pitfalls for Beginners
Do not use too little data. Neural networks need lots of data.
Avoid overfitting. Your model might memorize data instead of learning.
Always normalize your input data. This helps the training process.
Start simple. A complex model is not always better.
Conclusion
You have taken your first step. You built a neural network in Python.
You used TensorFlow and Keras. You trained it on the MNIST dataset.
The process is straightforward. Define, compile, fit, and evaluate.
Keep experimenting with different layers and parameters. Practice is key.
The world of deep learning is vast and exciting. Your journey has just begun.