NLP huggingface model에 input을 제대로 넣었는데 IndexError: index out of range in self가 나는 이유

데이터셋을 다음과 같이 구현하고

 

def preprocess(data):

    preprocessed_data = []

    for i in range(len(data)):

        d = data[i]

        knowledge = ' '.join(d['knowledge'])

        query = f"질문: {d['query']}\n지식: {knowledge}"
        answer = f"{d['answer']}"

        preprocessed_data.append((i,query,answer))

    return preprocessed_data

 

 

T5 토크나이저, 모델 불러온 다음

 

tokenizer = T5Tokenizer.from_pretrained('digit82/kolang-t5-base')
model = T5ForConditionalGeneration.from_pretrained('digit82/kolang-t5-base')

 

 

데이터를 하나 토크나이징하면 input_ids, attention_mask가 나오는데, 이거랑 label을 토크나이징해서 넣어보면

model(input_ids = tokenizer(preprocessed_train_data[1][1],max_length = 128,padding = "max_length",truncation=True,return_tensors='pt')['input_ids'],
      attention_mask = tokenizer(preprocessed_train_data[1][1],max_length = 128,padding = "max_length",truncation=True,return_tensors='pt')['attention_mask'],
      labels = tokenizer(preprocessed_train_data[1][2],max_length = 128,padding = "max_length",truncation=True,return_tensors='pt')['input_ids']).loss

 

 

이런식으로 에러가 난다

 

 

 

문제는 토크나이저에 있다

 

토크나이저의 vocab_size를 보면 35000인데

 

 

 

padding 토큰이 35100으로 설정되어있다

 

 

모델이 0~34999까지만 인식하는데, input으로 들어오는 데이터에 35100이 있으니까

 

이해를 못해서 인덱스 에러가 나는거

 

토크나이저 다시 훈련시키거나 하면 되는것 같은데

 

처음에 불러온 토크나이저는 T5Tokenizer였고, 보통 일반적으로 쓰는 토크나이저는 뒤에 Fast가 붙은 형태이다

 

tokenizer = T5TokenizerFast.from_pretrained('digit82/kolang-t5-base')

 

 

T5TokenizerFast는 설정이 제대로 되어있다

 

vocab size가 35100이고 pad token이 0번으로 설정되어있음

 

 

 

이러면 모델이 값을 잘 내주긴한다

 

TAGS.

Comments