Dense 및 TimeDistributed 레이어

Keras로 배우는 Machine Translation

Thushan Ganegedara

Data Scientist and Author

Dense 레이어 소개

  • 입력 벡터를 받아 확률 예측으로 변환합니다.
    • y = Weights.x + Bias

가중치와 바이어스

Keras로 배우는 Machine Translation

Dense 레이어 이해하기

Dense 레이어 정의와 사용

dense = Dense(3, activation='softmax')
inp = Input(shape=(3,))
pred = dense(inp)
model = Model(inputs=inp, outputs=pred)

사용자 지정 초기화로 Dense 레이어 정의

from tensorflow.keras.initializers import RandomNormal
init = RandomNormal(mean=0.0, stddev=0.05, seed=6000)
dense = Dense(3, activation='softmax', 
             kernel_initializer=init, bias_initializer=init)
Keras로 배우는 Machine Translation

Dense 레이어의 입력과 출력

  • Dense softmax 레이어
    • (배치 크기, 입력 크기) 배열을 입력으로 받음
      • 예: x = [[1, 6, 8], [8, 9, 10]] # 2x3 배열
    • (배치 크기, 클래스 수) 배열을 출력함
      • 예: 클래스 수 = 4
      • 예: y = [[0.1, 0.3, 0.4, 0.2], [0.2, 0.5, 0.1, 0.2]] # 2x4 배열
    • 각 샘플의 출력은 클래스에 대한 확률 분포
      • 열 방향 합이 1
    • np.argmax(y, axis=-1)로 각 샘플의 클래스를 얻을 수 있음
      • 예: np.argmax(y,axis=-1) → [2,1]
Keras로 배우는 Machine Translation

TimeDistributed 레이어 이해하기

  • TimeDistributed는 Dense 레이어가 시계열 입력을 처리하도록 합니다
dense_time = TimeDistributed(Dense(3, activation='softmax'))
inp = Input(shape=(2, 3))
pred = dense_time(inp)
model = Model(inputs=inp, outputs=pred)
Keras로 배우는 Machine Translation

TimeDistributed 레이어의 입력과 출력

  • (배치 크기, 시퀀스 길이, 입력 크기) 배열을 입력으로 받음
x = [[[1, 6], [8, 2], [1, 2]], 
    [[8, 9], [10, 8], [1, 0]]] # a 2x3x2 array
  • (배치 크기, 시퀀스 길이, 클래스 수) 배열을 출력함
    • 예: 클래스 수 = 3
y = [[[0.1, 0.5, 0.4], [0.8, 0.1, 0.1], [0.6, 0.2, 0.2]], 
     [[0.2, 0.5, 0.3], [0.2, 0.5, 0.3], [0.2, 0.8, 0.0]]] # a 2x3x3 array
  • 각 샘플의 출력은 클래스에 대한 확률 분포
  • np.argmax(y, axis=-1)로 각 샘플의 클래스를 얻을 수 있음
Keras로 배우는 Machine Translation

시간 차원 슬라이싱

y = [[[0.1, 0.5, 0.4], [0.8, 0.1, 0.1], [0.6, 0.2, 0.2]], 
     [[0.2, 0.5, 0.3], [0.2, 0.5, 0.3], [0.2, 0.8, 0.0]]] # a 2x3x3 array
classes = np.argmax(y, axis=-1) # a 2 x 3 array

시간 분포 데이터 반복 처리

for t in range(3):
  # Get the t-th time-dimension slice of y and classes
  for prob, c in zip(y[:,t,:], classes[:,t]):
     print("Prob: ", prob, ", Class: ", c)
Prob:  [0.1 0.5 0.4] , Class:  1
Prob:  [0.2 0.5 0.3] , Class:  1
Prob:  [0.8 0.1 0.1] , Class:  0
...
Keras로 배우는 Machine Translation

연습해 봅시다!

Keras로 배우는 Machine Translation

Preparing Video For Download...