바뀜

1,091 바이트 추가됨 ,  2022년 4월 19일 (화) 22:54
80번째 줄: 80번째 줄:     
이외 클래스 상속을 통해 모델을 정의하여 객체지향형으로 사용할 수도 있는데, 이 때에도 역시 연쇄형, 분산형으로 나뉜다.
 
이외 클래스 상속을 통해 모델을 정의하여 객체지향형으로 사용할 수도 있는데, 이 때에도 역시 연쇄형, 분산형으로 나뉜다.
 
+
{| class="wikitable"
== 연쇄방식 ==
+
|+
keras.models.Sequential() 순차적으로 쌓일 모델 객체를 생성한다.<syntaxhighlight lang="python">
+
!방식
 +
!설명
 +
!한계
 +
!예시
 +
|-
 +
|연쇄방식
 +
(순차적 모델)
 +
|순차적으로 쌓는 직관적 방식.
 +
간단해서 별다른 설명이 필요치 않다.
 +
|
 +
* 모델에 하나의 출력, 하나의 입력만 가능.
 +
* 레이어를 공유하는 형태 불가.
 +
* 차례대로 층을 쌓는 간단한 방식만 가능.
 +
|<syntaxhighlight lang="python">
 
from keras.models import Sequential
 
from keras.models import Sequential
 
from keras import layers  # 층을 불러온다.
 
from keras import layers  # 층을 불러온다.
88번째 줄: 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.concatenated([input, input_2], axis=-1)  # 두 인풋을 합친다.
 +
x = layers.Dense(32, activation='relu')(concatenated)
 +
output = layers.Dense(10, activation='softmax')(x)
 +
 +
model = models.Model(inputs=[input, input_2], outputs=output)  # 모델생성.
 +
</syntaxhighlight>
 +
 +
== 이외 팁 ==
    
=== input ===
 
=== input ===
138번째 줄: 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>
      
= 모델 교육 =
 
= 모델 교육 =
160번째 줄: 206번째 줄:  
                 metrics= ['accuracy'])              # 모니터링 지표.
 
                 metrics= ['accuracy'])              # 모니터링 지표.
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
    
== 교육 ==
 
== 교육 ==
170번째 줄: 217번째 줄:     
</syntaxhighlight>history.history 안의 loss, val_loss 등의 속성에 접근하여 교육 결과를 살펴볼 수 있다.
 
</syntaxhighlight>history.history 안의 loss, val_loss 등의 속성에 접근하여 교육 결과를 살펴볼 수 있다.
 +
    
= 모델평가 =
 
= 모델평가 =