0

I'm using Jupyter Notebook installed by Anaconda 1.9.7 to run a machine learning model using Tensorflow, Keras, Python 3.x, and Matplotlib. When I run the code from the Terminal on my Mac everything runs fine and the graph is plotted to an external window. When I run the same code in Jupyter Notebook, the kernel dies and restarts the first time the code uses Matplotlib.

Initially, I was not using "%matplotlib inline" so I added this to the top, but the graph still does not show. I created a simple use case (not the machine learning code provided here) and the graph plotted inline to Jupyter Notebook. The current code works without a problem when I run it from the Terminal on my Mac, and the graph displays to external window.

get_ipython().run_line_magic('matplotlib', 'inline')


import tensorflow as tf

from keras.datasets import reuters

import numpy as np

np_load_old = np.load

np.load = lambda *a,**k: np_load_old(*a, allow_pickle=True, **k)


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

np.load = np_load_old

word_index = reuters.get_word_index()

reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])

decoded_newswire = ' '.join([reverse_word_index.get(i - 3, '?') for i in train_data[10]])

decoded_newswire

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

x_train = vectorize_sequences(train_data)

x_test = vectorize_sequences(test_data)

from keras.utils.np_utils import to_categorical

one_hot_train_labels = to_categorical(train_labels)

one_hot_test_labels = to_categorical(test_labels)

from keras import models

from keras import layers

model = models.Sequential()

model.add(layers.Dense(64, activation='relu', input_shape=(10000,)))

model.add(layers.Dense(64, activation='relu'))

model.add(layers.Dense(46, activation='softmax'))

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

x_val = x_train[:1000]

partial_x_train = x_train[1000:]

y_val = one_hot_train_labels[:1000]

partial_y_train = one_hot_train_labels[1000:]

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

loss = history.history['loss']

val_loss = history.history['val_loss']

epochs = range(1, len(loss) + 1)

import matplotlib.pyplot as plt

plt.title('Training and validation loss')

plt.xlabel('Epochs')

plt.ylabel('Loss')

plt.legend()

plt.plot(epochs, loss, 'bo', label='Training loss')

plt.plot(epochs, val_loss, 'b', label='Validation loss')

plt.show()

I expect the last line to plot a graph inline in Jupyter Notebook. Instead, the Kernel dies at the line "plt.title('Training and validation loss')" and when I run the line independently it give the error "NameError: name 'plt' is not defined."

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • The only thing different I do is using `%matplotlib inline` instead of `get_ipython().run_line_magic('matplotlib', 'inline')`. But I import `plt` right after that line. – Daniel Möller Aug 22 '19 at 18:07
  • I actually use "%matplotlib inline" in the Jupyter Notebook. To insert the code into this post, I exported the Notebook to .py and "%matplotlib inline" was translated to "get_ipython().run_line_magic('matplotlib', 'inline')." – Delmonik C Aug 23 '19 at 01:05

1 Answers1

0

I tried your code with a little modifications:

  • commented out the following lines:
    • np_load_old = np.load
    • np.load = lambda *a, **l: np_load_old ...
    • np.load = np_load_old

and it worked. I got the following plot:

enter image description here

My versions are:

  • tensorflow 1.13.1
  • matplotlib 3.1.1
  • jupyter 1.0.0
  • python 3.6.6
Hamed
  • 315
  • 2
  • 13
  • I'm using tensorflow 1.13.1, matplotlib 3.1.0, jupyter 6.0.0, and 3.7.3. When I comment those lines I get "ValueError: Object arrays cannot be loaded when allow_pickle=False" when trying to download the Reuters data. I added those comments to address the issue. How is 'allow_pickle' configured on your system? – Delmonik C Aug 23 '19 at 01:02
  • what's numpy version? mine is 1.16.4. According to [this](https://stackoverflow.com/questions/55890813/how-to-fix-object-arrays-cannot-be-loaded-when-allow-pickle-false-for-imdb-loa), I think it's true by default. – Hamed Aug 23 '19 at 13:40
  • Try also this: `np.load(path, allow_pickle)` where `allow_pickle` is a boolean. [source](https://github.com/tensorflow/tensorflow/issues/28102) – Hamed Aug 23 '19 at 13:42
  • This reference I found [here](https://github.com/tensorlayer/tensorlayer/issues/1019) states that allow_pickle is set to false by default. I am successfully setting allow_pickle to true through a boolean in my current implementation. There is some incompatibility between Jupyter Notebook and matplotlib, and I'm not sure how numpy plays a role. The code runs perfectly as-is if I run it through the Terminal on my Mac. – Delmonik C Aug 23 '19 at 17:39
  • I downgraded my version of numpy to 1.16.2, which allowed me to comment out the np.load lines, but the problem still exists. – Delmonik C Aug 23 '19 at 18:52
  • try `numpy 1.16.4` and have you tried `np.load(patch, allow_pickle=true)`? – Hamed Aug 23 '19 at 19:14
  • Originally I was using 1.16.4. I tried "allow_pickle=true" in the code listed above. I added that line to solve a problem with the download of the Reuters data. I followed the instructions listed [here](https://stackoverflow.com/questions/55890813/how-to-fix-object-arrays-cannot-be-loaded-when-allow-pickle-false-for-imdb-loa/56243777#56243777). – Delmonik C Aug 24 '19 at 03:58