BasicTensorboardFromPrinciples and Labs for Deep Learning
BasicTensorboardFromPrinciples and Labs for Deep Learning
4 Introduction to TensorBoard 43
4. The average percentage error on test data
Predict house price on test data and calculate the average percentage error.
#Load model
model.load_weights('lab2-logs/models/Best-model-1.h5')
#take out the house price
y_test = np.array(test_data['price'])
# data norm alization
test_data = (test_data - mean) / std
# Save the input data in Numpy format
x_test = np.array(test_data.drop('price', axis='columns'))
# Predict on test data
y_pred = model.predict(x_test)
# Convert the prediction results back
y_pred = np.reshape(y_pred * std['price'] + mean['price'], y_test.shape)
# Calculate the mean percentage error
percentage_error = np.mean(np.abs(y_test - y_pred)) / np.mean(y_test) * 100
# Display percentage error
print(" Model_1 Percentage Error: { :.2f } %" .format(percentage_error))
Result: Model_1 Percentage Error: 14.08%
▪ The Scalars dashboard: helps to track scalar values such as learning rate, loss, accuracy, and so on during training
neural networks
▪ Graphs dashboard: helps to visualize the models built by TensorFlow
▪ The Distributions and Histograms dashboards: help to display the distribution of the tensor. They are widely used
for visualizing weights and biases of the TensorFlow models
The advantages and disadvantages of TensorBoard include:
▪ Advantages: The information during training the model such as changes in the loss, accuracy, the histograms of
weights, biases, and so on, can be tracked and viewed in real time, without having to wait until the training is
completed.
▪ Disadvantages: The information will be written to the log file many times during training the model. If a lot of
information is recorded, training time is increased.
When training the house price-prediction model, the TensorBoard callback function, namely, “keras.callbacks.
TensorBoard,” has been added for creating and storing the log. There are two ways to open the log file. The first
way is to directly open the log file on Jupyter Notebook, and the second way is to run TensorBoard through a terminal
and then observe results through the browser.
▪ Open log file with Jupyter Notebook (results are shown in Fig. 2.15)
- The port number can be specified for displaying the result; following the command below, the result can be
observed through URL: http://localhost:9527/, as shown in Fig. 2.16.
In addition to metrics such as loss and accuracy, the model graph is also visualized, as shown in Fig. 2.17. We discuss
the other visualization functions of TensorBoard such as Images, Text, Audio, and so on in Chapter 7.
46 2. Neural networks
Error
Validation error
optimism
Training error
optimal C apacity
C apacity
FIG. 2.18 Overfitting phenomenon.
The training result of the house price-prediction model in Section 2.3 is shown in Fig. 2.19, and the overfitting
phenomenon can also be observed from the loss curve graph.