강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của bestshoot
bestshoot

câu hỏi đã được viết

Khóa học xử lý ngôn ngữ tự nhiên (NLP) sử dụng trí tuệ nhân tạo (Deep Learning) (từ cơ bản đến ChatGPT/mô hình tạo)

Thực hành - Phân tích code máy dịch Transformer (Code cũ) - Tùy chọn

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

Viết

·

310

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인 이유가 궁금합니다.

딥러닝tensorflowNLP

Câu trả lời 2

1

YoungJea Oh님의 프로필 이미지
YoungJea Oh
Người chia sẻ kiến thức

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
Người đặt câu hỏi

감사합니다!

Hình ảnh hồ sơ của bestshoot
bestshoot

câu hỏi đã được viết

Đặt câu hỏi