• 카테고리

    질문 & 답변
  • 세부 분야

    자연어 처리

  • 해결 여부

    미해결

Decoder 전체(10) 부분에서 attn_weight output shape 관련 질문 드립니다.

23.08.04 17:40 작성 조회수 156

0

sample_decoder = Decoder(num_layers=2, d_model=512, num_heads=8,
                         dff=2048, target_vocab_size=8000,
                         maximum_position_encoding=5000)

x = tf.random.uniform((64, 26), dtype=tf.int64, minval=0, maxval=200)

output, attn = sample_decoder(x,
                              enc_output=sample_encoder_output,
                              training=False,
                              look_ahead_mask=None,
                              padding_mask=None)

output.shape, attn['decoder_layer2_block2'].shape

위 코드 결과 output은

(TensorShape([64, 26, 512]), TensorShape([64, 8, 26, 62]))

인데요.

atten output size 64 x 8 x 26 x 62는

batch x head num x seq_len x depth(=len_k) 의 사이즈일것 같은데요.

depth의 경우, d_model(512) / num_head(8) = 64 가 되야하는게 아닌지요? 62인 이유가 궁금합니다.

답변 2

·

답변을 작성해보세요.

1

62는 입력 시퀀스의 길이에 입니다.

sample_encoder = Encoder(num_layers=2, d_model=512, num_heads=8, dff=2048, input_vocab_size=8500, maximum_position_encoding=10000)

x = tf.random.uniform((64, 62), dtype=tf.int64, minval=0, maxval=200)

sample_encoder_output = sample_encoder(x, training=False, mask=None)

print (sample_encoder_output.shape) # (batch_size, input_seq_len, d_model)
----> (64, 62, 512) 출력

트랜스포머에서, Self-Attention의 output shape은 일반적으로 (batch_size, num_heads, sequence_length, depth)입니다. 여기서 'depth'는 주로 d_model/num_heads로 계산되는데, 이는 각 attention head가 처리하는 feature의 차원수입니다. (62 라는 숫자는 단순히 예제로 정한 숫자입니다.)

Self-Attention 메커니즘에서, attention score는 Query와 Key 사이의 dot product로 계산되므로, Query와 Key의 길이가 동일해야합니다. 이 경우, Key(즉, 입력 시퀀스)의 길이가 62라면, attention weight 행렬의 마지막 차원도 62가 될 것입니다.

좋은 질문 감사합니다.

0

David님의 프로필

David

질문자

2023.08.07

감사합니다!