data preprocessing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 from keras.datasets import mnist(X_train, y_train), (X_test, y_test) = mnist.load_data() X_train = X_train.reshape(X_train.shape[0 ], -1 ) / 255. X_test = X_test.reshape(X_test.shape[0 ], -1 ) / 255. y_train = np_utils.to_categorical(y_train, num_classes=10 ) y_test = np_utils.to_categorical(y_test, num_classes=10 ) print (X_train[1 ].shape)""" (784,) """ """ [[ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.] [ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]] """
1. build model
1 2 3 4 5 6 7 8 9 10 11 import numpy as npnp.random.seed(1337 ) from keras.datasets import mnistfrom keras.utils import np_utilsfrom keras.models import Sequentialfrom keras.layers import Dense, Activationfrom keras.optimizers import RMSprop
Another way to build your neural net
1 2 3 4 5 6 7 8 model = Sequential([ Dense(32 , input_dim=784 ), Activation('relu' ), Dense(10 ), Activation('softmax' ), ]) model.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_5 (Dense) (None, 32) 25120
_________________________________________________________________
activation_5 (Activation) (None, 32) 0
_________________________________________________________________
dense_6 (Dense) (None, 10) 330
_________________________________________________________________
activation_6 (Activation) (None, 10) 0
=================================================================
Total params: 25,450
Trainable params: 25,450
Non-trainable params: 0
_________________________________________________________________
1 2 rmsprop = RMSprop(lr=0.001 , rho=0.9 , epsilon=1e-08 , decay=0.0 )
2. compile model
1 2 3 4 model.compile (optimizer=rmsprop, loss='categorical_crossentropy' , metrics=['accuracy' ])
3. train model
1 2 3 4 5 6 7 8 9 10 11 print ('Training ------------' )model.fit(X_train, y_train, epochs=2 , batch_size=32 )
4. evaluate model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 print ('\nTesting ------------' )loss, accuracy = model.evaluate(X_test, y_test) print ('test loss: ' , loss)print ('test accuracy: ' , accuracy)""" Testing ------------ 9760/10000 [============================>.] - ETA: 0s test loss: 0.1724540345 test accuracy: 0.9489 """
Reference
Checking if Disqus is accessible...