Introduction to Neural Networks with TensorFlow

Introduction to Neural Networks with TensorFlow

Neural networks are computational models inspired by the structure of biological neurons. They consist of layers of interconnected nodes (neurons) that can learn patterns from data through a process called training. TensorFlow, Google’s machine learning framework, provides a comprehensive ecosystem for building, training, and deploying neural networks. This article walks through building your first neural network with TensorFlow’s Keras API.

The Basic Building Blocks

A neural network consists of an input layer (receiving raw data), hidden layers (where computation happens), and an output layer (producing the prediction). Each connection between neurons has a weight, and each neuron has a bias. During training, the network adjusts these weights and biases to minimize the difference between its predictions and the actual target values. The Keras Sequential API makes this intuitive—you stack layers one after another.

import tensorflow as tf
from tensorflow.keras import layers, models

# Build a simple feedforward network
model = models.Sequential([
    layers.Dense(64, activation='relu', input_shape=(784,)),
    layers.Dropout(0.3),
    layers.Dense(32, activation='relu'),
    layers.Dropout(0.3),
    layers.Dense(10, activation='softmax')  # 10 classes
])

model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)
model.summary()

Activation Functions

Activation functions introduce non-linearity into the network, allowing it to learn complex patterns. Without them, stacking linear layers would be equivalent to a single linear layer. ReLU (Rectified Linear Unit, f(x) = max(0, x)) is the most popular hidden layer activation because it avoids the vanishing gradient problem and is computationally efficient. The sigmoid function (f(x) = 1/(1+e^-x)) squashes values between 0 and 1, making it suitable for binary classification outputs. Softmax generalizes this to multi-class problems by converting raw scores into probabilities that sum to 1.

Training the Network

Training uses backpropagation: the forward pass computes predictions and the loss (error), and the backward pass calculates gradients of the loss with respect to each weight using the chain rule. The optimizer (Adam is the most common) updates weights in the direction that reduces loss. The learning rate controls step size—too large and training diverges; too small and training is impractically slow. Typical learning rates range from 1e-3 to 1e-5 depending on the problem.

# Load and preprocess data
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train = x_train.reshape(-1, 784).astype('float32') / 255.0
x_test = x_test.reshape(-1, 784).astype('float32') / 255.0

# Train the model
history = model.fit(
    x_train, y_train,
    batch_size=32,
    epochs=15,
    validation_split=0.2,
    verbose=1
)

# Evaluate on test data
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Test accuracy: {test_acc:.4f}")

Overfitting and Regularization

Overfitting occurs when the model memorizes the training data but fails to generalize to new data. Dropout (randomly disabling neurons during training) and weight decay (L2 regularization) are common techniques to prevent this. Early stopping (monitoring validation loss and stopping when it stops improving) is another practical approach. The goal is a model that performs well on both training and validation data, indicating genuine pattern learning rather than memorization.

Transfer Learning with Pre-Trained Models

Training a neural network from scratch requires substantial data and compute. Transfer learning reuses a pre-trained model (trained on ImageNet’s 1.2 million images) and adapts it to your specific task. You freeze the early layers (which detect universal features like edges and textures), replace the classification head, and train only the new layers on your data. This approach achieves state-of-the-art results with as few as 100 images per class. TensorFlow Hub and PyTorch Hub provide downloadable pre-trained models (ResNet, EfficientNet, MobileNet) that you can integrate in a few lines of code. Fine-tuning (unfreezing later layers with a low learning rate) further improves performance when your dataset differs significantly from ImageNet.

from tensorflow.keras.applications import MobileNetV2

base = MobileNetV2(weights='imagenet', include_top=False, input_shape=(224,224,3))
base.trainable = False

model = tf.keras.Sequential([
    base,
    tf.keras.layers.GlobalAveragePooling2D(),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

Convolutional Neural Networks for Images

While dense networks work for simple datasets, real-world image classification requires convolutional layers. CNNs use filters that slide over the input image, detecting local patterns like edges, textures, and shapes. Each convolutional layer learns increasingly complex features—early layers detect edges and color blobs, middle layers detect shapes and patterns, and final layers detect complete objects. Pooling layers reduce spatial dimensions, providing translation invariance. TensorFlow’s Keras API provides Conv2D, MaxPooling2D, and Flatten layers that stack to form a CNN. Pre-trained models like ResNet50 and MobileNetV2 provide state-of-the-art accuracy through transfer learning on the ImageNet dataset.

Training Tips and Common Issues

When training neural networks, watch for: loss not decreasing (learning rate too high or too low, data not normalized), loss plateauing (model capacity insufficient, try more neurons or layers), loss diverging (learning rate too high, gradient clipping needed), and overfitting (training loss much lower than validation loss, add dropout or reduce model size). Use learning rate schedulers (ReduceLROnPlateau reduces LR when validation loss plateaus) and early stopping to prevent overfitting automatically. TensorBoard provides real-time visualization of training curves, weight distributions, and gradient histograms. Start with a simple model that overfits a small sample of data, then add regularization and increase data to achieve generalization. This iterative approach is faster than building a complex model from the start.

Leave a Reply

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