如何解决卷积神经网络的断言错误?

时间:2019-05-11 14:24:40

标签: python keras deep-learning conv-neural-network

断言错误:

我正在运行附带的代码,并在model.fit步骤出现错误。我尝试通过不同的后端选择来解决,但未成功。

错误:

raceback (most recent call last):

  File "<ipython-input-151-392f3df82a71>", line 1, in <module>
    runfile('/Users/sandeepagarwal/Documents/Learning/CNN', wdir='/Users/sandeepagarwal/Documents/Learning')

  File "/anaconda3/envs/DL/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "/anaconda3/envs/DL/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/sandeepagarwal/Documents/Learning/CNN", line 62, in <module>
    history = model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=1, validation_data=(x_test,y_test))

  File "/anaconda3/envs/DL/lib/python3.6/site-packages/keras/engine/training.py", line 1037, in fit
    validation_steps=validation_steps)

  File "/anaconda3/envs/DL/lib/python3.6/site-packages/keras/engine/training_arrays.py", line 176, in fit_loop
    np.random.shuffle(index_array)

  File "mtrand.pyx", line 4823, in mtrand.RandomState.shuffle

  File "/anaconda3/envs/DL/lib/python3.6/site-packages/numpy/core/_internal.py", line 296, in __init__
    assert self._data.value == ptr

AssertionError 代码:

import numpy as np
import matplotlib.pyplot as plt

from keras import backend as K
from keras import models
from keras import layers
from keras import optimizers
from keras import losses
from keras import metrics

from keras.utils import to_categorical

from keras.datasets import mnist

K.set_image_dim_ordering("th")
# data




(x_train,y_train),(x_test,y_test) = mnist.load_data()

x_train = x_train.astype("float32")
x_test = x_test.astype("float32")

x_train /=255
x_test /=255

y_train =to_categorical(y_train,10)
y_test =to_categorical(y_test,10)

print(y_train.shape)
print(y_test.shape)

# we need a 60K x [1 x 28 x 28] shape as input to the CONVNET
x_train = x_train[:, np.newaxis,:, :]
x_test = x_test[:, np.newaxis,:, :]
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')



model = models.Sequential()
model.add(layers.Convolution2D(20,kernel_size=5, padding ='same', activation= 'relu',input_shape = (1,28,28)))
model.add(layers.pooling.MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(layers.Convolution2D(50,kernel_size=5, padding ='same', activation= 'relu'))
model.add(layers.pooling.MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(500,activation ='relu'))
model.add(layers.Dense(10,activation = 'softmax'))
model.summary()

model.compile(loss="categorical_crossentropy", optimizer=optimizers.Adam(),metrics=["accuracy"])

history = model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=1, validation_data=(x_test,y_test))



score = model.evaluate(x_test, y_test, verbose=1)
print("Test score:", score[0])
print('Test accuracy:', score[1])

history_dict = history.history
history_dict.keys()
#plotting training & validation loss
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
x_axis = range(1,len(loss_values)+1)
plt.plot(x_axis,loss_values,'b',label='Training Loss')
plt.plot(x_axis,val_loss_values,'r',label='Validation Loss')
plt.title("Training and Validation Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()
plt.show()

我想知道确切的问题在哪里以及如何解决。

0 个答案:

没有答案