본문 바로가기
AI/딥러닝 기초(Deep learning)

[Pytorch][BERT] 버트 소스코드 이해_⑧ BERT model 입력값

by Hyen4110 2022. 10. 28.
반응형

[Pytorch][BERT] 버트 소스코드 이해 목차

BERT  📑 BERT Config    
  📑 BERT Tokenizer      
  📑 BERT Model 📑 BERT Input 👀    
    📑 BERT Output    
    📑 BERT Embedding    
    📑 BERT Pooler    
    📑 BERT Enocder 📑 BERT Layer 📑 BERT SelfAttention
        📑 BERT SelfOtput

 

BertModel 입력값

- BERT 모델을 생성한 후, forward 함수에서 필요한 입력값

class BertModel(BertPreTrainedModel):
			def forward(
			        self,
			        input_ids,
			        attention_mask,
			        token_type_ids,
			        position_ids,
			        head_mask,
			        inputs_embeds,
			        encoder_hidden_states,
			        encoder_attention_mask,
			        past_key_values,
			        use_cache,
			        output_attentions,
			        output_hidden_states,
			        return_dict,

 

🍅입력 값들!

  변수명 type 설명
1 input_ids torch.LongTensor BertTokenizer로부터 얻어지는 sequence 토큰들의 vocabulary index 값
2 attention_mask

torch.FloatTensor (zero) 패딩된 토큰값에 대한 attention 연산을 피하기 위한 마스크 (0: 마스킹됨, 1: 안됨)
3 token_type_ids torch.LongTensor A, B문장에 대해 나누기 위해 부여되는 토큰
{'input_ids': tensor([[ 101, 6207, 2111, 2300,  102],
                    [ 101, 2111, 6207, 2300,  102],
                    [ 101, 2300, 6207, 2111,  102]]), 

'token_type_ids': tensor([[0, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0],
                        [0, 0, 0, 0, 0]]), 

'attention_mask': tensor([[1, 1, 1, 1, 1],
                        [1, 1, 1, 1, 1],
                        [1, 1, 1, 1, 1]])
}

 

 

  변수명 type 설명
3 position_ids torch.LongTensor position embedding에서 각 입력토큰의 위치 index값들
4 head_mask

torch.FloatTensor  self-attention 모듈에서 특정 head를 반영하지 않기 위해 마스킹
- shape : (num_heads,) or (num_layers, num_heads)
5 inputs_embeds torch.FloatTensor (optional) input_ids를 넣는 대신에 이 값을 넣을 수 있음
6 output_attentions bool (optional) 모든 attention layer의 attention 텐서 출력 여부
7 output_hidden_states bool (optional) 모든 laeyr의 hidden state 출력 여부
8 return_dict bool (optional) ModelOutput 클래스를 반환 할 것인지 아닌지 여부 

반응형

댓글