Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

Output{'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],'input_ids': [101, 1998, 1010, 2061, 2521, 2004, 2027, 2354, 1010,2027, 2020, 3243, 2157, 1012, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0],'labels': 0,'sentence': 'And, so far as they knew, they were quite right.','source': 'wizoz10-1740.txt'}See? Regular Python lists, not PyTorch tensors. It created new columns(attention_mask and input_ids) and kept the old ones (labels, sentence, andsource).But we don’t need all these columns for training; we need only the first three. So,let’s tidy it up by using the set_format() method of Dataset:Data Preparation1 tokenized_train_dataset.set_format(2 type='torch',3 columns=['input_ids', 'attention_mask', 'labels']4 )5 tokenized_test_dataset.set_format(6 type='torch',7 columns=['input_ids', 'attention_mask', 'labels']8 )Not only are we specifying the columns we’re actually interested in, but we’re alsotelling it to return PyTorch tensors:tokenized_train_dataset[0]Fine-Tuning with HuggingFace | 993

Output{'attention_mask': tensor([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),'input_ids': tensor([ 101, 1998, 1010, 2061, 2521, 2004, 2027,2354, 1010, 2027, 2020, 3243, 2157, 1012, 102, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),'labels': tensor(0)}Much better! We’re done with our datasets and can move on to the…TrainerEven though every pre-trained model on HuggingFace can be fine-tuned in nativePyTorch, as we did in the previous section, the library offers an easy-to-useinterface for training and evaluation: Trainer.As expected, it takes a model and a training dataset as required arguments, andthat’s it:from transformers import Trainertrainer = Trainer(model=bert_cls,train_dataset=tokenized_train_dataset)We only need to call the train() method, and our model will be trained! YES, thistrain() method actually trains the model! Thank you, HuggingFace :-)"Awesome! But … does it train for how many epochs? Whichoptimizer and learning rate does it use for training?"We can find it all out by looking at its TrainingArguments:trainer.args994 | Chapter 11: Down the Yellow Brick Rabbit Hole

Output

{'attention_mask': tensor([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),

'input_ids': tensor([ 101, 1998, 1010, 2061, 2521, 2004, 2027,

2354, 1010, 2027, 2020, 3243, 2157, 1012, 102, 0, 0, 0,

0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),

'labels': tensor(0)}

Much better! We’re done with our datasets and can move on to the…

Trainer

Even though every pre-trained model on HuggingFace can be fine-tuned in native

PyTorch, as we did in the previous section, the library offers an easy-to-use

interface for training and evaluation: Trainer.

As expected, it takes a model and a training dataset as required arguments, and

that’s it:

from transformers import Trainer

trainer = Trainer(model=bert_cls,

train_dataset=tokenized_train_dataset)

We only need to call the train() method, and our model will be trained! YES, this

train() method actually trains the model! Thank you, HuggingFace :-)

"Awesome! But … does it train for how many epochs? Which

optimizer and learning rate does it use for training?"

We can find it all out by looking at its TrainingArguments:

trainer.args

994 | Chapter 11: Down the Yellow Brick Rabbit Hole

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!