반응형
본 게시글은 필자가 강의, 책 등을 통해 개인적으로 학습한 것으로
본문의 모든 정보는 출처를 기반으로 작성되었습니다.
이제 CNN을 실습해보자.
data set으로는 MNIST를 사용한다.
전체 코드
import numpy as np
import tensorflow as tf
import random
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_test = x_test / 255
x_train = x_train / 255
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
# one hot encode y data
y_train = tf.keras.utils.to_categorical(y_train, 10)
y_test = tf.keras.utils.to_categorical(y_test, 10)
# hyper parameters
learning_rate = 0.001
training_epochs = 12
batch_size = 128
tf.model = tf.keras.Sequential()
# L1
tf.model.add(tf.keras.layers.Conv2D(filters=16, kernel_size=(3, 3), input_shape=(28, 28, 1), activation='relu'))
tf.model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
# L2
tf.model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu'))
tf.model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
# L3 fully connected
tf.model.add(tf.keras.layers.Flatten())
tf.model.add(tf.keras.layers.Dense(units=10, kernel_initializer='glorot_normal', activation='softmax'))
tf.model.compile(loss='categorical_crossentropy', optimizer=tf.keras.optimizers.Adam(lr=learning_rate), metrics=['accuracy'])
tf.model.summary()
tf.model.fit(x_train, y_train, batch_size=batch_size, epochs=training_epochs)
# predict 10 random hand-writing data
y_predicted = tf.model.predict(x_test)
for x in range(0, 10):
random_index = random.randint(0, x_test.shape[0]-1)
print("index: ", random_index,
"actual y: ", np.argmax(y_test[random_index]),
"predicted y: ", np.argmax(y_predicted[random_index]))
evaluation = tf.model.evaluate(x_test, y_test)
print('loss: ', evaluation[0])
print('accuracy', evaluation[1])
몇가지만 집고 넘아가자.
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_test = x_test / 255
x_train = x_train / 255
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
Data preprocessing 과정에서 training data를 convolutional Layer의 input에 알맞게
reshape해주었다.
tf.model = tf.keras.Sequential()
# L1
tf.model.add(tf.keras.layers.Conv2D(filters=16, kernel_size=(3, 3), input_shape=(28, 28, 1), activation='relu'))
tf.model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
# L2
tf.model.add(tf.keras.layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu'))
tf.model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
# L3 fully connected
tf.model.add(tf.keras.layers.Flatten())
tf.model.add(tf.keras.layers.Dense(units=10, kernel_initializer='glorot_normal', activation='softmax'))
Data set → Convolutional Layer → Pooling Layer → Convolutional Layer → Pooling Layer
→ Flatten Layer → FC Layer 단계로 층을 쌓아준 모습이다.
이하 설명은 생략한다.
참고 문헌 및 자료
1. Sung Kim Youtube channel : https://www.youtube.com/channel/UCML9R2ol-l0Ab9OXoNnr7Lw
Sung Kim
컴퓨터 소프트웨어와 딥러닝, 영어등 다양한 재미있는 이야기들을 나누는 곳입니다.
www.youtube.com
2. Andrew Ng Coursera class : https://www.coursera.org/learn/machine-learning
3. 조태호(2017). 모두의 딥러닝. 서울: 길벗
반응형
'IT study > 모두를 위한 딥러닝' 카테고리의 다른 글
RNN(1) (0) | 2020.10.30 |
---|---|
CNN(3) - ex.2(Ensemble) (0) | 2020.09.03 |
CNN(1) (0) | 2020.08.26 |
Drop out (0) | 2020.08.25 |
Weight Initialization (0) | 2020.08.25 |