본문 바로가기

필사필요코드

캐글의 bert 최종 전처리

반응형
#전처리할 컬럼
options = 'ABCDE'
# 컬럼의 갯수
indices = list(range(5))

option_to_index = {option: index for option, index in zip(options, indices)}
index_to_option = {index: option for option, index in zip(options, indices)}

def preprocess(example):
    # The AutoModelForMultipleChoice class expects a set of question/answer pairs
    # so we'll copy our question 5 times before tokenizing
    first_sentence = [example['prompt']] * 5
    
    second_sentence = []
    for option in options:
        second_sentence.append(example[option])
    # Our tokenizer will turn our text into token IDs BERT can understand
    tokenized_example = tokenizer(first_sentence, second_sentence, truncation=True)
    tokenized_example['label'] = option_to_index[example['answer']]
    return tokenized_example

tokenized_train_ds = train_ds.map(preprocess, batched=False, remove_columns=['prompt', 'A', 'B', 'C', 'D', 'E', 'answer'])
반응형

'필사필요코드' 카테고리의 다른 글

캐글 결측치 컬럼별 출력  (0) 2024.03.03
seaborn 시본 형제수에 따른 생존률  (0) 2024.03.03
캐글의 bert 최종 전처리  (0) 2024.03.03
10진수를 n진수로 변환  (0) 2024.02.29
최대공략수 최소공배수  (0) 2024.02.29