File size: 6,606 Bytes
435eb54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env python
# coding: utf-8

# # Creating a Zero-Shot classifier based on BETO
# 
# This notebook/script fine-tunes a BETO (spanish bert, 'dccuchile/bert-base-spanish-wwm-cased') model on the spanish XNLI dataset.
# The fine-tuned model can then be fed to a Huggingface ZeroShot pipeline to obtain a ZeroShot classifier.

# In[ ]:


from datasets import load_dataset, Dataset, load_metric, load_from_disk
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import Trainer, TrainingArguments
import torch
from pathlib import Path
# from ray import tune
# from ray.tune.suggest.hyperopt import HyperOptSearch
# from ray.tune.schedulers import ASHAScheduler


# # Prepare the datasets

# In[ ]:


xnli_es = load_dataset("xnli", "es")


# In[ ]:


xnli_es


# >joeddav
# >Aug '20
# >
# >@rsk97 In addition, just make sure the model used is trained on an NLI task and that the **last output label corresponds to entailment** while the **first output label corresponds to contradiction**.
# 
# => We change the original `label` and use the `labels` column, which is required by a `AutoModelForSequenceClassification`

# In[ ]:


# see markdown above
def switch_label_id(row):
    if row["label"] == 0:
        return {"labels": 2}
    elif row["label"] == 2:
        return {"labels": 0}
    else:
        return {"labels": 1}

for split in xnli_es:
    xnli_es[split] = xnli_es[split].map(switch_label_id)


# ## Tokenize data

# In[ ]:


tokenizer = AutoTokenizer.from_pretrained("dccuchile/bert-base-spanish-wwm-cased")


# In a first attempt i padded all data to the maximum length of the dataset (379). However, the traninig takes substanially longer with all the paddings, it's better to pass in the tokenizer to the `Trainer` and let the `Trainer` do the padding on a batch level.

# In[ ]:


# Figured out max length of the dataset manually
# max_length = 379
def tokenize(row):
    return tokenizer(row["premise"], row["hypothesis"], truncation=True, max_length=512)  #, padding="max_length", max_length=max_length)


# In[ ]:


data = {}
for split in xnli_es:
    data[split] = xnli_es[split].map(
        tokenize, 
        remove_columns=["hypothesis", "premise", "label"], 
        batched=True, 
        batch_size=128
    )


# In[ ]:


train_path = str(Path("./train_ds").absolute())
valid_path = str(Path("./valid_ds").absolute())

data["train"].save_to_disk(train_path)
data["validation"].save_to_disk(valid_path)


# In[ ]:


# We can use `datasets.Dataset`s directly

# class XnliDataset(torch.utils.data.Dataset):
#     def __init__(self, data):
#         self.data = data

#     def __getitem__(self, idx):
#         item = {key: torch.tensor(val) for key, val in self.data[idx].items()}
#         return item

#     def __len__(self):
#         return len(self.data)


# In[ ]:


def trainable(config):
    metric = load_metric("xnli", "es")

    def compute_metrics(eval_pred):
        predictions, labels = eval_pred
        predictions = predictions.argmax(axis=-1)
        return metric.compute(predictions=predictions, references=labels)
    
    model = AutoModelForSequenceClassification.from_pretrained("dccuchile/bert-base-spanish-wwm-cased", num_labels=3)

    training_args = TrainingArguments(
        output_dir='./results',          # output directory
        do_train=True,
        do_eval=True,
        evaluation_strategy="steps",
        eval_steps=500,
        load_best_model_at_end=True,
        metric_for_best_model="eval_accuracy",
        num_train_epochs=config["epochs"],              # total number of training epochs
        per_device_train_batch_size=config["batch_size"],  # batch size per device during training
        per_device_eval_batch_size=config["batch_size_eval"],   # batch size for evaluation
        warmup_steps=config["warmup_steps"],  # 500
        weight_decay=config["weight_decay"],  # 0.001               # strength of weight decay
        learning_rate=config["learning_rate"],  # 5e-05
        logging_dir='./logs',            # directory for storing logs
        logging_steps=250,
        #save_steps=500,  # ignored when using load_best_model_at_end
        save_total_limit=10,
        no_cuda=False,
        disable_tqdm=True,
    )
    
#     train_dataset = XnliDataset(load_from_disk(config["train_path"]))
#     valid_dataset = XnliDataset(load_from_disk(config["valid_path"]))
    train_dataset = load_from_disk(config["train_path"])
    valid_dataset = load_from_disk(config["valid_path"])

    
    trainer = Trainer(
        model,
        tokenizer=tokenizer,
        args=training_args,                  # training arguments, defined above
        train_dataset=train_dataset,         # training dataset
        eval_dataset=valid_dataset,          # evaluation dataset
        compute_metrics=compute_metrics,
    )
    
    trainer.train()


# In[ ]:


trainable(
    {
        "train_path": train_path,
        "valid_path": valid_path,
        "batch_size": 16,
        "batch_size_eval": 64,
        "warmup_steps": 500,
        "weight_decay": 0.001,
        "learning_rate": 5e-5,
        "epochs": 3,
    }
)


# # HPO

# In[ ]:


# config = {
#     "train_path": train_path,
#     "valid_path": valid_path,
#     "warmup_steps": tune.randint(0, 500),
#     "weight_decay": tune.loguniform(0.00001, 0.1),
#     "learning_rate": tune.loguniform(5e-6, 5e-4),
#     "epochs": tune.choice([2, 3, 4])
# }


# # In[ ]:


# analysis = tune.run(
#     trainable,
#     config=config,
#     metric="eval_acc",
#     mode="max",
#     #search_alg=HyperOptSearch(),
#     #scheduler=ASHAScheduler(),
#     num_samples=1,
# )


# # In[ ]:


# def model_init():
#     return AutoModelForSequenceClassification.from_pretrained("dccuchile/bert-base-spanish-wwm-cased", num_labels=3)

# trainer = Trainer(
#     args=training_args,                  # training arguments, defined above
#     train_dataset=train_dataset,         # training dataset
#     eval_dataset=valid_dataset,          # evaluation dataset
#     model_init=model_init,
#     compute_metrics=compute_metrics,
# )


# # In[ ]:


# best_trial = trainer.hyperparameter_search(
#     direction="maximize",
#     backend="ray",
#     n_trials=2,
#     # Choose among many libraries:
#     # https://docs.ray.io/en/latest/tune/api_docs/suggestion.html
#     search_alg=HyperOptSearch(mode="max", metric="accuracy"),
#     # Choose among schedulers:
#     # https://docs.ray.io/en/latest/tune/api_docs/schedulers.html
#     scheduler=ASHAScheduler(mode="max", metric="accuracy"),
#     local_dir="tune_runs",
# )