import numpy as np import matplotlib.pyplot as plt mnist = np.load('mnist.npz') x_train = mnist['x_train']/255. y_train = np.array([np.eye(10)[n] for n in mnist['y_train']]) x_test = mnist['x_test']/255. y_test = np.array([np.eye(10)[n] for n in mnist['y_test']]) from keras.models import Sequential from keras.layers import * from keras.optimizers import Adadelta model = Sequential() model.add(Reshape((28,28,1), input_shape=(28,28))) model.add(Conv2D(16, kernel_size=(5,5), activation='relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Flatten()) model.add(Dropout(0.2)) model.add(Dense(10, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer=Adadelta(), metrics=['accuracy']) model.fit(x_train, y_train, epochs=20, batch_size=128, validation_data=(x_test, y_test)) print('Performance (training)') print('Loss: %.5f, Acc: %.5f' % tuple(model.evaluate(x_train, y_train))) print('Performance (testing)') print('Loss: %.5f, Acc: %.5f' % tuple(model.evaluate(x_test, y_test))) fig = plt.figure(figsize=(8,8), dpi=80) for i in range(16): plt.subplot(4,4,i+1) w = model.layers[1].get_weights() plt.imshow(w[0][:,:,0,i], cmap='Greys') plt.show()