deep-learning-with-python-notebooks icon indicating copy to clipboard operation
deep-learning-with-python-notebooks copied to clipboard

3.5-classifying-movie-reviews.ipynb: Undefined variable `acc`

Open tomsing1 opened this issue 7 years ago • 9 comments

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)

tomsing1 avatar Dec 26 '17 21:12 tomsing1

Actually, this line is missing in the Listing 3.9 , Page 74 of the book: acc = history.history['acc']

mmalekzadeh avatar Jan 04 '18 21:01 mmalekzadeh

val_acc = history.history['val_acc'] line is also missing.

neilpanchal avatar Jan 15 '18 18:01 neilpanchal

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 :)

krinkere avatar Feb 07 '18 20:02 krinkere

There seems to be a typo in code block "In [18]". plt.ylabel('Loss') should be plt.ylabel('Accuracy').

cfsmile avatar Mar 05 '18 06:03 cfsmile

acc = history.history['binary_accuracy'] you may try

jsxyhelu avatar Jun 01 '18 12:06 jsxyhelu

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?

renewang avatar Sep 28 '18 12:09 renewang

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.

RorisangSitoboli avatar Aug 31 '19 09:08 RorisangSitoboli

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

l1legend avatar Jan 24 '20 15:01 l1legend

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.

ghimireadarsh avatar Sep 28 '20 08:09 ghimireadarsh