1. x_train, y_train 설정
x_train = ㄱ ~ ㅊ까지 5x5 배열로 표현
y_train = 0 ~ 9까지 순서대로 ㄱ - 0, ㄴ - 1, ㄷ- 2, ㄹ-3, ㅁ-4, ㅂ-5, ㅅ-6, ㅇ-7, ㅈ-8, ㅊ-9
# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow import keras
import numpy as np
# ㄱ
Giyeok_train = [[1,1,1,1,1],
[0,0,0,0,1],
[0,0,0,0,1],
[0,0,0,0,1],
[0,0,0,0,1]]
# ㄴ
NeeEun_train = [[1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,0],
[1,1,1,1,1]]
#ㄷ
Digeut_train = [[1,1,1,1,1],
[1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,0],
[1,1,1,1,1]]
#ㄹ
Rieul_train = [[1,1,1,1,1],
[0,0,0,0,1],
[1,1,1,1,1],
[1,0,0,0,0],
[1,1,1,1,1]]
#ㅁ
Mieum_train = [[1,1,1,1,1],
[1,0,0,0,1],
[1,0,0,0,1],
[1,0,0,0,1],
[1,1,1,1,1]]
#ㅂ
Bieup_train = [[1,0,0,0,1],
[1,0,0,0,1],
[1,1,1,1,1],
[1,0,0,0,1],
[1,1,1,1,1]]
#ㅅ
Siot_train = [[0,0,1,0,0],
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1],
[1,0,0,0,1]]
#ㅇ
Ieung_train = [[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1],
[0,1,0,1,0],
[0,0,1,0,0]]
#ㅈ
Jieut_train = [[1,1,1,1,1],
[0,0,1,0,0],
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1]]
#ㅊ
Chieut_train = [[0,0,1,0,0],
[1,1,1,1,1],
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1]]
#X_train 설정
x_train = [Giyeok_train, NeeEun_train, Digeut_train, Rieul_train, Mieum_train, Bieup_train, Siot_train, Ieung_train, Jieut_train, Chieut_train]
# ㄱ~ㅈ까지 다 넣기
y_train = [0,1,2,3,4,5,6,7,8,9]
# 3. 모델
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(5, 5)),
tf.keras.layers.Dense(5, activation='relu'),
tf.keras.layers.Dense(5, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
# 4. 모델 컴파일
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
인풋 - 5x5
히든층 - 2개의 층, 5 노드로 구성 relu함수 사용
아웃 풋 - 10노드 (0~9까지 표현을 위해), softmax 사용
loss(손실함수) - sparse_categorical_crossentropy
optimizer(옵티마이저) - adam
손실함수와 옵티마이저는 클릭 시 어떤 역할을 하는지 더 자세히 볼 수 있습니다.
# 5. 모델 훈련
model.fit(x_train, y_train, epochs=600)
# 예측
pred = model.predict([[[1,0,0,0,0],
[1,0,0,0,0],
[1,0,0,0,0],
[0,1,0,0,0],
[0,0,1,1,1]]])
print(pred)
결과
[[3.9235163e-03 7.6216877e-01 2.0186201e-01 4.3969421e-04 1.6150685e-02
6.5032253e-03 3.9332822e-06 6.5048498e-06 8.9414325e-03 2.2372269e-07]]
예측을 완벽한 ㄴ 모양은 아니지만 찌그러진 ㄴ 모양으로 예측을 시킴.
'AI > 딥러닝' 카테고리의 다른 글
flask서버에서 yolov4 모델 실행하기 (0) | 2021.03.08 |
---|---|
BP 알고리즘 without tensorflow (0) | 2021.01.18 |
yolov3 커스텀 학습 (0) | 2021.01.12 |