[英]Matplotlib kills jupyter kernel after training model
我在 Jupyter 筆記本中運行了一個神經網絡,我想 plot 結果(損失與紀元數)。 我可以毫無問題地運行 model,但即使是簡單的 matplotlib plot 也會殺死 Z50484C19F1AFDAEDF3D821。
這是創建 model 和我想使用的數據的代碼:
from keras import models
from keras import layers
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
from keras.datasets import imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data( num_words=10000)
# Change review into array
def vectorize_sequences(sequences, dimension=10000):
results = np.zeros((len(sequences), dimension)) # create all-zero matrix
for i, sequence in enumerate(sequences):
results[i, sequence] = 1. # If review has word, change that index to 1
return results
x_train = vectorize_sequences(train_data)
x_test = vectorize_sequences(test_data)
y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')
# Create model
model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=(10000,))) # two int. layers w/16 hidden units each
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid')) # outputs the scalar prediction
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
# Create mini-test data
x_val = x_train[:10000]
partial_x_train = x_train[10000:]
y_val = y_train[:10000]
partial_y_train = y_train[10000:]
# fit model
history = model.fit(partial_x_train, partial_y_train, epochs=20, batch_size=512, validation_data=(x_val, y_val))
# Get values for plot
history_dict = history.history
history_dict.keys()
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
epoch_num = [i for i in range(1,21)]
這按預期工作。 但是,當我嘗試使用以下代碼對 plot 數據進行處理時,我收到一條消息: “kernel 似乎已經死了。它將自動重新啟動。”
plt.plot(epoch_num, loss_values, 'bo', label='Training loss')
plt.plot(epoch_num, val_loss_values, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
I can restart the kernel and make matplotlib plots, but when I try to make a plot after running the model matplotlib causes the error to appear. 我已嘗試將 keras、tensorflow、matplotlib 和 Z2EA9510C37F7F89E4941FF75F62F2 更新為無效果。 任何人都可以提供有關為什么會發生這種情況的見解並提供解決方案嗎?
我使用了最新的 tensorflow 並從 tensorflow 導入了 keras。 一切都按預期工作。 我改變了前三行,如下所示。 完整代碼在這里
from tensorflow import keras
from tensorflow.keras import models
from tensorflow.keras import layers
以下 plot 顯示了歷元與損失
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.