Welcome to the practical implementation guide of our Deep Learning Illustrated series. In this series, we’ll bridge the gap between theory and application, bringing to life the neural network concepts explored in previous articles.
Remember the simple neural network we discussed for predicting ice cream revenue? We will build that using TensorFlow, a powerful tool for creating neural networks.
Deep Learning Illustrated, Part 2: How Does a Neural Network Learn?
And the kicker: we’ll do it in less than 5 minutes with just 27 lines of code!
Let’s first start with: what is TensorFlow?
TensorFlow is a comprehensive ecosystem of tools, libraries, and community resources for building and deploying machine learning applications. Developed by Google, it’s designed to be flexible and efficient, capable of running on various platforms from CPUs to GPUs and even specialized hardware like TPUs. The name "TensorFlow" derives from its core concept: tensor flow. Tensors, which are multi-dimensional arrays, flow through a computational graph during the training and inference processes.
Okay, let’s get to building our neural network. The goal of the model is to predict daily ice cream revenue based on two features: temperature and day of the week. We’ll approach this task step-by-step, explaining each component of the process.
Step 1: Data preparation
First, we’ll translate the ice cream sales data that we used previously…

…into a format suitable for our neural network:
import numpy as np
# Data
day = [2, 6, 1, 3, 2, 5, 7, 4, 3, 1]
temperature = [22, 33, 20, 25, 24, 30, 35, 28, 26, 21]
revenue = [1.51, 2.22, 1.37, 1.77, 1.64, 2.04, 2.42, 1.90, 1.75, 1.45]
# Combine day and temperature into a single feature array
X_train = np.column_stack((day, temperature))
y_train = np.array(revenue)
This creates our input features, X_train
…

…and target values, y_train
:

Step 2: Standardize the data
Next, we’ll standardize our data. Standardization is a crucial preprocessing step that transforms features to have a mean of zero and a standard deviation of one.
from sklearn.preprocessing import StandardScaler
# Standardize the features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
This ensures all features contribute equally to the model, improving convergence speed and stability during training.
Step 3: Build the Neural Network
In this step, we define our neural network model. We decided previously that the architecture consists of one hidden layer with two neurons and one output neuron all using the ReLU activation.

Let’s stick to this same architecture and translate the above to code. We construct our neural network using TensorFlow’s Keras API.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Initialize the model
model = Sequential()
# Add hidden layer - 2 neurons with the ReLU activation function with 2 inputs
model.add(Dense(2, input_dim=2, activation='relu'))
# Add output layer - 1 neuron with the ReLU activation function
model.add(Dense(1, activation='relu'))
The Sequential
model allows us to build a stack of layers. The Dense
layers are fully connected layers where each neuron in one layer is connected to every neuron in the next layer.
Step 4: Compile and train the model
Before training, we need to compile our model:
# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')
Compilation is a crucial step that configures the learning process. Here, we specify:
- The optimizer: adam (Adaptive Moment Estimation), which adapts the learning rate during training.
- The loss function: Mean Squared Error (MSE), which measures the average squared difference between predictions and actual values.
_Note: Here we used the Adam optimizer, but we can used any other optimization algorithm that makes sense. This lists all the ones we can use in TensorFlow. And similarly we can use any of the various loss functions defined here._
Now we can train our model:
# Train the model
history = model.fit(X_train_scaled, y_train, epochs=100, verbose=1)
The fit
method is where the actual learning occurs. We specify our input features (X_train_scaled
), target values (y_train
), and the number of training cycles (epochs). The verbose
parameter controls the level of output during training.
We can visualize the training process:
import matplotlib.pyplot as plt
# Plot training loss over epochs
plt.plot(history.history['loss'])
plt.title('Model Training Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.show()

This plot illustrates how our loss (prediction error) decreases over time, providing insight into the learning process.
Step 5: Make predictions
Finally, we can use our trained model to make predictions:
from sklearn.metrics import mean_squared_error
# Make predictions on the training data
predictions = model.predict(X_train_scaled)
print("Predicted Revenues on Training Data:", predictions)

Here, we use our trained model to predict ice cream sales based on our input features. And if we want to see how accurate the predictions were, we can use the MSE to measure of our model’s accuracy.
# Calculate the Mean Squared Error
mse = mean_squared_error(y_train, predictions)
print("Mean Squared Error on Training Data:", mse)

The MSE isn’t as low as we’d hoped, but that’s okay. This is a super basic neural network, and the whole point is to add complexity and tweak the architecture to improve our results.
While this example uses a simple dataset and model architecture, the principles we’ve covered lay the groundwork for more complex neural network applications. As we continue our journey in Deep Learning, we’ll encounter more sophisticated architectures and larger datasets, but the fundamental process remains the same.
Bonus: PyTorch
Now that we’ve seen how to implement our model in TensorFlow, let’s take a look at how we can achieve the same results using another powerful framework: PyTorch. PyTorch, developed by Facebook’s AI Research lab, is known for its flexibility and efficiency, making it a popular choice as well.
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
# Data
day = [2, 6, 1, 3, 2, 5, 7, 4, 3, 1]
temperature = [22, 33, 20, 25, 24, 30, 35, 28, 26, 21]
revenue = [1.51, 2.22, 1.37, 1.77, 1.64, 2.04, 2.42, 1.90, 1.75, 1.45]
# Convert to numpy array
X = np.array(list(zip(day, temperature)), dtype=np.float32)
y = np.array(revenue, dtype=np.float32)
# Standardize the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Convert data to PyTorch tensors
X_tensor = torch.tensor(X_scaled, dtype=torch.float32)
y_tensor = torch.tensor(y, dtype=torch.float32).view(-1, 1)
# Build the neural network
class IceCreamSalesModel(nn.Module):
def __init__(self):
super(IceCreamSalesModel, self).__init__()
self.hidden = nn.Linear(2, 2)
self.output = nn.Linear(2, 1)
self.relu = nn.ReLU()
def forward(self, x):
x = self.relu(self.hidden(x))
x = self.output(x)
return x
model = IceCreamSalesModel()
# Define loss function and optimizer
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.01)
# Train the model
num_epochs = 100
losses = []
for epoch in range(num_epochs):
# Forward pass
predictions = model(X_tensor)
loss = criterion(predictions, y_tensor)
losses.append(loss.item())
# Backward pass and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Plot training loss
plt.plot(losses)
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Training Loss')
plt.show()
# Make predictions
with torch.no_grad():
predictions = model(X_tensor)
print("Predicted Revenue:", predictions.numpy())
# Evaluate the model
mse_value = criterion(predictions, y_tensor).item()
print("Mean Squared Error:", mse_value)
And that’s all! We’ve learned how to implement a simple neural network to predict ice cream sales using both TensorFlow and PyTorch. In the next article, we’ll cover how to implement a Convolutional Neural Network (CNN) in both frameworks.
As always, feel free to connect with me on LinkedIn for any comments/questions!
Note: Unless specified, all images are by the author.