3 Keras

Keras MNIST Example

# import the keras modules
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import SGD
from keras import utils
import numpy as np

# define some hyper parameters
batch_size = 100
n_inputs = 784
n_classes = 10
n_epochs = 10

# get the data
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# reshape the two dimensional 28 x 28 pixels
#   sized images into a single vector of 784 pixels
x_train = x_train.reshape(60000, n_inputs)
x_test = x_test.reshape(10000, n_inputs)

# convert the input values to float32
x_train = x_train.astype(np.float32)
x_test = x_test.astype(np.float32)

# normalize the values of image vectors to fit under 1
x_train /= 255
x_test /= 255

# convert output data into one hot encoded format
y_train = utils.to_categorical(y_train, n_classes)
y_test = utils.to_categorical(y_test, n_classes)

# build a sequential model
model = Sequential()
# the first layer has to specify the dimensions of the input vector
model.add(Dense(units=128, activation='sigmoid', input_shape=(n_inputs,)))
# add dropout layer for preventing overfitting
model.add(Dropout(0.1))
model.add(Dense(units=128, activation='sigmoid'))
model.add(Dropout(0.1))
# output layer can only have the neurons equal to the number of outputs
model.add(Dense(units=n_classes, activation='softmax'))

# print the summary of our model
model.summary()

# compile the model
model.compile(loss='categorical_crossentropy',
              optimizer=SGD(),
              metrics=['accuracy'])

# train the model
model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=n_epochs)

# evaluate the model and print the accuracy score
scores = model.evaluate(x_test, y_test)

print('\n loss:', scores[0])
print('\n accuracy:', scores[1])
Using TensorFlow backend.


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 128)               100480    
_________________________________________________________________
dropout_1 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 128)               16512     
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_3 (Dense)              (None, 10)                1290      
=================================================================
Total params: 118,282
Trainable params: 118,282
Non-trainable params: 0
_________________________________________________________________
Epoch 1/10
60000/60000 [==============================] - 3s 45us/step - loss: 2.3189 - acc: 0.1149
Epoch 2/10
60000/60000 [==============================] - 2s 35us/step - loss: 2.2513 - acc: 0.1761
Epoch 3/10
60000/60000 [==============================] - 2s 33us/step - loss: 2.1656 - acc: 0.2686
Epoch 4/10
60000/60000 [==============================] - 2s 30us/step - loss: 2.0350 - acc: 0.3792
Epoch 5/10
60000/60000 [==============================] - 2s 36us/step - loss: 1.8411 - acc: 0.4734
Epoch 6/10
60000/60000 [==============================] - 2s 36us/step - loss: 1.6026 - acc: 0.5484: 0s - loss: 1.64
Epoch 7/10
60000/60000 [==============================] - 2s 32us/step - loss: 1.3812 - acc: 0.6058
Epoch 8/10
60000/60000 [==============================] - 2s 31us/step - loss: 1.2057 - acc: 0.6496
Epoch 9/10
60000/60000 [==============================] - 2s 29us/step - loss: 1.0779 - acc: 0.6844
Epoch 10/10
60000/60000 [==============================] - 2s 25us/step - loss: 0.9752 - acc: 0.7150
10000/10000 [==============================] - 0s 34us/step

 loss: 0.855843718529

 accuracy: 0.7909

results matching ""

    No results matching ""