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

set num_labels=1 as argument.If you want to learn more about the arguments the pre-trainedmodels may take, please check the documentation onconfiguration: PretrainedConfig. [210]To learn more about the outputs of several pre-trained models,please check the documentation on model outputs. [211]The ForSequenceClassification models add a single linear layer (classifier) on topof the pooled output from the underlying base model to produce the logitsoutput.More AutoModelsIf you want to quickly try different fine-tuning models without having to importtheir corresponding classes, you can use HuggingFace’s AutoModelcorresponding to a fine-tuning task:from transformers import AutoModelForSequenceClassificationauto_cls = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)print(auto_cls.__class__)Output<class 'transformers.modeling_distilbert.DistilBertForSequenceClassification'>As you can see, it infers the correct model class based on the name of themodel you’re loading, e.g., distilbert-base-uncased.We already have a model, let’s look at our dataset…Fine-Tuning with HuggingFace | 991

Tokenized DatasetThe training and test datasets are HF’s Datasets, and, finally, we’ll keep them likethat instead of building TensorDatasets out of them. We still have to tokenizethem, though:Data Preparation1 auto_tokenizer = AutoTokenizer.from_pretrained(2 'distilbert-base-uncased'3 )4 def tokenize(row):5 return auto_tokenizer(row['sentence'],6 truncation=True,7 padding='max_length',8 max_length=30)We load a pre-trained tokenizer and build a simple function that takes one rowfrom the dataset and tokenizes it. So far, so good, right?IMPORTANT: The pre-trained tokenizer and pre-trained modelmust have matching architectures—in our case, both are pretrainedon distilbert-base-uncased.Next, we use the map() method of HF’s Dataset to create new columns by usingour tokenize() function:Data Preparation1 tokenized_train_dataset = train_dataset.map(2 tokenize, batched=True3 )4 tokenized_test_dataset = test_dataset.map(tokenize, batched=True)The batched argument speeds up the tokenization, but the tokenizer must returnlists instead of tensors (notice the missing return_tensors='pt' in the call toauto_tokenizer):print(tokenized_train_dataset[0])992 | Chapter 11: Down the Yellow Brick Rabbit Hole

set num_labels=1 as argument.

If you want to learn more about the arguments the pre-trained

models may take, please check the documentation on

configuration: PretrainedConfig. [210]

To learn more about the outputs of several pre-trained models,

please check the documentation on model outputs. [211]

The ForSequenceClassification models add a single linear layer (classifier) on top

of the pooled output from the underlying base model to produce the logits

output.

More AutoModels

If you want to quickly try different fine-tuning models without having to import

their corresponding classes, you can use HuggingFace’s AutoModel

corresponding to a fine-tuning task:

from transformers import AutoModelForSequenceClassification

auto_cls = AutoModelForSequenceClassification.from_pretrained(

'distilbert-base-uncased', num_labels=2

)

print(auto_cls.__class__)

Output

<class 'transformers.modeling_distilbert

.DistilBertForSequenceClassification'>

As you can see, it infers the correct model class based on the name of the

model you’re loading, e.g., distilbert-base-uncased.

We already have a model, let’s look at our dataset…

Fine-Tuning with HuggingFace | 991

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

Saved successfully!

Ooh no, something went wrong!