<jupyter_start><jupyter_text>케라스로 만드는 얕은 신경망 얕은 신경망을 만들어 MNIST 숫자를 분류합니다. [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/rickiepark/dl-illustrated/blob/master/notebooks/5-1.shallow_net_in_keras.ipynb)  라이브러리 적재<jupyter_code>from tensorflow import keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from matplotlib import pyplot as plt<jupyter_output><empty_output><jupyter_text>데이터 적재<jupyter_code>(X_train, y_train), (X_valid, y_valid) = mnist.load_data()
X_train.shape
y_train.shape
y_train[0:12]
plt.figure(figsize=(5,5))
for k in range(12):
    plt.subplot(3, 4, k+1)
    plt.imshow(X_train[k], cmap='Greys')
    plt.axis('off')
plt.tight_layout()
plt.show()
X_valid.shape
y_valid.shape
plt.imshow(X_valid[0], cmap='Greys')
plt.show()
X_valid[0]
y_valid[0]<jupyter_output><empty_output><jupyter_text>데이터 전처리<jupyter_code>X_train = X_train.reshape(60000, 784).astype('float32')
X_valid = X_valid.reshape(10000, 784).astype('float32')
X_train /= 255
X_valid /= 255
X_valid[0]
n_classes = 10
y_train = keras.utils.to_categorical(y_train, n_classes)
y_valid = keras.utils.to_categorical(y_valid, n_classes)
y_valid[0]<jupyter_output><empty_output><jupyter_text>신경망 구조 설계<jupyter_code>model = Sequential()
model.add(Dense(64, activation='sigmoid', input_shape=(784,)))
model.add(Dense(10, activation='softmax'))
model.summary()
(64*784)
(64*784)+64
(10*64)+10<jupyter_output><empty_output><jupyter_text>모델 컴파일<jupyter_code>model.compile(loss='mean_squared_error', optimizer=SGD(learning_rate=0.01), metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>훈련!<jupyter_code>model.fit(X_train, y_train, batch_size=128, epochs=200, verbose=1, validation_data=(X_valid, y_valid))
model.evaluate(X_valid, y_valid)<jupyter_output>313/313 [==============================] - 0s 1ms/step - loss: 0.0285 - accuracy: 0.8592
{'fraction_non_alphanumeric': 0.15974282888229477, 'fraction_numerical': 0.04846686449060336, 'mean_word_length': 8.453271028037383, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '370982', 'license': 'MIT', 'n_tokens_mistral': 955, 'n_tokens_neox': 923, 'n_words': 117, 'path': 'notebooks/5-1.shallow_net_in_keras.ipynb', 'repo_name': 'sunny191019/dl-illustrated', 'size': '2022'}