Build Artificial Neural Network in TensorFlow

Recently, I was working on a project that required predicting housing prices in the US market. After exploring various options, I decided to use an artificial neural network built with TensorFlow.

The problem was, I wasn’t sure where to start. TensorFlow offers numerous options, which can be overwhelming for beginners.

In this article, I’ll walk you through building your first artificial neural network using TensorFlow. I’ll share the exact steps I used and some best practices I’ve learned over my 10+ years as a Python developer.

Artificial Neural Network

An artificial neural network (ANN) is a computing system inspired by the human brain. It consists of interconnected nodes (neurons) that process information.

Neural networks are excellent for:

TensorFlow, developed by Google, is one of the most popular frameworks for building these networks.

Prerequisites for Building a Neural Network

Before we begin, make sure you have:

  • Python 3.6 or later installed
  • Basic understanding of Python
  • TensorFlow 2.x installed
  • NumPy and Matplotlib for data manipulation and visualization

You can install TensorFlow using pip:

pip install tensorflow

Method 1: Build a Simple Neural Network for Housing Price Prediction

Let’s start with a simple neural network to predict housing prices in California. This example will help you understand the basic structure.

Step 1: Import the necessary libraries

import tensorflow as tf
from tensorflow import keras
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

Step 2: Prepare the dataset

For this example, I’ll use a simplified version of the California housing dataset:

# Load and prepare data
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# Get California housing data
housing = fetch_california_housing()
X = housing.data
y = housing.target

# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Scale the features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

Read Compile Neural Network in Tensorflow

Step 3: Build the neural network model

# Build a simple neural network
model = keras.Sequential([
    keras.layers.Dense(64, activation='relu', input_shape=(X_train.shape[1],)),
    keras.layers.Dense(32, activation='relu'),
    keras.layers.Dense(1)
])

# Compile the model
model.compile(optimizer='adam', 
              loss='mean_squared_error',
              metrics=['mae'])

# Print model summary
model.summary()

In this model:

  • The first layer has 64 neurons with ReLU activation
  • The second layer has 32 neurons with ReLU activation
  • The output layer has 1 neuron (for price prediction)

Step 4: Train the model

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

Step 5: Evaluate the model

# Evaluate the model
loss, mae = model.evaluate(X_test, y_test, verbose=1)
print(f"Test MAE: ${mae * 100000:.2f}")

# Visualize training history
plt.figure(figsize=(10, 6))
plt.plot(history.history['mae'])
plt.plot(history.history['val_mae'])
plt.title('Model Mean Absolute Error')
plt.ylabel('MAE')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper right')
plt.show()

I executed the above example code and added the screenshot below.

tensorflow neural network

Check out Tensor in TensorFlow

Method 2: Build a Neural Network with Functional API

The Sequential API is great for simple models, but for more complex architectures, TensorFlow’s Functional API offers more flexibility.

# Build a model using the Functional API
inputs = keras.Input(shape=(X_train.shape[1],))
x = keras.layers.Dense(64, activation='relu')(inputs)
x = keras.layers.Dense(32, activation='relu')(x)
x = keras.layers.Dropout(0.2)(x)  # Add dropout for regularization
outputs = keras.layers.Dense(1)(x)

functional_model = keras.Model(inputs=inputs, outputs=outputs)

# Compile the model
functional_model.compile(
    optimizer=keras.optimizers.Adam(learning_rate=0.001),
    loss='mean_squared_error',
    metrics=['mae']
)

# Train the model
functional_model.fit(
    X_train, y_train,
    epochs=50,
    batch_size=32,
    validation_split=0.2,
    verbose=1
)

I executed the above example code and added the screenshot below.

tensorflow neural network example

The Functional API is especially useful when you want to:

  • Create models with multiple inputs or outputs
  • Share layers between different models
  • Create non-sequential layer connections

Read TensorFlow Variable

Method 3: Use Callbacks for Better Training

TensorFlow provides callbacks that can help you monitor and improve the training process:

# Define callbacks
early_stopping = keras.callbacks.EarlyStopping(
    monitor='val_loss',
    patience=10,
    restore_best_weights=True
)

model_checkpoint = keras.callbacks.ModelCheckpoint(
    'best_housing_model.h5',
    monitor='val_loss',
    save_best_only=True
)

# Train with callbacks
model = keras.Sequential([
    keras.layers.Dense(64, activation='relu', input_shape=(X_train.shape[1],)),
    keras.layers.Dense(32, activation='relu'),
    keras.layers.Dense(1)
])

model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae'])

history = model.fit(
    X_train, y_train,
    epochs=100,  # Set a higher number, early stopping will prevent overfitting
    batch_size=32,
    validation_split=0.2,
    callbacks=[early_stopping, model_checkpoint],
    verbose=1
)

I executed the above example code and added the screenshot below.

tensorflow ann

With these callbacks:

  • EarlyStopping prevents overfitting by stopping training when validation loss stops improving
  • ModelCheckpoint saves the best model during training

Read Tensorflow Convert String to Int

Fine-tuning Your Neural Network

After building your basic model, you might want to improve its performance. Here are some techniques I’ve found helpful:

Adjusting Learning Rate

# Create a learning rate scheduler
def scheduler(epoch, lr):
    if epoch < 10:
        return lr
    else:
        return lr * tf.math.exp(-0.1)

lr_scheduler = keras.callbacks.LearningRateScheduler(scheduler)

# Use the scheduler during training
model.fit(
    X_train, y_train,
    epochs=50,
    batch_size=32,
    validation_split=0.2,
    callbacks=[lr_scheduler],
    verbose=1
)

Adding Regularization

model = keras.Sequential([
    keras.layers.Dense(64, activation='relu', 
                     kernel_regularizer=keras.regularizers.l2(0.001),
                     input_shape=(X_train.shape[1],)),
    keras.layers.Dense(32, activation='relu', 
                     kernel_regularizer=keras.regularizers.l2(0.001)),
    keras.layers.Dense(1)
])

Adding L2 regularization helps prevent overfitting by penalizing large weights.

Batch Normalization

model = keras.Sequential([
    keras.layers.Dense(64, input_shape=(X_train.shape[1],)),
    keras.layers.BatchNormalization(),
    keras.layers.Activation('relu'),
    keras.layers.Dense(32),
    keras.layers.BatchNormalization(),
    keras.layers.Activation('relu'),
    keras.layers.Dense(1)
])

Batch normalization helps stabilize and accelerate training by normalizing the inputs to each layer.

Read TensorFlow Fully Connected Layer

Making Predictions with Your Model

Once your model is trained, you can use it to make predictions:

# Make predictions on test data
predictions = model.predict(X_test)

# Sample prediction for a house in California
sample_house = X_test[0].reshape(1, -1)
predicted_price = model.predict(sample_house)[0][0]
actual_price = y_test[0]

print(f"Predicted price: ${predicted_price * 100000:.2f}")
print(f"Actual price: ${actual_price * 100000:.2f}")

Saving and Loading Your Model

One of the advantages of TensorFlow is the ability to save your trained model and load it later:

# Save the model
model.save('california_housing_model')

# Load the model later
loaded_model = keras.models.load_model('california_housing_model')

# Verify the loaded model works
test_loss, test_mae = loaded_model.evaluate(X_test, y_test)
print(f"Loaded model MAE: ${test_mae * 100000:.2f}")

Check out Batch Normalization TensorFlow

Common Issues and Solutions

Throughout my experience building neural networks, I’ve encountered several common issues:

Overfitting

If your model performs well on training data but poorly on test data, it’s likely overfitting.

Solutions:

  • Add dropout layers
  • Use regularization
  • Collect more training data
  • Simplify your model

Vanishing/Exploding Gradients

This happens when gradients become too small or too large during backpropagation.

Solutions:

  • Use ReLU activation functions
  • Apply batch normalization
  • Initialize weights properly
  • Use gradient clipping
# Using gradient clipping
optimizer = keras.optimizers.Adam(clipnorm=1.0)
model.compile(optimizer=optimizer, loss='mean_squared_error', metrics=['mae'])

Building neural networks with TensorFlow is a powerful way to solve complex problems like housing price prediction. The process involves preparing your data, designing the network architecture, training the model, and fine-tuning it for optimal performance.

I’ve shared the methods that have worked best for me over the years of experience. Whether you’re predicting housing prices, classifying images, or solving other complex problems, the principles remain the same.

If you have any questions or suggestions, please leave them in the comments below. Happy coding!

Other articles you may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.