Last active
April 20, 2020 08:34
-
-
Save ty60/61e0c0b0e6cca6ec05ba1a1a5bf4a59d to your computer and use it in GitHub Desktop.
mnist tutorial with keras
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.tensorflow.org/tutorials/images/cnn | |
from __future__ import absolute_import, division, print_function, unicode_literals | |
import keras | |
from keras.datasets import mnist | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten | |
from keras.optimizers import RMSprop | |
(train_images, train_labels), (test_images, test_labels) = mnist.load_data() | |
train_images = train_images.reshape((train_images.shape[0], | |
train_images.shape[1], | |
train_images.shape[2], | |
1)) | |
test_images = test_images.reshape((test_images.shape[0], | |
test_images.shape[1], | |
test_images.shape[2], | |
1)) | |
train_images, test_images = train_images / 255.0, test_images / 255.0 | |
model = Sequential() | |
# 32 output channels, 3 * 3 filter | |
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=train_images.shape[1:])) | |
# 2 * 2 pool size | |
model.add(MaxPooling2D((2, 2))) | |
model.add(Conv2D(64, (3, 3), activation='relu')) | |
model.add(MaxPooling2D((2, 2))) | |
model.add(Flatten()) | |
model.add(Dense(64, activation='relu')) | |
model.add(Dense(10, activation='softmax')) | |
model.summary() | |
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) | |
model.fit(train_images, train_labels, epochs=5) | |
score = model.evaluate(test_images, test_labels, verbose=2) | |
print('Test loss:', score[0]) | |
print('Test accuracy:', score[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Shakyou: https://github.com/keras-team/keras/blob/master/examples/mnist_mlp.py | |
# Kaisetsu: https://qiita.com/ash8h/items/29e24fc617b832fba136#%E3%83%A2%E3%83%87%E3%83%AB%E6%A7%8B%E7%AF%89 | |
from __future__ import print_function | |
import keras | |
from keras.datasets import mnist | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout | |
from keras.optimizers import RMSprop | |
batch_size = 128 | |
num_classes = 10 | |
epochs = 20 | |
(in_train, out_train), (in_test, out_test) = mnist.load_data() | |
# shape: (num_data, number of rows, number of columns) | |
in_train = in_train.reshape(in_train.shape[0], in_train.shape[1] * in_train.shape[2]) | |
in_train = in_train.astype('float32') | |
in_train /= 255 | |
in_test = in_test.reshape(in_test.shape[0], in_test.shape[1] * in_test.shape[2]) | |
in_test = in_test.astype('float32') | |
in_test /= 255 | |
out_train = keras.utils.to_categorical(out_train, num_classes) | |
out_test = keras.utils.to_categorical(out_test, num_classes) | |
model = Sequential() | |
model.add(Dense(512, activation='relu', input_shape=in_train.shape[1:])) | |
model.add(Dropout(0.2)) | |
model.add(Dense(512, activation='relu')) | |
model.add(Dropout(0.2)) | |
model.add(Dense(num_classes, activation='softmax')) | |
model.summary() | |
model.compile(RMSprop(), 'categorical_crossentropy', ['accuracy']) | |
history = model.fit(in_train, out_train, | |
batch_size=batch_size, | |
epochs=epochs, | |
verbose=1, | |
validation_data=(in_test, out_test)) | |
score = model.evaluate(in_test, out_test, verbose=0) | |
print('Test loss:', score[0]) | |
print('Test accuracy:', score[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment