Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Keras + Matplotlib causing crash
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(

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

# check training labels

# 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]])

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

# view the labels

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

# set up a validation set

history =,
                   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')

Thanks buran for the tags prompt, will make sure to do this properly from now on.
buran wrote May-09-2019, 10:44 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.

Top Page

Messages In This Thread
Keras + Matplotlib causing crash - by spearced - May-09-2019, 10:40 AM
RE: Keras + Matplotlib causing crash - by scidam - May-11-2019, 03:51 AM
RE: Keras + Matplotlib causing crash - by spearced - May-17-2019, 09:47 AM

Possibly Related Threads...
Thread Author Replies Views Last Post
  Keras: Time series classification midarq 0 93 Sep-25-2019, 09:03 AM
Last Post: midarq
  Keras. ModuleNotFoundError: No module named 'error' agatte 1 310 Sep-18-2019, 12:27 PM
Last Post: shivani
  Keras Target Problem inco 1 377 May-05-2019, 10:47 AM
Last Post: scidam
  keras tensor mainpulation output to next input JohnyBeGood 0 405 Nov-21-2018, 03:30 PM
Last Post: JohnyBeGood
  Keras: tweets classicifcation kiton 0 399 Nov-14-2018, 03:19 PM
Last Post: kiton

Forum Jump:

Users browsing this thread: 1 Guest(s)