<jupyter_start><jupyter_text>Load Dataset<jupyter_code>from keras.datasets import mnist

((X_train, y_train), (X_test, y_test)) = mnist.load_data()
 
print(X_train.shape, y_train.shape)
print(X_test.shape, y_test.shape)<jupyter_output>(60000, 28, 28) (60000,)
(10000, 28, 28) (10000,)<jupyter_text>Visualize<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt

plt.gray()

print(y_train[0:10])

figures, axes = plt.subplots(nrows=2, ncols=5)
figures.set_size_inches(18, 8)

axes[0][0].matshow(X_train[0])
axes[0][1].matshow(X_train[1])
axes[0][2].matshow(X_train[2])
axes[0][3].matshow(X_train[3])
axes[0][4].matshow(X_train[4])
axes[1][0].matshow(X_train[5])
axes[1][1].matshow(X_train[6])
axes[1][2].matshow(X_train[7])
axes[1][3].matshow(X_train[8])
axes[1][4].matshow(X_train[9])<jupyter_output>[5 0 4 1 9 2 1 3 1 4]<jupyter_text>Preprocessing<jupyter_code># Dimension 기준
# X = number of data / input height / input width / input channel
# W = output channel / input channel / weight height / weight width
# Y = number of data / output height / output width / output channel
X_train = X_train.reshape(60000, 28, 28, 1)
X_test = X_test.reshape(10000, 28, 28, 1)

print(X_train.shape, X_test.shape)
from keras.utils import to_categorical

# One hot encoding을 합니다.
# np.eye(10)[y_train]과 동일합니다.
y_train_hot = to_categorical(y_train)

# np.eye(10)[y_test]와 동일합니다.
y_test_hot = to_categorical(y_test)

print(y_train_hot.shape, y_test_hot.shape)<jupyter_output>(60000, 10) (10000, 10)<jupyter_text>Build a Model  Convolutional Neural Network<jupyter_code>from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

model = Sequential()

model.add(Conv2D(filters=6,
                 kernel_initializer='random_uniform',
                 activation='sigmoid',
                 kernel_size=(5, 5),
                 input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2),
                       strides=(2, 2)))

model.add(Conv2D(filters=12,
                 kernel_initializer='random_uniform',
                 activation='sigmoid',
                 kernel_size=(5, 5)))
model.add(MaxPooling2D(pool_size=(2, 2),
                       strides=(2, 2)))

model.add(Flatten())

model.add(Dense(units=128,
                kernel_initializer='random_uniform',
                activation='sigmoid'))
model.add(Dense(units=10,
                kernel_initializer='random_uniform',
                activation='sigmoid'))

from keras.optimizers import SGD

optimizer = SGD(lr=0.1)

model.compile(optimizer=optimizer,
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(X_train,
          y_train_hot,
          epochs=30,
          validation_data=(X_test, y_test_hot))
import pandas as pd

predictions = model.predict(X_test)
predictions = np.argmax(predictions, axis=1)

result = pd.DataFrame({'actual': y_test, 'predict': predictions})

accuracy = (result['actual'] == result['predict']).mean()
print("Accuracy = {0:.6f}".format(accuracy))
      
result.head(10)<jupyter_output>Accuracy = 0.983600
{'fraction_non_alphanumeric': 0.1447070249271609, 'fraction_numerical': 0.05212042732275817, 'mean_word_length': 4.073891625615763, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '659687', 'license': 'MIT', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1166, 'n_words': 197, 'path': 'example/02-Keras-tutorial.ipynb', 'repo_name': 'daesookimds/Deep-Learning', 'size': '3089'}