inputs = layers.Input(input_shape, dtype=tf.float64) net = layers.Conv2D(32, (3, 3), padding='SAME')(inputs) net = layers.Activation('relu')(net) net = layers.Conv2D(32, (3, 3), padding='SAME')(net) net = layers.Activation('relu')(net) net = layers.MaxPooling2D(pool_size=(2, 2))(net) net = layers.Dropout(0.5)(net)
net = layers.Conv2D(64, (3, 3), padding='SAME')(net) net = layers.Activation('relu')(net) net = layers.Conv2D(64, (3, 3), padding='SAME')(net) net = layers.Activation('relu')(net) net = layers.MaxPooling2D(pool_size=(2, 2))(net) net = layers.Dropout(0.5)(net)
net = layers.Flatten()(net) net = layers.Dense(512)(net) net = layers.Activation('relu')(net) net = layers.Dropout(0.5)(net) net = layers.Dense(num_classes)(net) net = layers.Activation('softmax')(net)
model = tf.keras.Model(inputs=inputs, outputs=net, name='Basic_CNN')
Preprocess
TensorFlow 공식 홈페이지에서 말한 expert 방법을 사용한다.
1 2 3 4 5 6 7 8 9 10 11
mnist = tf.keras.datasets.mnist
# Load Data from MNIST (x_train, y_train), (x_test, y_test) = mnist.load_data()
for epoch in range(2): for images, labels in train_ds: train_step(images, labels)
for test_images, test_labels in test_ds: test_step(test_images, test_labels)
template = 'Epoch {}, Loss: {}, Accuracy: {}, Test Loss {}, Test Accuracy: {}' print(template.format(epoch+1, train_loss.result(), train_accuracy.result() * 100, test_loss.result(), test_accuracy.result() * 100))
결과
1 2 3 4
Start Training Epoch 1, Loss: 0.04196552559733391, Accuracy: 98.74666595458984, Test Loss 0.043360475450754166, Test Accuracy: 98.72000122070312 Start Training Epoch 2, Loss: 0.033374134451150894, Accuracy: 99.0050048828125, Test Loss 0.03336939960718155, Test Accuracy: 98.95500183105469