1번째 줄:
1번째 줄:
== 개요 ==
== 개요 ==
학습 모델을 설계하고 제작한다. 층을 쌓아서 제작하는 형태.
학습 모델을 설계하고 제작한다. 층을 쌓아서 제작하는 형태.
+
+
모델을 만드는 방법은 클래스를 사용하느냐 안하느냐, 연쇄형이냐 분산형이냐에 따라 4가지 방법이 있다.
=== 예시와 사용 ===
=== 예시와 사용 ===
40번째 줄:
42번째 줄:
|모델 교육
|모델 교육
|모델을 교육한다.
|모델을 교육한다.
+
+
+
만약 입력하는 데이터가 제너레이터라면 fit 대신
+
+
fit_generato 함수로 교육해야 한다.
|<syntaxhighlight lang="python">
|<syntaxhighlight lang="python">
# 어떤 방식으로 교육할지 지정.
# 어떤 방식으로 교육할지 지정.
70번째 줄:
77번째 줄:
= 모델 제작 =
= 모델 제작 =
+
모델은 Sequential과 add를 이용한 연쇄방식과, 순차적으로 넘겨주는 분산방식이 있다.
−
== Sequential 사용 ==
+
이외 클래스 상속을 통해 모델을 정의하여 객체지향형으로 사용할 수도 있는데, 이 때에도 역시 연쇄형, 분산형으로 나뉜다.
−
keras.models.Sequential() 순차적으로 쌓일 모델 객체를 생성한다.<syntaxhighlight lang="python">
+
{| class="wikitable"
+
|+
+
!방식
+
!설명
+
!한계
+
!예시
+
|-
+
|연쇄방식
+
(순차적 모델)
+
|순차적으로 쌓는 직관적 방식.
+
간단해서 별다른 설명이 필요치 않다.
+
|
+
* 모델에 하나의 출력, 하나의 입력만 가능.
+
* 레이어를 공유하는 형태 불가.
+
* 차례대로 층을 쌓는 간단한 방식만 가능.
+
|<syntaxhighlight lang="python">
from keras.models import Sequential
from keras.models import Sequential
from keras import layers # 층을 불러온다.
from keras import layers # 층을 불러온다.
78번째 줄:
101번째 줄:
model = Sequential()
model = Sequential()
# 필요한 층을 더한다.
# 필요한 층을 더한다.
−
model.add(layers.LSTM(50, return_sequences=True, input_shape=(50,1))) # 50개 유닛을 갖는 LSTM층 생성.
+
model.add(layers.LSTM(50, return_sequences=True, input_shape=(50,1)))
−
# 50개 OUT.
+
# 50개 유닛을 갖는 LSTM층 생성. 50개 OUT.
−
model.add(layers.Dense(1, activation='linear')) # 일반 뉴런층 1개. 결과가 1개가 나온다.
+
model.add(layers.Dense(1, activation='linear'))
−
# 1개 OUT.
+
# 일반 뉴런층 1개. 결과가 1개가 나온다.
model.compile(loss='mse', optimizer='rmsprop') # 손실함수 따위를 결정한다.
model.compile(loss='mse', optimizer='rmsprop') # 손실함수 따위를 결정한다.
</syntaxhighlight>
</syntaxhighlight>
+
|-
+
|분산방식
+
(함수형 API)
+
|함수처럼 생겨서 함수형 API라 부른다.
+
|
+
|<syntaxhighlight lang="python">
+
from keras.models import Model
+
from keras import layers # 층을 불러온다.
+
from keras import Input
+
+
input = Input(shape=(50,1))
+
x = layers.Dense(50, activation='relu')(input)
+
predict = layers.LSTM(50, activation='relu')(x)
+
+
model = Model(input, predict) # 인풋과 아웃풋을 지정하여 모델을 설정한다.
+
# 인풋이나 아웃풋이 여러개인 경우 리스트로 넣을 수 있다.
+
+
</syntaxhighlight>
+
|-
+
|
+
|
+
|
+
|
+
|}
+
+
== 분산방식(함수형 API 사용) ==
+
다음의 예시를 참고하면 다양하게 활용할 수 있을 것이다.
+
+
함수형 API를 사용하면 다중 입력, 다중 출력을 갖는 모델을 만들 수 있다. 혹은 하나의 층을 여러 출력에서 공유할 수도 있고.
+
+
=== 인풋이 여러 개인 경우 ===
+
<syntaxhighlight lang="python">
+
from keras import layers, models
+
+
input = layers.Input(shape(50,200,200))
+
input_2 = layers.Input(shape(50,200,200))
+
concatenated = layers.Concatenate(axis=-1)([input, input_2])
+
x = layers.Dense(32, activation='relu')(concatenated)
+
output = layers.Dense(10, activation='softmax')(x)
+
+
model = models.Model(inputs=[input, input_2], outputs=output) # 모델생성.
+
</syntaxhighlight><code>axis=</code>에 주어지는 숫자는 input_shape의 어떤 곳에 추가할 것이냐에 대한 결정값이다. 2번째 차원방향으로 추가할 거라면 axis=1을 넣어주면 된다.
+
+
== 이외 팁 ==
=== input ===
=== input ===
128번째 줄:
195번째 줄:
</syntaxhighlight>
</syntaxhighlight>
|}
|}
−
−
== 함수형 API 사용 ==
−
함수형 API를 사용하면 다중 입력, 다중 출력을 갖는 모델을 만들 수 있다. 혹은 하나의 층을 여러 출력에서 공유할 수도 있고.<syntaxhighlight lang="python">
−
from keras import layers, models
−
−
input = layers.Input(shape(50,200,200))
−
x = layers.Dense(32, activation='relu')(Input)
−
output = layers.Dense(10, activation='softmax')(x)
−
−
model = models.Model(inputs=input, outputs=output) # 모델생성.
−
</syntaxhighlight>
= 모델 교육 =
= 모델 교육 =
150번째 줄:
206번째 줄:
metrics= ['accuracy']) # 모니터링 지표.
metrics= ['accuracy']) # 모니터링 지표.
</syntaxhighlight>
</syntaxhighlight>
+
== 교육 ==
== 교육 ==
161번째 줄:
218번째 줄:
</syntaxhighlight>history.history 안의 loss, val_loss 등의 속성에 접근하여 교육 결과를 살펴볼 수 있다.
</syntaxhighlight>history.history 안의 loss, val_loss 등의 속성에 접근하여 교육 결과를 살펴볼 수 있다.
−
== 저장 ==
= 모델평가 =
= 모델평가 =
197번째 줄:
253번째 줄:
디렉터리 형태로 저장된다.<syntaxhighlight lang="python">
디렉터리 형태로 저장된다.<syntaxhighlight lang="python">
model.save('\경로\디렉토리명')
model.save('\경로\디렉토리명')
−
</syntaxhighlight>
+
</syntaxhighlight>디렉토리를 생성하기 때문에 관리자 권한이 필요하다.
+
+
어째서인지.... 경로를 하나의 텍스트가 아니라 +로 잇는 방식을 사용하면 \가 /로 바뀌어버리고 만다.(그래서 윈도우에선 에러가 난다;) 때문에 하나의 따옴표쌍 안에 경로를 수기로 입력해야 한다....
== 모델 불러오기 ==
== 모델 불러오기 ==