deep-learning-with-python-notebooks
deep-learning-with-python-notebooks copied to clipboard
3.5-classifying-movie-reviews.ipynb: Undefined variable `acc`
In 3.5-classifying-movie-reviews.ipynb , code block 36, the following line produces an error:
epochs = range(1, len(acc) + 1)
because acc
is not defined. I think it should be the following instead:
epochs = range(1, len(history_dict['acc']) + 1)
Actually, this line is missing in the Listing 3.9 , Page 74 of the book:
acc = history.history['acc']
val_acc = history.history['val_acc']
line is also missing.
yep. looks like a sloppy code even in the notebook itself... in https://github.com/fchollet/deep-learning-with-python-notebooks/blob/master/3.5-classifying-movie-reviews.ipynb
In [38]: plt.clf() # clear figure acc_values = history_dict['acc'] val_acc_values = history_dict['val_acc']
acc_values and val_acc_values are not even used.... since the author uses acc and val_acc instead. Let's hope they hire good tech proof reader to clean it up :)
There seems to be a typo in code block "In [18]". plt.ylabel('Loss') should be plt.ylabel('Accuracy').
acc = history.history['binary_accuracy'] you may try
It looks like the code behavior depending on how the model is compiled:
If you pass function object, then the keras code will prepend "binary" to the key.
model.compile(optimizer=optimizers.RMSprop(lr=0.001),
loss=losses.binary_crossentropy,
metrics=[metrics.binary_accuracy])
Then try to examine the history_dict.keys()
history = model.fit(x_train, y_train, epochs=10, batch_size=512,
validation_data=(x_val, y_val))
history_dict = history.history
history_dict.keys()
output => dict_keys(['val_loss', 'val_binary_accuracy', 'loss', 'binary_accuracy']) If you pass string, then the kears code will use "accuracy" without any problem.
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
output of history.history.keys() => dict_keys(['val_loss', 'val_acc', 'loss', 'acc']) Maybe some piece of keras code does this?
Thanks for the solutions you provided. I tried running the code but had used Listing 3.4 instead of listings 3.5 and 3.6. I also notice that val_acc_values is not used. Where do i post for Listing 2.2? I had issues with it two weeks back but have since solved them. Maybe someone has encountered problems with it as well. I am grateful for the help. Spent the entire night trying to make the code work.
import matplotlib.pyplot as plt history_dict = history.history acc = history_dict['accuracy'] #<----Add this line and the program should graph correctly! loss_values = history_dict['loss'] val_loss_values = history_dict['val_loss']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, loss_values, 'bo', label='Training loss') #“bo” is for “blue dot.” plt.plot(epochs, val_loss_values, 'b', label='Validation loss') #“b” is for “solid blue line.” plt.title('Training and validation loss') plt.xlabel('Epochs') plt.ylabel('Loss') plt.legend() plt.show()
Fixes #23
Look into history.history.keys()
to find the actual keys used which varies with different versions of keras, then replace val_acc
, acc
with the respective keys.