【Python】Keras(MNIST)

忘れないうちに、とりあえず、コードを残しておく。

Kerasを使ってみようを参考にさせていただきました。

import keras
from keras.datasets import mnist
from keras.models import Sequential, Model
from keras.layers import Dense, Activation, Dropout, Input
from keras import optimizers

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
num_classes = 10

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
60000 train samples
10000 test samples
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))

input_layer = Input(shape=(784,))
layer2 = Dense(512, activation='relu')(input_layer)
layer2 = Dropout(0.2)(layer2)
layer3 = Dense(512, activation='relu')(layer2)
layer3 = Dropout(0.2)(layer3)
output = Dense(num_classes, activation='softmax')(layer3)

model = Model(input_layer, output)

model.summary()
Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         [(None, 784)]             0         
_________________________________________________________________
dense_20 (Dense)             (None, 512)               401920    
_________________________________________________________________
dropout_12 (Dropout)         (None, 512)               0         
_________________________________________________________________
dense_21 (Dense)             (None, 512)               262656    
_________________________________________________________________
dropout_13 (Dropout)         (None, 512)               0         
_________________________________________________________________
dense_22 (Dense)             (None, 10)                5130      
=================================================================
Total params: 669,706
Trainable params: 669,706
Non-trainable params: 0
_________________________________________________________________
model.compile(loss='categorical_crossentropy',
              optimizer=keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=None, decay=0.0),
              metrics=['accuracy'])
batch_size = 10
epochs = 10

history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test))
Epoch 1/10
6000/6000 [==============================] - 17s 3ms/step - loss: 0.3788 - accuracy: 0.8944 - val_loss: 0.1804 - val_accuracy: 0.9623
Epoch 2/10
6000/6000 [==============================] - 15s 2ms/step - loss: 0.2415 - accuracy: 0.9565 - val_loss: 0.1754 - val_accuracy: 0.9712
Epoch 3/10
6000/6000 [==============================] - 15s 2ms/step - loss: 0.2432 - accuracy: 0.9619 - val_loss: 0.2450 - val_accuracy: 0.9707
Epoch 4/10
6000/6000 [==============================] - 15s 2ms/step - loss: 0.2547 - accuracy: 0.9658 - val_loss: 0.2058 - val_accuracy: 0.9735
Epoch 5/10
6000/6000 [==============================] - 16s 3ms/step - loss: 0.2806 - accuracy: 0.9657 - val_loss: 0.2553 - val_accuracy: 0.9730
Epoch 6/10
6000/6000 [==============================] - 16s 3ms/step - loss: 0.2809 - accuracy: 0.9657 - val_loss: 0.2608 - val_accuracy: 0.9732
Epoch 7/10
6000/6000 [==============================] - 15s 2ms/step - loss: 0.2732 - accuracy: 0.9694 - val_loss: 0.2511 - val_accuracy: 0.9739
Epoch 8/10
6000/6000 [==============================] - 15s 2ms/step - loss: 0.2768 - accuracy: 0.9706 - val_loss: 0.2653 - val_accuracy: 0.9748
Epoch 9/10
6000/6000 [==============================] - 15s 3ms/step - loss: 0.2876 - accuracy: 0.9712 - val_loss: 0.3487 - val_accuracy: 0.9747
Epoch 10/10
6000/6000 [==============================] - 15s 3ms/step - loss: 0.2941 - accuracy: 0.9701 - val_loss: 0.3550 - val_accuracy: 0.9714
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
Test loss: 0.3550007939338684
Test accuracy: 0.9714000225067139

Add a Comment

メールアドレスが公開されることはありません。