Skip to content

Instantly share code, notes, and snippets.

@mmmikael
Last active October 28, 2016 19:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmmikael/cc895249e7941c2f56b3 to your computer and use it in GitHub Desktop.
Save mmmikael/cc895249e7941c2f56b3 to your computer and use it in GitHub Desktop.
from __future__ import absolute_import
from __future__ import print_function
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
from scipy.special import expit
import numpy as np
np.set_printoptions(suppress=True)
np.set_printoptions(precision=6)
'''
Train a simple convnet on the MNIST dataset.
Run on GPU: THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python mnist_cnn.py
Get to 99.25% test accuracy after 12 epochs (there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''
TRAIN = True
batch_size = 128
nb_classes = 10
nb_epoch = 5
# the data, shuffled and split between tran and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
X_train = X_train.astype("float32")
X_test = X_test.astype("float32")
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)
model = Sequential()
model.add(Convolution2D(32, 1, 3, 3, border_mode='valid'))
model.add(Activation('relu'))
model.add(Convolution2D(32, 32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(128, 32, 12, 12))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(nb_classes, 128, 1, 1))
model.add(Flatten())
model.add(Activation('sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta')
if TRAIN:
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.save_weights('/tmp/model_weights.hdf5', overwrite=True)
else:
model.load_weights('/tmp/model_weights.hdf5')
# create mosaic image with twice the same digit
data = np.zeros([1,1,28, 28*2], 'float32')
data[0,0,:,:28] = X_train[0,0,:,:]
data[0,0,:,28:] = X_train[0,0,:,:]
print('real label: %d' % y_train[0])
r0 = (model.predict(data[:,:,:,:28])).ravel()
print('expected result (centered):', r0, '(label=%d)' % np.argmax(r0))
pred = model.predict(data).ravel()
pred = pred.reshape(nb_classes, len(pred)/nb_classes).T
for i,p in enumerate(pred):
print('pos:%02d' % i, p, '(label=%d)' % np.argmax(p))
@guy4261
Copy link

guy4261 commented Jul 30, 2015

What's this import for?

from scipy.special import expit

Doesn't seem to get used anywhere...
Sorry for the n00b question, but I'm new to both Keras and ANNs but not to Python, so this is a shallow question but would help me understand one more bit...

@moilerat
Copy link

moilerat commented Aug 9, 2015

Hi Guy,
It is indeed useless here, used in other parts of our library, left by mistake.
Victor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment