Cookbook for basic steps to create a regression model with TensorFlow.

  • Create some sample data
  • setup a model
  • measure performance
  • visualize train, test and predicted data
  • evaluate model
Data

Import

Python
import tensorflow as tf
import matplotlib.pyplot as plt

Create some sample data.

Python
# create some data 
X = tf.range (-100, 100, 4)
Y = X + 10

# Calculate the splitting index for 80% training and 20% testing
split_index = int(len(X)*0.8)

# Splitting the data into training and testing sets
train_data = X[:split_index]
train_label = Y[:split_index]
test_data = X[split_index:]
test_label = Y[split_index:]
Model

Setup/configure your model

Python
# set seed
tf.random.set_seed(42)

# create
model_1 = tf.keras.models.Sequential([
    tf.keras.layers.Dense(1, name="output_layer")
    ], name="model_1")

# compile

model_1.compile(
    loss        = tf.keras.losses.mae,
    optimizer   = tf.keras.optimizers.SGD(),
    metrics     = ['mae']
)

# fit
train_data_expand = tf.expand_dims(train_data, axis=-1)

model_1_history = model_1.fit(train_data_expand, train_label, epochs=100)

# predict
test_data_expand = tf.expand_dims(test_data, axis = -1)

predict_label_1 = model_1.predict(test_data_expand)

Some notes about hyper parameters

Hyper parameterTypical value
Input layer shapeSame shape as number of features (e.g. 3 for # bedrooms, # bathrooms, # car spaces in housing price prediction) -> shape()
Hidden layer(s)Problem specific, minimum = 1, maximum = unlimited
Neurons per hidden layerProblem specific, generally 10 to 100 -> Dense()
Output layer shapeSame shape as desired prediction shape (e.g. 1 for house price)
Hidden layer activationUsually ReLU(rectified linear unit)
Output activationNone,ReLU, logistic/tanh
Loss functionMSE (mean spare error) or MAE (mean absolute error) / Huber (combination of MAE/MSE) if outliers
OptimizerSGD (stochastic gradient descent), Adam
Visualization – prediction
Python
# Initialize the figure 
plt.figure(figsize=(7, 10))
    
# Plot training data in blue
plt.scatter(train_data, train_label, color="b", 
            label="training data")
    
# Plot testing data in green
plt.scatter(test_data, test_label, color="g", label="test data")

# Plot model's predictions in red
plt.scatter(test_data, predict_label_1, color="r",
            label="predictions")

# Display the legend
plt.legend()

# Show the plot
plt.show()
Measure performance
Python
# mean_absolute_error
tf.keras.metrics.mean_absolute_error(
                      test_label, 
                      tf.squeeze(predict_label_1))

# mean_squared_error
tf.keras.metrics.mean_squared_error(
                      test_label, 
                      tf.squeeze(predict_label_1))
Evaluate
Python
model_1.evaluate(test_data, test_label)

Evaluate is a method available in TensorFlow’s Kreas API.

  • model.evaluate(x, y): This method computes the loss based on the input you pass it, along with any other metrics that are requested in the metrics parameter when the model will be compiled, such as mae in this example.
    • x is the input data (the testing data set).
    • y is the true labels for x.
  • The method returns the loss value and metric values for the model in test mode.

Loss value: This is a measure of the model’s error on the test data. A low loss signifies that the model is performing well.

Metrics Values: These are the evaluation metrics that were specified when the model was compiled. These could be metrics like accuracy, precision, recall, mae etc. The actual metrics returned would depend on what metrics were passed to the model.compile() function during the model’s compilation. If mae was specified as a metric, for example, the function will return the mae of the model on the test data.

Visualize – training loss per epoch
Python
pd.DataFrame(model_1_history.history).plot()
plt.ylabel("loss")
plt.xlabel("epoch")
Links