使用theano和keras训练mnist数据集时的AssertionError

时间:2016-03-29 16:40:08

标签: python theano deep-learning keras

我正在做深度学习的教程,并且有mnist数据集分类示例:

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(123)

from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.utils import np_utils

batch_size = 128
nb_classes = 10 # number of classes

# input image dimensions
img_rows, img_cols = 28, 28

(X_train, y_train) , (X_test, y_test) = mnist.load_data()

# reshape data
X_train =  X_train.reshape(X_train.shape[0], 1, img_rows, img_cols) # 1 is 1 channnel i.e. grayscale
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)

X_train = X_train.astype("float32")
X_test = X_test.astype("float32")

# normalization
X_train /= 255;
X_test /= 255;

print("X_train shape: ", X_train.shape)
print(X_train.shape[0], " train samples")
print(X_test.shape[0], " test samples")

# convert class vectors to binary class matrices

y_train = np_utils.to_categorical(y_train, nb_classes)
y_test = np_utils.to_categorical(y_train, nb_classes)
print("One hot encoding: {}".format(y_train[0, :]))

model = Sequential();

model.add(Convolution2D(6, 5, 5, input_shape = (1, img_rows, img_cols), border_mode = "same"))

model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(16, 5, 5, border_mode= "same"))

model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(120, 5, 5, border_mode= "same"))

model.add(Activation("relu"))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(84))
model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(10)) # because 10 output classes
model.add(Activation("softmax"))

# TRAIN
model.compile(loss = "categorical_crossentropy", optimizer="adadelta")
nb_epoch = 2

model.fit(X_train, y_train, batch_size = batch_size, nb_epoch = nb_epoch, show_accuracy= True, verbose = 1, validation_data = (X_test, y_test))
score = model.evaluate(X_test, y_test, show_accuracy = True, verbose = 0)
print("Test score: ", score[0])
print("Test accuracy: ", score[1])

然而,在线:

model.fit(X_train, y_train, batch_size = batch_size, nb_epoch = nb_epoch, show_accuracy= True, verbose = 1, validation_data = (X_test, y_test))

我得到一个例外,“AssertionError未被用户代码处理”。我在异常中没有得到任何其他消息。

欢迎任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

正确的代码应该是:

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(123)

from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.utils import np_utils

batch_size = 128
nb_classes = 10 # number of classes

# input image dimensions
img_rows, img_cols = 28, 28

(X_train, y_train) , (X_test, y_test) = mnist.load_data()

# reshape data
X_train =  X_train.reshape(X_train.shape[0], 1, img_rows, img_cols) # 1 is 1 channnel i.e. grayscale
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)

X_train = X_train.astype("float32")
X_test = X_test.astype("float32")

# normalization
X_train /= 255;
X_test /= 255;

print("X_train shape: ", X_train.shape)
print(X_train.shape[0], " train samples")
print(X_test.shape[0], " test samples")

# convert class vectors to binary class matrices

y_train = np_utils.to_categorical(y_train, nb_classes)
y_test = np_utils.to_categorical(y_test, nb_classes)
print("One hot encoding: {}".format(y_train[0, :]))

model = Sequential();

model.add(Convolution2D(6, 5, 5, input_shape = (1, img_rows, img_cols), border_mode = "same"))

model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(16, 5, 5, border_mode= "same"))

model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(120, 5, 5, border_mode= "same"))

model.add(Activation("relu"))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(84))
model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(10)) # because 10 output classes
model.add(Activation("softmax"))

# TRAIN
model.compile(loss = "categorical_crossentropy", optimizer="adadelta")
nb_epoch = 2

model.fit(X_train, y_train, batch_size = batch_size, nb_epoch = nb_epoch, show_accuracy= True, verbose = 1, validation_data = (X_test, y_test))
score = model.evaluate(X_test, y_test, show_accuracy = True, verbose = 0)
print("Test score: ", score[0])
print("Test accuracy: ", score[1])