저희는 이제 MNIST를 해보겠습니다.
손글씨 인식하는 내용을 만드는 것인데, 이 것을 딥러닝 방식으로 학습시켜서 인식하는 프로그램을 만들어봅니다.
이 것을 처음에 봤을 때 드는 의문이, 저 이미지들을 사진으로 보여주면, 그냥 컴퓨터가 알아서 학습하나?
그게 가능해?
라고 생각했었습니다.
좀 찾아보니, 컴퓨터가 학습하는 방식은 저 친구들을 이용해서 벡터 행렬로 바꿉니다.
array([[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.2 , 0.62352941, 0.99215686, 0.62352941, 0.19607843, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.18823529, 0.93333333, 0.98823529, 0.98823529, 0.98823529, 0.92941176, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.21176471, 0.89019608, 0.99215686, 0.98823529, 0.9372549 , 0.91372549, 0.98823529, 0.22352941, 0.02352941, 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.03921569, 0.23529412, 0.87843137, 0.98823529, 0.99215686, 0.98823529, 0.79215686, 0.32941176, 0.98823529, 0.99215686, 0.47843137, 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.63921569, 0.98823529, 0.98823529, 0.98823529, 0.99215686, 0.98823529, 0.98823529, 0.37647059, 0.74117647, 0.99215686, 0.65490196, 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.2 , 0.93333333, 0.99215686, 0.99215686, 0.74509804, 0.44705882, 0.99215686, 0.89411765, 0.18431373, 0.30980392, 1. , 0.65882353, 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.18823529, 0.93333333, 0.98823529, 0.98823529, 0.70196078, 0.04705882, 0.29411765, 0.4745098 , 0.08235294, 0. , 0. , 0.99215686, 0.95294118, 0.19607843, 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.14901961, 0.64705882, 0.99215686, 0.91372549, 0.81568627, 0.32941176, 0. , 0. , 0. , 0. , 0. , 0. , 0.99215686, 0.98823529, 0.64705882, 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.02745098, 0.69803922, 0.98823529, 0.94117647, 0.27843137, 0.0745098 , 0.10980392, 0. , 0. , 0. , 0. , 0. , 0. , 0.99215686, 0.98823529, 0.76470588, 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.22352941, 0.98823529, 0.98823529, 0.24705882, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.99215686, 0.98823529, 0.76470588, 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.77647059, 0.99215686, 0.74509804, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 1. , 0.99215686, 0.76862745, 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0.29803922, 0.96470588, 0.98823529, 0.43921569, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.99215686, 0.98823529, 0.58039216, 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0.33333333, 0.98823529, 0.90196078, 0.09803922, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.02745098, 0.52941176, 0.99215686, 0.72941176, 0.04705882, 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0.33333333, 0.98823529, 0.8745098 , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.02745098, 0.51372549, 0.98823529, 0.88235294, 0.27843137, 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0.33333333, 0.98823529, 0.56862745, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.18823529, 0.64705882, 0.98823529, 0.67843137, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0.3372549 , 0.99215686, 0.88235294, 0. , 0. , 0. , 0. , 0. , 0. , 0.44705882, 0.93333333, 0.99215686, 0.63529412, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0.33333333, 0.98823529, 0.97647059, 0.57254902, 0.18823529, 0.11372549, 0.33333333, 0.69803922, 0.88235294, 0.99215686, 0.8745098 , 0.65490196, 0.21960784, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0.33333333, 0.98823529, 0.98823529, 0.98823529, 0.89803922, 0.84313725, 0.98823529, 0.98823529, 0.98823529, 0.76862745, 0.50980392, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0.10980392, 0.78039216, 0.98823529, 0.98823529, 0.99215686, 0.98823529, 0.98823529, 0.91372549, 0.56862745, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.09803922, 0.50196078, 0.98823529, 0.99215686, 0.98823529, 0.55294118, 0.14509804, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]])
자, 얘네가 뭔지 다시한번 보기 시작하면
이렇게 생긴 애가 나옵니다.
이 친구들(행렬)을 이용해서 데이터를 읽습니다.
import tensorflow as tf
print(tf.__version__)
# 텐서플로우 버전 변경
!pip uninstall tensorflow --yes
!pip install tensorflow==2.0.0
import tensorflow as tf
print(tf.__version__)
# 어떻게 생겼나?
from matplotlib import pyplot as plt
plt.imshow(x_train[1])
# 쌩 데이터는?
x_train[1]
# 데이터 셋을 받아와서 처리
# train은 뭐고 test는 무었인가?
# 기출문제로 학습을 시키고, 새로운 수능 문제로 테스트를 해야하는데, 또 기출문제가 들어오면 정확도를 판단할 수가 없음
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# 모양을 봄
x_train.shape
# 모델을 만듬
# relu와 dropout softmax는 다음에 설명
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
# optimizer adam이란?
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 학습하고 평가
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)
참조
[1] - https://wikidocs.net/32105
[2] - https://www.tensorflow.org/tutorials/quickstart/beginner
[3] - https://www.youtube.com/watch?v=7gGxBGvSAa0
[4] - https://www.youtube.com/watch?v=BQEhUD2XTaA&t=2751s
[5] - youtube.com/watch?v=bee0GrKBCrE
'인프런 - 강의 > 나도 만들어본다 AI 앱 (tensorflow+android)' 카테고리의 다른 글
9 - Tensorflow lite 만들기 (0) | 2020.03.10 |
---|---|
8 - relu, dropout, softmax (0) | 2020.03.05 |
6 - 그러면 deep러닝은 뭔가요? (0) | 2020.03.03 |
5 - Linear Regression 구현 (0) | 2020.03.03 |
4 - 정말 간단한 Linear regression 이론 (0) | 2020.03.03 |