Python Forum
Keras + Matplotlib causing crash
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Keras + Matplotlib causing crash
#1
Hi all,

I'm working my way through Francios Chollet's 'Deep Learning with Python', which teaches Deep Learning through the Keras frontend for Tensorflow, using Jupyter Notebook. I'm running into a bit of an issue when trying out one of the early practical implementations, where after successfully training a neural network on some data (a dataset of IMDB reviews), the Jupyter kernel crashes when I try plotting the resulting accuracy and loss through matplotlib.

I have successfully plotted graphs in other separate code, and the neural network seems to train with no problem by itself, but when I try to do one after the other it seems to overload something and cause a crash. This may be a processing issue with my laptop (a fairly decent Macbook Pro), but could somebody who has experience with this check the code for themselves and see if they can get it to run successfully? At least then I would know if it's an issue with my hardware and not the code itself.

Thanks in advance if anyone can help. Code is below, sorry if it should be formatted or shared differently, I'm new to all this.


# setup
from keras.datasets import imdb

(train_data, train_labels), (test_data, test_labels) = imdb.load_data(
    num_words=10000)

import numpy as np

import matplotlib.pyplot as plt

from keras import models
from keras import layers
from keras import optimizers
from keras import losses
from keras import metrics

# check training data
train_data[0]

# check training labels
train_labels[0]

# test that maximum number of unique words is 10000
max([max(sequence) for sequence in train_data])

# read original reviews for kicks
word_index = imdb.get_word_index()
reverse_word_index = dict(
    [(value, key) for (key, value) in word_index.items()])
decoded_review = ' '.join(
    [reverse_word_index.get(i - 3, '?') for i in train_data[950]])
decoded_review

def vectorise_sequences(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1.
    return results

x_train = vectorise_sequences(train_data)
x_test = vectorise_sequences(test_data)

y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')

# view the transformed samples
x_train[0]

# view the labels
y_train[0]

model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=(10000,)))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(optimizer=optimizers.RMSprop(lr=0.001),
             loss=losses.binary_crossentropy,
             metrics=['acc'])


# set up a validation set
x_val=x_train[:10000]
partial_x_train=x_train[10000:]
y_val=y_train[:10000]
partial_y_train=y_train[10000:]

history = model.fit(partial_x_train,
                   partial_y_train,
                   epochs=20,
                   batch_size=512,
                   validation_data=(x_val, y_val))

history_dict = history.history
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')
plt.plot(epochs, val_loss_values, 'b', label='Validation Loss')
plt.title('Training & Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()

plt.show()

Thanks buran for the tags prompt, will make sure to do this properly from now on.
Reply
#2
I got MemoryError on my computer, but it is quite weak (it has only 4GB of RAM). When I changed
10000 to 1000 everything works fine, but you have a typo in epochs = range(1, len('acc') + 1); it should be
epochs = list(range(1, len(history_dict['acc']) + 1)).
Reply
#3
(May-11-2019, 03:51 AM)scidam Wrote: I got MemoryError on my computer, but it is quite weak (it has only 4GB of RAM). When I changed
10000 to 1000 everything works fine, but you have a typo in epochs = range(1, len('acc') + 1); it should be
epochs = list(range(1, len(history_dict['acc']) + 1)).

Thanks for correcting the typo, my copy of the book is obviously a bit out of date, that's how it appears in the text. If you're changing the 10000 to 1000 then surely the training data becomes incomplete? I tried it anyway and I'm still getting the same error, despite also correcting the typo. My macbook has 8gb of ram, and it's not saying anything about memory issues, but jupyter just keeps saying the kernel has crashed Think
Reply
#4
Hi spearced,

Did you find out a way to fix this problem ?

I had the same problem with you. After I train my model using keras, if I try to plot using matplotlib, then the console crashes...

I tried this both in Spyder, and Jupyter Notebook.

I've no idea what happens, but I believe it must be something related to the backend of the plotting, rather than the memory, because I tested small scale problems with only a few training data, the same problem occured.

BTW, I'm also working on Macbook pro, I am not sure if it's relevant to the Mac OS.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  1st layer tf.keras output shape set at multiple - need help! Afrodizzyjack 0 1,782 Jun-07-2022, 04:53 PM
Last Post: Afrodizzyjack
  issue displaying summary of whole keras CNN model on TensorFlow with python Afrodizzyjack 0 1,619 Oct-27-2021, 04:07 PM
Last Post: Afrodizzyjack
  Understanding Keras and TensorFlow and how to use them bytecrunch 1 2,052 Mar-11-2021, 02:40 PM
Last Post: jefsummers
  Problems feeding live input from my microphone into a keras model (SegFault: 11) zeptozetta 1 2,540 Sep-14-2020, 03:08 AM
Last Post: zeptozetta
  Keras.Predict into Dataframe Finpyth 13 9,563 Aug-31-2020, 07:22 AM
Last Post: hussainmujtaba
  Making a Basic Keras Model - Input Shape and Parameters MattKahn13 0 2,093 Aug-16-2020, 04:36 PM
Last Post: MattKahn13
  How to find what is causing the unboundlocalerror 'crumb' and invalid syntax error? JonathanBanks 1 2,261 Jul-28-2020, 11:46 AM
Last Post: Yoriz
  Error when import Keras Azadfalah 1 2,753 Apr-29-2020, 04:45 AM
Last Post: buran
  Keras Dense layer with wrong input d1r4c 0 1,736 Jan-02-2020, 02:35 PM
Last Post: d1r4c
  Keras: Time series classification midarq 0 1,963 Sep-25-2019, 09:03 AM
Last Post: midarq

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020