2

I'm getting an error while creating a log file using TensorBoard with Keras.

The code

import pandas as pd
from keras.callbacks import TensorBoard
from keras.models import Sequential
from keras.layers import *

training_data_df = pd.read_csv("sales_data_training_scaled.csv")

X = training_data_df.drop('total_earnings', axis=1).values
Y = training_data_df[['total_earnings']].values

# Define the model
model = Sequential()
model.add(Dense(50, input_dim=9, activation='relu', name='layer_1'))
model.add(Dense(100, activation='relu', name='layer_2'))
model.add(Dense(50, activation='relu', name='layer_3'))
model.add(Dense(1, activation='linear', name='output_layer'))
model.compile(loss='mean_squared_error', optimizer='adam')

# Create a TensorBoard logger
logger = TensorBoard(
    log_dir='logs',
    histogram_freq=5,
    write_graph=True
)

# Train the model
model.fit(
    X,
    Y,
    epochs=50,
    shuffle=True,
    verbose=2,
    callbacks=[logger]
)

# Load the separate test data set

test_data_df = pd.read_csv("sales_data_test_scaled.csv")
X_test = test_data_df.drop('total_earnings', axis=1).values
Y_test = test_data_df[['total_earnings']].values
test_error_rate = model.evaluate(X_test, Y_test, verbose=0)
print(test_error_rate)

Then I got this error:

Traceback (most recent call last):

File "E:/Building.Deep.Learning.Applications.with.Keras.2.0/Exercise Files/06/model_logging final.py", line 34, in callbacks=[logger]

File "C:\Python3.6.4\lib\site-packages\keras\engine\training.py", line 1041, in fit steps_per_epoch=steps_per_epoch)

File "C:\Python3.6.4\lib\site-packages\keras\engine\training_arrays.py", line 219, in fit_loop callbacks.on_epoch_end(epoch, epoch_logs)

File "C:\Python3.6.4\lib\site-packages\keras\callbacks.py", line 77, in on_epoch_end callback.on_epoch_end(epoch, logs)

File "C:\Python3.6.4\lib\site-packages\keras\callbacks.py", line 865, in on_epoch_end

raise ValueError("If printing histograms, validation_data must be " ValueError: If printing histograms, validation_data must be provided, and cannot be a generator.

3 Answers 3

4

Move your validation into your .fit function like so:

# Train the model
model.fit(
X,
Y,
epochs=50,
shuffle=True,
verbose=2,
validation_data=(X_test, Y_test),
callbacks=[logger]
)

When you do it after the .fit function like you are, the logger cannot see the validation data.

You can also set histogram_freq=0 if that doesnt work. Your histogram will not work then though.

Sign up to request clarification or add additional context in comments.

Comments

1

First load X_test, Y_test data and use them with validation_data arg in model.fit. Working code below.

import pandas as pd
from keras.callbacks import TensorBoard
from keras.models import Sequential
from keras.layers import *

training_data_df = pd.read_csv("sales_data_training_scaled.csv")

X = training_data_df.drop('total_earnings', axis=1).values
Y = training_data_df[['total_earnings']].values

# Define the model
model = Sequential()
model.add(Dense(50, input_dim=9, activation='relu', name='layer_1'))
model.add(Dense(100, activation='relu', name='layer_2'))
model.add(Dense(50, activation='relu', name='layer_3'))
model.add(Dense(1, activation='linear', name='output_layer'))
model.compile(loss='mean_squared_error', optimizer='adam')

# Create a TensorBoard logger
logger = TensorBoard(
    log_dir='logs',
    histogram_freq=5,
    write_graph=True
)

# Load the separate test data set.
# >>> Setup X_test, Y_test before using in model.fit below. <<<
test_data_df = pd.read_csv("sales_data_test_scaled.csv")
X_test = test_data_df.drop('total_earnings', axis=1).values
Y_test = test_data_df[['total_earnings']].values

# Train the model
model.fit(
    X,
    Y,
    epochs=50,
    shuffle=True,
    verbose=2,
    callbacks=[logger],
    validation_data=(X_test, Y_test)   # <<< Add this.
)

# Evaluate
test_error_rate = model.evaluate(X_test, Y_test, verbose=0)
print(test_error_rate)

Comments

0

one solution is to turn off histograms with histogram_freq=0 :

logger = TensorBoard( log_dir='logs', histogram_freq=0, write_graph=True )

another solution is to specify not generator validation data

another solution is to create a wrapper for tensor board like here: https://github.com/keras-team/keras/issues/3358

see answer here: Keras autoencoder with Tensorflow Dataset API and logging to Tensorboard

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.