File size: 2,662 Bytes
c776352
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from datasets import load_dataset, DatasetDict
from evaluate import load
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer
import torch
import numpy as np

labels = {
    'contradiction': 0,
    'neutral': 1,
    'entailment': 2,
}

datasets = load_dataset('seongs1024/DKK-nli')
datasets = DatasetDict({
    'train': datasets['train'].shard(num_shards=100, index=0),
    'validation': datasets['validation'].shard(num_shards=100, index=0),
})

metric = load('glue', 'mnli')
def compute_metrics(eval_pred):
    predictions, labels = eval_pred
    predictions = np.argmax(predictions, axis=1)
    return metric.compute(predictions=predictions, references=labels)

check_point = 'klue/roberta-small'
model = AutoModelForSequenceClassification.from_pretrained(check_point, num_labels=3)
tokenizer = AutoTokenizer.from_pretrained(check_point)

def preprocess_function(examples):
    return tokenizer(
        examples['premise'],
        examples['hypothesis'],
        truncation=True,
        return_token_type_ids=False,
    )
encoded_datasets = datasets.map(preprocess_function, batched=True)

batch_size = 8
args = TrainingArguments(
    "test-nli",
    evaluation_strategy="epoch",
    save_strategy='epoch',
    learning_rate=2e-5,
    per_device_train_batch_size=batch_size,
    per_device_eval_batch_size=batch_size,
    num_train_epochs=5,
    weight_decay=0.01,
    load_best_model_at_end=True,
    metric_for_best_model='accuracy',
)

trainer = Trainer(
    model,
    args,
    train_dataset=encoded_datasets["train"],
    eval_dataset=encoded_datasets["validation"],
    tokenizer=tokenizer,
    compute_metrics=compute_metrics,
)

trainer.train()
trainer.evaluate()

trainer.save_model('./model')

# features = tokenizer(
#     [
#         'A man is eating pizza',
#         'A black race car starts up in front of a crowd of people.',
#         '์˜ค๋Š˜ ๋ง›์žˆ๋Š” ๋ฐฅ์„ ๋จน์—ˆ์–ด ๊ทผ๋ฐ ์ด๊ฒŒ ์–ด๋–ป๊ฒŒ ๋ง›์žˆ๋‹ค ์„ค๋ช…ํ•˜๊ธฐ๋Š” ์ข€ ์• ๋งคํ•ด.',
#         '๋‚˜ ์ง‘์— ๊ฐ€๋Š” ์ค‘.',
#     ],
#     [
#         'A man eats something',
#         'A man is driving down a lonely road.',
#         '์˜ค๋Š˜ ๋ง›์—†๋Š” ๋ฐฅ์„ ๋จน์—ˆ์–ด ๊ทผ๋ฐ ์ด๊ฒŒ ์–ด๋–ป๊ฒŒ ๋ง›์žˆ๋‹ค ์„ค๋ช…ํ•˜๊ธฐ๋Š” ์ข€ ์• ๋งคํ•ด.',
#         '๋‚˜ ์ง‘์— ๋„์ฐฉํ–ˆ์–ด.',
#     ],
#     padding=True,
#     truncation=True,
#     return_tensors="pt"
# )
# print(features)

# model.eval()
# with torch.no_grad():
#     scores = model(**features).logits
#     print(scores)
#     label_mapping = ['contradiction', 'entailment', 'neutral']
#     labels = [label_mapping[score_max] for score_max in scores.argmax(dim=1)]
#     print(labels)