순차 모델 이해하기

Keras로 배우는 Machine Translation

Thushan Ganegedara

Data Scientist and Author

시계열 입력과 순차 모델

  • 문장은 시계열 입력입니다
    • 현재 단어는 이전 단어의 영향을 받습니다
    • 예: He went to the pool for a ....
  • 인코더/디코더는 머신러닝 모델을 사용합니다
    • 시계열 입력에서 학습하는 모델
    • 이를 순차 모델이라 합니다
Keras로 배우는 Machine Translation

순차 모델

  • 순차 모델
    • 각 시간 단계마다 출력을 내며 입력을 순차 처리합니다

순차 모델 아키텍처

Keras로 배우는 Machine Translation

순차 모델로서의 인코더

  • GRU - Gated Recurrent Unit

게이트 순환 유닛

Keras로 배우는 Machine Translation

GRU 레이어 소개

시간 단계 1에서 GRU 레이어는

  • 입력 "We"를 사용합니다
  • 초기 상태 (0,0)을 사용합니다
  • 새 상태 (0.8, 0.3)를 출력합니다

GRU 1

Keras로 배우는 Machine Translation

GRU 레이어 소개

시간 단계 2에서 GRU 레이어는

  • 입력 "like"를 사용합니다
  • 초기 상태 (0.8,0.3)을 사용합니다
  • 새 상태 (0.5, 0.9)를 출력합니다

은닉 상태는 모델이 본 내용을 "기억"합니다

GRU 2

Keras로 배우는 Machine Translation

Keras(Functional API) 복습

  • Keras에는 두 가지 핵심 객체가 있습니다: Layer, Model
  • 입력 레이어
    • inp = keras.layers.Input(shape=(...))
  • 은닉 레이어
    • layer = keras.layers.GRU(...)
  • 출력
    • out = layer(inp)
  • 모델
    • model = Model(inputs=inp, outputs=out)
Keras로 배우는 Machine Translation

데이터 형태 이해하기

  • 시계열 입력은 3차원입니다
    • 배치 차원(예: 문장 묶음)
    • 시간 차원 - 시퀀스 길이
    • 입력 차원(예: 원-핫 벡터 길이)
  • GRU 모델 입력 형태
    • (Batch, Time, Input)
    • (배치 크기, 시퀀스 길이, 원-핫 길이)

입력 데이터

Keras로 배우는 Machine Translation

Keras로 GRU 구현하기

Keras 레이어 정의

inp = keras.layers.Input(batch_shape=(2,3,4))
gru_out = keras.layers.GRU(10)(inp)

Keras 모델 정의

model = keras.models.Model(inputs=inp, outputs=gru_out)
Keras로 배우는 Machine Translation

Keras로 GRU 구현하기

Keras 모델로 예측하기

x = np.random.normal(size=(2,3,4))
y = model.predict(x)
print("shape (y) =", y.shape, "\ny = \n", y)
shape (y) = (2, 10) 
y = 
[[ 0.2576233   0.01215531  ... -0.32517594  0.4483121 ],
 [ 0.54189587 -0.63834655  ... -0.4339783   0.4043917 ]]
Keras로 배우는 Machine Translation

Keras로 GRU 구현하기

배치 내 샘플 수가 가변인 GRU

inp = keras.layers.Input(shape=(3,4))
gru_out = keras.layers.GRU(10)(inp)
model = keras.models.Model(inputs=inp, outputs=gru_out)
x = np.random.normal(size=(5,3,4))
y = model.predict(x)
print("y = \n", y)
y = 
 [[-1.3941444e-02 -3.3123985e-02 ... 6.5081201e-02  1.1245312e-01]
 [ 1.1409521e-03  3.6983326e-01 ... -3.4610277e-01 -3.4792548e-01]
 [ 2.5911796e-01 -3.9517123e-01 ... 5.8505309e-01  3.6908010e-01]
 [-2.8727052e-01 -5.1150680e-02 ... -1.9637148e-01 -1.5587148e-01]
 [ 3.1303680e-01  2.3338445e-01 ... 9.1499090e-04 -2.0590121e-01]]
Keras로 배우는 Machine Translation

GRU 레이어의 return_state 인수

inp = keras.layers.Input(batch_shape=(2,3,4))
gru_out2, gru_state = keras.layers.GRU(10, return_state=True)(inp)
print("gru_out2.shape = ", gru_out2.shape)
print("gru_state.shape = ", gru_state.shape)
gru_out2.shape =  (2, 10)
gru_state.shape =  (2, 10)

GRU return_state

Keras로 배우는 Machine Translation

GRU 레이어의 return_sequences 인수

inp = keras.layers.Input(batch_shape=(2,3,4))
gru_out3 = keras.layers.GRU(10, return_sequences=True)(inp)
print("gru_out3.shape = ", gru_out2.shape)
gru_out3.shape =  (2, 3, 10)

GRU return_sequences

Keras로 배우는 Machine Translation

연습해 봅시다!

Keras로 배우는 Machine Translation

Preparing Video For Download...