AssertionError enquanto conjunto de dados de treinamento mnist usando Teano e keras

votos
0

Eu estava fazendo um tutorial sobre aprendizagem profunda e havia mnist exemplo classificação de conjunto de dados:

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

No entanto, na linha:

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

Eu recebo uma exceção AssertionError foi não tratada pelo código do usuário. Eu recebo nenhuma outra mensagem na exceção.

Qualquer ajuda é bem vinda. Obrigado.

Publicado 29/03/2016 em 15:40
fonte usuário
Em outras línguas...                            


1 respostas

votos
0

O código correto deve ser:

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])
Respondeu 13/05/2016 em 13:36
fonte usuário

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more