vector listlengths 1.02k 1.02k | text stringlengths 2 11.8k |
|---|---|
[
-0.00008505015,
-0.010700855,
0.0029312235,
-0.01863887,
0.011319402,
0.044370405,
-0.034528643,
-0.034088787,
0.0029432506,
0.06432884,
0.046679646,
-0.027147321,
0.038597304,
-0.043105822,
-0.013683625,
0.038074978,
-0.019683527,
-0.044892736,
-0.049126342,
-0.020205854,
-0... | You'll start with a [PreTrainedModel] or a torch.nn.Module:
from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased")
[TrainingArguments] contains the model hyperparameters you can change like learning rate, batch s... |
[
0.032283105,
0.029651014,
0.008783331,
0.0021322118,
-0.019529823,
-0.0033191976,
-0.0035573218,
-0.0034900655,
-0.019587992,
0.049297173,
0.02032963,
-0.0003946773,
0.0373437,
-0.03652935,
0.0062966594,
0.05557929,
-0.013414939,
-0.033271957,
-0.05107129,
-0.008943292,
-0.02... | from transformers import TrainingArguments
training_args = TrainingArguments(
output_dir="path/to/save/folder/",
learning_rate=2e-5,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
num_train_epochs=2,
)
Load a preprocessing class like a tokenizer, image proc... |
[
0.0005789264,
-0.009732367,
0.0023797343,
-0.006683888,
-0.0048732976,
-0.0134744905,
0.009696796,
-0.01097737,
-0.020133479,
0.050568465,
-0.00790399,
-0.006075615,
0.033864085,
-0.010159225,
-0.0059013143,
0.013289519,
-0.041917473,
-0.043567993,
-0.068752624,
-0.0036709805,
... | from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased")
Load a dataset:
from datasets import load_dataset
dataset = load_dataset("rotten_tomatoes") # doctest: +IGNORE_RESULT
Create a function to tokenize the dataset:
def tokenize_dataset(datase... |
[
0.017454915,
0.017568259,
0.041030385,
0.01659067,
-0.011036833,
-0.032019567,
0.009768802,
0.0067156083,
-0.018786702,
0.050126206,
-0.005529043,
0.015060531,
0.015117203,
-0.012949507,
-0.020288505,
0.0022208262,
-0.027599167,
-0.07129312,
-0.06483254,
0.0041866293,
-0.0208... | Then apply it over the entire dataset with [~datasets.Dataset.map]:
dataset = dataset.map(tokenize_dataset, batched=True)
A [DataCollatorWithPadding] to create a batch of examples from your dataset:
from transformers import DataCollatorWithPadding
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
... |
[
0.023188729,
0.02015469,
0.015632527,
-0.0036968323,
-0.021036007,
-0.016297126,
0.012143381,
0.00938385,
-0.021281619,
0.055624053,
0.00069575215,
0.0382,
0.013819327,
-0.027161876,
0.002159947,
0.0041392967,
-0.027812026,
-0.07108321,
-0.08576218,
0.0045907903,
-0.031207262... | from transformers import DataCollatorWithPadding
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
Now gather all these classes in [Trainer]:
from transformers import Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=dataset["train"],
eval_dataset=dataset["te... |
[
0.060958896,
-0.0085241515,
-0.05655662,
-0.029722368,
-0.002744412,
0.005870869,
-0.024226535,
-0.0059444737,
0.014917265,
0.043489996,
0.056472503,
0.005551914,
0.034180723,
-0.04923819,
0.0065087783,
0.0405458,
-0.010465919,
-0.008348902,
-0.077670716,
0.018436281,
-0.0156... |
Take a look at the Create a custom architecture guide for more information about building custom configurations.
Trainer - a PyTorch optimized training loop
All models are a standard torch.nn.Module so you can use them in any typical training loop. While you can write your own training loop, 🤗 Transformers provides ... |
[
0.01768105,
0.026697122,
-0.050894458,
-0.02661286,
0.013727741,
-0.026346028,
-0.027511658,
-0.0057473946,
-0.03699117,
0.04460287,
0.024590563,
0.0059053865,
0.04097959,
-0.06931983,
-0.017807443,
0.0014517701,
-0.0026402203,
-0.042131174,
-0.083700605,
0.009296946,
-0.0118... |
You can customize the training loop behavior by subclassing the methods inside [Trainer]. This allows you to customize features such as the loss function, optimizer, and scheduler. Take a look at the [Trainer] reference for which methods can be subclassed.
The other way to customize the training loop is by using Cal... |
[
0.0051692375,
-0.002034255,
-0.0014882457,
0.0034676387,
0.01695677,
-0.0026647348,
-0.015047914,
-0.008248487,
-0.025065925,
0.04820906,
-0.0069283107,
-0.021373613,
0.038204983,
-0.033328343,
-0.022516139,
0.02505199,
-0.0236308,
-0.07468224,
-0.048487723,
-0.025177391,
-0.... | You'll start with a [TFPreTrainedModel] or a tf.keras.Model:
from transformers import TFAutoModelForSequenceClassification
model = TFAutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased")
Load a preprocessing class like a tokenizer, image processor, feature extractor, or processo... |
[
0.033671256,
0.021065202,
-0.026520938,
-0.002417883,
-0.007446528,
-0.033340607,
0.010208839,
-0.010139953,
-0.044692945,
0.043645885,
0.014341972,
0.0029259138,
0.04761369,
-0.03981585,
-0.013653116,
0.023558859,
-0.019907925,
-0.030915836,
-0.0926924,
-0.017455598,
-0.0123... | dataset = dataset.map(tokenize_dataset) # doctest: +SKIP
tf_dataset = model.prepare_tf_dataset(
dataset["train"], batch_size=16, shuffle=True, tokenizer=tokenizer
) # doctest: +SKIP
When you're ready, you can call compile and fit to start training. Note that Transformers models all have a default tas... |
[
0.009134762,
-0.008586254,
-0.010745124,
0.022502879,
-0.015667628,
-0.0115116285,
0.014837835,
-0.0017369409,
-0.036201503,
0.05037832,
0.0049717296,
0.007587689,
0.035779577,
-0.020379169,
-0.024682844,
0.018789902,
-0.020336976,
-0.055610236,
-0.056257196,
-0.015808271,
-0... | from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased")
Create a function to tokenize the dataset:
def tokenize_dataset(dataset):
return tokenizer(dataset["text"]) # doctest: +SKIP
Apply the tokenizer over the entire dataset with [~datas... |
[
0.019627674,
-0.004363604,
0.003018985,
0.0071280166,
0.012055438,
-0.019627674,
-0.00082949473,
-0.016319945,
0.0077089192,
0.07583171,
-0.013750306,
0.013880155,
0.019395312,
-0.020352094,
0.014051009,
0.028211365,
-0.015677536,
-0.05642273,
-0.077253215,
0.00019829709,
-0.... | When you're ready, call [~Trainer.train] to start training:
trainer.train() # doctest: +SKIP
For tasks - like translation or summarization - that use a sequence-to-sequence model, use the [Seq2SeqTrainer] and [Seq2SeqTrainingArguments] classes instead. |
[
0.056493573,
0.012435112,
-0.043866932,
-0.05564234,
0.009519636,
-0.03197803,
-0.0015357683,
0.037965044,
0.02443042,
0.03751105,
0.028260974,
0.0041072047,
0.043810185,
-0.044037182,
0.018826462,
0.041568603,
0.026033578,
-0.010697176,
-0.072354905,
-0.022727951,
-0.0282893... | What's next?
Now that you've completed the 🤗 Transformers quick tour, check out our guides and learn how to do more specific things like writing a custom model, fine-tuning a model for a task, and how to train a model with a script. If you're interested in learning more about 🤗 Transformers core concepts, grab a cup ... |
[
0.064445704,
0.027309341,
-0.038276512,
-0.030675504,
-0.020169822,
-0.030105427,
0.0034034878,
-0.0014548808,
-0.037950754,
0.02855808,
0.029101009,
-0.0123516405,
0.043732952,
-0.059830803,
-0.0052630208,
0.03800505,
-0.011679766,
-0.015432765,
-0.08605429,
0.006501578,
-0.... | When you're ready, you can call compile and fit to start training. Note that Transformers models all have a default task-relevant loss function, so you don't need to specify one unless you want to:
from tensorflow.keras.optimizers import Adam
model.compile(optimizer=Adam(3e-5)) # No loss argument!
model.fit(tf_datase... |
[
0.05051779,
0.007601644,
-0.0449047,
-0.029870652,
0.016162308,
-0.028121851,
0.0044107866,
-0.0010092627,
-0.0019638755,
0.038501833,
0.048007414,
0.009004916,
0.03587863,
-0.055200063,
0.0020625982,
0.0125307245,
-0.014695571,
-0.009456219,
-0.062110648,
0.0070410403,
-0.00... |
Trainer
The [Trainer] is a complete training and evaluation loop for PyTorch models implemented in the Transformers library. You only need to pass it the necessary pieces for training (model, tokenizer, dataset, evaluation function, training hyperparameters, etc.), and the [Trainer] class takes care of the rest. This ... |
[
0.04448272,
0.044621207,
-0.00392963,
-0.011958539,
0.001300067,
0.006398199,
-0.028390277,
-0.02388938,
-0.011612316,
0.036367252,
-0.001784779,
0.018363662,
0.0019388482,
-0.04805574,
0.029802866,
0.014035876,
-0.0024564515,
-0.03994027,
-0.052210413,
-0.024277149,
-0.02424... | In addition to the [Trainer] class, Transformers also provides a [Seq2SeqTrainer] class for sequence-to-sequence tasks like translation or summarization. There is also the [~trl.SFTTrainer] class from the TRL library which wraps the [Trainer] class and is optimized for training language models like Llama-2 and Mistral ... |
[
0.03692462,
0.02436363,
-0.00034104084,
0.01288501,
-0.033477586,
-0.018558826,
-0.016725006,
-0.02783824,
0.0043294737,
0.056365885,
-0.0020509847,
0.009258731,
0.015029064,
-0.036979772,
0.02000658,
-0.0037882894,
-0.018214123,
-0.03948921,
-0.06298419,
-0.021178572,
-0.002... | Feel free to check out the API reference for these other [Trainer]-type classes to learn more about when to use which one. In general, [Trainer] is the most versatile option and is appropriate for a broad spectrum of tasks. [Seq2SeqTrainer] is designed for sequence-to-sequence tasks and [~trl.SFTTrainer] is designed fo... |
[
0.030153887,
0.0035863998,
-0.07279689,
0.00072035653,
-0.015105072,
-0.020885505,
-0.018072642,
-0.01881805,
0.022910766,
0.055385265,
-0.011089711,
0.016905304,
0.05015334,
-0.03150406,
0.036314055,
0.02419062,
-0.026764387,
-0.039914522,
-0.05018147,
0.017453812,
-0.002751... | Before you start, make sure Accelerate - a library for enabling and running PyTorch training across distributed environments - is installed.
```bash
pip install accelerate
upgrade
pip install accelerate --upgrade
This guide provides an overview of the [Trainer] class.
Basic usage
[Trainer] includes all the code you'll... |
[
0.025514327,
-0.0063169706,
-0.03018229,
-0.0025025061,
0.0027362667,
-0.01627988,
0.009676599,
-0.011097283,
0.015439068,
0.032675736,
0.017686067,
0.015163629,
0.02361525,
-0.051202618,
-0.010560902,
0.024861973,
-0.002073039,
-0.013228309,
-0.08930015,
0.0058277044,
0.0078... |
The [Trainer] class abstracts all of this code away so you don't have to worry about manually writing a training loop every time or if you're just getting started with PyTorch and training. You only need to provide the essential components required for training, such as a model and a dataset, and the [Trainer] class ... |
[
0.046964582,
0.04606033,
-0.000981078,
0.0059094275,
-0.045749493,
-0.023383388,
0.010978183,
-0.0050510946,
-0.005496156,
0.035689693,
0.0326661,
0.01102057,
0.025545117,
-0.05137281,
-0.010222285,
0.049818628,
0.0016742789,
-0.006283844,
-0.054622464,
0.0016062834,
0.005323... | from transformers import TrainingArguments
training_args = TrainingArguments(
output_dir="your-model",
learning_rate=2e-5,
per_device_train_batch_size=16,
per_device_eval_batch_size=16,
num_train_epochs=2,
weight_decay=0.01,
evaluation_strategy="epoch",
save_strategy="epoch",
load_be... |
[
0.017904155,
0.017398313,
-0.025119064,
-0.013631119,
-0.042810235,
-0.023881081,
0.013378198,
0.00953779,
-0.0044627273,
0.033225853,
0.0059037125,
0.018689543,
0.016280135,
-0.039801802,
0.015188581,
0.027874574,
-0.03253365,
-0.039562196,
-0.086525664,
-0.00937805,
-0.0096... | from transformers import Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=dataset["train"],
eval_dataset=dataset["test"],
tokenizer=tokenizer,
data_collator=data_collator,
compute_metrics=compute_metrics,
)
trainer.train() |
[
0.033726703,
0.011437243,
0.0023912906,
0.02979241,
-0.043467373,
-0.032790665,
0.0090020755,
-0.0075029484,
-0.0076053278,
0.06850646,
-0.011773633,
0.022947615,
0.030977085,
-0.015444666,
0.0041756174,
0.021704435,
0.03416547,
-0.0011069775,
-0.060667116,
-0.0060842624,
-0.... | Checkpoints
The [Trainer] class saves your model checkpoints to the directory specified in the output_dir parameter of [TrainingArguments]. You'll find the checkpoints saved in a checkpoint-000 subfolder where the numbers at the end correspond to the training step. Saving checkpoints are useful for resuming training la... |
[
0.043890666,
0.021464678,
-0.011206127,
0.00143424,
-0.033673313,
-0.0343325,
0.01041648,
0.029004093,
0.00018582464,
0.02638109,
0.0027311502,
0.03452476,
0.016204938,
-0.028125182,
-0.00479625,
0.054437608,
-0.012737356,
-0.038067874,
-0.069708705,
-0.009070646,
-0.00109692... | Pass training_args to the [Trainer] along with a model, dataset, something to preprocess the dataset with (depending on your data type it could be a tokenizer, feature extractor or image processor), a data collator, and a function to compute the metrics you want to track during training.
Finally, call [~Trainer.train] ... |
[
0.036901545,
0.047238596,
-0.036930416,
0.00366525,
-0.0076300725,
-0.04602587,
0.0022341604,
-0.0072041745,
0.009088232,
0.059308115,
0.017642288,
0.012776943,
0.023691483,
-0.030549165,
0.0032880777,
-0.0038655666,
-0.018133152,
-0.06265755,
-0.10129156,
0.018725079,
-0.005... | This guide provides an overview of the [Trainer] class.
Basic usage
[Trainer] includes all the code you'll find in a basic training loop:
perform a training step to calculate the loss
calculate the gradients with the [~accelerate.Accelerator.backward] method
update the weights based on the gradients
repeat this proces... |
[
0.022226408,
-0.0038946467,
-0.027567638,
0.054503515,
-0.031989947,
-0.04758289,
-0.025413916,
-0.013676133,
0.014365324,
0.020919817,
-0.006902678,
0.016741596,
0.015750885,
-0.022226408,
0.0022973032,
-0.00843541,
0.010323506,
0.0046699867,
-0.10067931,
0.009691748,
-0.014... | resume from latest checkpoint
trainer.train(resume_from_checkpoint=True)
resume from specific checkpoint saved in output directory
trainer.train(resume_from_checkpoint="your-model/checkpoint-1000")
You can save your checkpoints (the optimizer state is not saved by default) to the Hub by setting push_to_hub=True in [Tr... |
[
0.045577683,
0.016611753,
-0.017015861,
0.06615836,
-0.030365879,
-0.042027302,
-0.01185626,
-0.014952021,
-0.0023073882,
0.06032765,
-0.024217654,
0.015644778,
0.010896501,
-0.022962032,
-0.02330841,
-0.0025455237,
0.013559289,
0.026396954,
-0.059634894,
0.0054446426,
0.0306... | hub_strategy="checkpoint" pushes the latest checkpoint to a subfolder named "last-checkpoint" from which you can resume training
hub_strategy="all_checkpoints" pushes all checkpoints to the directory defined in output_dir (you'll see one checkpoint per folder in your model repository) |
[
0.030767348,
0.0012517904,
-0.010776039,
-0.0010398915,
-0.028317908,
-0.039549485,
-0.0333064,
-0.0071765585,
-0.03249988,
0.07593262,
0.019416288,
0.00749394,
0.022761863,
-0.03181284,
-0.014435262,
-0.012262132,
-0.02624186,
0.0007495807,
-0.06959992,
0.018938348,
-0.00659... |
When you resume training from a checkpoint, the [Trainer] tries to keep the Python, NumPy, and PyTorch RNG states the same as they were when the checkpoint was saved. But because PyTorch has various non-deterministic default settings, the RNG states aren't guaranteed to be the same. If you want to enable full determi... |
[
0.0122832665,
0.033542205,
-0.032114256,
-0.0139079215,
-0.011256018,
-0.029098082,
-0.014002632,
-0.00580286,
-0.0054932283,
0.06253829,
0.03619411,
0.027553568,
0.015853137,
-0.048171386,
0.0090121,
-0.005566083,
-0.0004831163,
-0.057059634,
-0.053475194,
0.031414855,
-0.02... |
Callbacks
Another option for customizing the [Trainer] is to use callbacks. Callbacks don't change anything in the training loop. They inspect the training loop state and then execute some action (early stopping, logging results, etc.) depending on the state. In other words, a callback can't be used to implement some... |
[
0.019736707,
-0.008320093,
-0.032556884,
-0.01967883,
-0.016206095,
0.0067645973,
0.0024580448,
-0.000110105575,
-0.003939383,
0.064303465,
0.022847699,
0.0078208875,
0.037765987,
-0.05368269,
0.01692958,
0.035016738,
-0.00767619,
-0.031978097,
-0.06505589,
-0.015931169,
-0.0... |
from torch import nn
from transformers import Trainer
class CustomTrainer(Trainer):
def compute_loss(self, model, inputs, return_outputs=False):
labels = inputs.pop("labels")
# forward pass
outputs = model(**inputs)
logits = outputs.get("logits")
# compute custom loss for 3... |
[
0.0019686762,
0.0036140035,
-0.014748992,
-0.007013632,
-0.032613568,
-0.0130268475,
-0.0021848374,
-0.00549157,
-0.011890661,
0.01360566,
0.046019144,
0.00013052305,
0.012512348,
-0.055994723,
0.010683016,
0.007967601,
-0.028254611,
-0.051935893,
-0.057481054,
0.008360621,
0... | For example, if you want to customize the [~Trainer.compute_loss] method to use a weighted loss instead. |
[
0.000366466,
0.025875608,
-0.02736039,
-0.005092233,
-0.036874544,
-0.01685158,
0.01366578,
0.008180728,
0.015669517,
0.035750143,
0.015467702,
0.028513622,
0.003989456,
-0.040737864,
0.0076906052,
0.021507747,
-0.005283237,
-0.048406847,
-0.07219222,
0.022170855,
-0.01894180... | Then pass it to the [Trainer]'s callback parameter.
from transformers import Trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=dataset["train"],
eval_dataset=dataset["test"],
tokenizer=tokenizer,
data_collator=data_collator,
compute_metrics=compute_metrics,
callb... |
[
0.008947883,
0.010365581,
-0.022084743,
-0.024663672,
-0.044596933,
-0.01890739,
-0.014497565,
0.026857898,
-0.01445482,
0.016371205,
0.020075744,
0.021657296,
0.02531909,
-0.033426326,
0.013471693,
0.014233973,
-0.004509562,
-0.032485943,
-0.09135959,
0.017026624,
0.01025159... |
[~Trainer.get_train_dataloader] creates a training DataLoader
[~Trainer.get_eval_dataloader] creates an evaluation DataLoader
[~Trainer.get_test_dataloader] creates a test DataLoader
[~Trainer.log] logs information on the various objects that watch training
[~Trainer.create_optimizer_and_scheduler] creates an optimiz... |
[
0.003655332,
-0.0011656295,
-0.0023809331,
0.009948532,
-0.009825203,
-0.03480616,
0.0015981371,
-0.0049537113,
0.0043918793,
0.06237702,
0.012826206,
0.0069817863,
0.023569524,
-0.006598097,
0.007927308,
0.00009297844,
-0.030311504,
-0.027968256,
-0.043576214,
0.015936835,
-... | The [Trainer] is set to logging.INFO by default which reports errors, warnings, and other basic information. A [Trainer] replica - in distributed environments - is set to logging.WARNING which only reports errors and warnings. You can change the logging level with the log_level and log_level_replica parameters in [Trai... |
[
0.022737468,
0.051879857,
-0.014661699,
-0.006377073,
-0.01124342,
-0.019646397,
0.0011957012,
0.003373026,
0.016137615,
0.05071026,
0.015135106,
0.021679264,
0.01102064,
-0.046087578,
0.0018257505,
0.03792827,
-0.0029396496,
-0.028905684,
-0.050988737,
0.018476803,
-0.023545... | from transformers import TrainerCallback
class EarlyStoppingCallback(TrainerCallback):
def init(self, num_steps=10):
self.num_steps = num_steps
def on_step_end(self, args, state, control, **kwargs):
if state.global_step >= self.num_steps:
return {"should_training_stop": True}
else:
r... |
[
0.034670696,
0.026338004,
0.003109057,
-0.028864332,
-0.0039744284,
-0.028236238,
0.008186136,
0.008144263,
-0.014487996,
0.062195096,
0.008018645,
0.025053903,
0.044161867,
-0.031739596,
0.025584294,
0.024691006,
-0.030232176,
-0.013231811,
-0.050470706,
0.023462737,
0.00404... | [Trainer] sets the log level separately for each node in the [Trainer.__init__] method, so you may want to consider setting this sooner if you're using other Transformers functionalities before creating the [Trainer] object.
For example, to set your main code and modules to use the same log level according to each nod... |
[
0.04283391,
0.033739425,
-0.002966585,
0.0020911705,
-0.006459472,
-0.015466241,
0.011725994,
-0.022553766,
-0.030483373,
0.057289653,
-0.02310112,
0.020616977,
0.030735997,
-0.050524928,
0.0076489127,
0.021613441,
-0.045837335,
-0.027437842,
-0.06338071,
0.01677147,
0.011311... | For example, to set your main code and modules to use the same log level according to each node:
logger = logging.getLogger(name)
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
handlers=[logging.StreamHandler(sys.stdout)],
)
log_level = trai... |
[
0.035091415,
-0.0064320695,
-0.017505,
0.00006397933,
0.033897277,
-0.04999102,
-0.0014383952,
0.029853486,
0.031834673,
0.07799902,
-0.01564594,
-0.026257498,
0.03378872,
0.0026562817,
-0.014085418,
-0.014343243,
-0.028686486,
-0.022607232,
-0.03544423,
-0.012497755,
0.03897... | Use different combinations of log_level and log_level_replica to configure what gets logged on each of the nodes.
my_app.py --log_level warning --log_level_replica error
Add the log_on_each_node 0 parameter for multi-node environments.
```bash
my_app.py --log_level warning --log_level_replica error --log_on_each_no... |
[
0.0056810463,
0.03621713,
-0.044976488,
0.010034918,
-0.048043735,
-0.008221112,
-0.026720457,
-0.010654266,
-0.031822708,
0.0222523,
0.019081827,
0.020792408,
0.042145178,
-0.022620961,
0.013323362,
0.04884004,
-0.038045682,
-0.044121195,
-0.030554518,
-0.006396246,
-0.01645... | from transformers import TrainingArguments, Trainer
training_args = TrainingArguments(, neftune_noise_alpha=0.1)
trainer = Trainer(, args=training_args)
NEFTune is disabled after training to restore the original embedding layer to avoid any unexpected behavior.
Accelerate and Trainer
The [Trainer] class is powered by ... |
[
-0.0056334576,
0.03816464,
-0.017512167,
0.012066138,
-0.06540894,
-0.033440035,
-0.008883393,
-0.012681468,
-0.055563647,
0.022703577,
0.062127173,
0.00057996676,
0.032053772,
0.0034409005,
-0.0008823275,
0.028375935,
-0.04025818,
-0.063485146,
-0.023198672,
-0.02984707,
-0.... | NEFTune
NEFTune is a technique that can improve performance by adding noise to the embedding vectors during training. To enable it in [Trainer], set the neftune_noise_alpha parameter in [TrainingArguments] to control how much noise is added.
from transformers import TrainingArguments, Trainer
training_args = TrainingA... |
[
0.030015988,
-0.0076429597,
-0.008403599,
0.04227398,
-0.006929861,
-0.00773804,
0.0076722153,
-0.0054963487,
-0.011285251,
0.059271336,
0.011343761,
-0.017889645,
0.0896969,
-0.002316658,
-0.007229728,
0.028143646,
-0.019396296,
-0.007006656,
-0.076883055,
0.0022636326,
0.01... |
yml
compute_environment: LOCAL_MACHINE
distributed_type: FSDP
downcast_bf16: 'no'
fsdp_config:
fsdp_auto_wrap_policy: TRANSFORMER_BASED_WRAP
fsdp_backward_prefetch_policy: BACKWARD_PRE
fsdp_forward_prefetch: true
fsdp_offload_params: false
fsdp_sharding_strategy: 1
fsdp_state_dict_type: FULL_STATE_DICT
... |
[
0.021725202,
0.0046150154,
-0.03200121,
0.023788884,
-0.030983502,
-0.016410513,
0.03106831,
-0.0007358935,
0.0031220259,
0.06157123,
0.0006908388,
0.01966152,
0.08548733,
-0.04432676,
0.010141726,
0.022559155,
-0.014530584,
-0.0386163,
-0.10929035,
0.012466902,
0.0162833,
... |
yml
compute_environment: LOCAL_MACHINE
deepspeed_config:
gradient_accumulation_steps: 1
gradient_clipping: 0.7
... |
[
0.023899665,
0.03239991,
-0.031497486,
0.052224115,
-0.020391857,
0.008754962,
-0.016272731,
0.0032694654,
0.01518109,
0.034117427,
-0.018223131,
0.012692148,
0.037261352,
-0.014278667,
0.023099128,
0.028266229,
0.0126630375,
-0.029605309,
-0.050215494,
-0.003082067,
0.002334... | The accelerate_launch command is the recommended way to launch your training script on a distributed system with Accelerate and [Trainer] with the parameters specified in config_file.yaml. This file is saved to the Accelerate cache folder and automatically loaded when you run accelerate_launch.
For example, to run the ... |
[
0.0030345838,
0.040029146,
-0.013794217,
0.035680655,
-0.052066687,
-0.0034665528,
-0.03297365,
-0.0011969147,
-0.010648041,
0.057048734,
-0.019337822,
0.020417744,
0.056731958,
-0.030122653,
0.039251603,
0.03916521,
-0.01768194,
-0.023599917,
-0.08023108,
0.0034575535,
-0.01... | Learn more about FSDP sharding strategies, CPU offloading, and more with the [Trainer] in the Fully Sharded Data Parallel guide.
To use Accelerate with [Trainer], run the accelerate.config command to set up training for your training environment. This command creates a config_file.yaml that'll be used when you launch ... |
[
0.025708094,
0.004988561,
-0.015490048,
0.032227237,
-0.025311276,
-0.0369607,
0.03500496,
0.001201081,
-0.01792764,
0.057141695,
0.0067423517,
0.025523858,
0.06745894,
-0.033190936,
-0.007192314,
0.01259895,
-0.0127831865,
-0.033134248,
-0.071994,
-0.01744579,
0.011862004,
... |
yml
compute_environment: LOCAL_MACHINE
distributed_type: MULTI_GPU
downcast_bf16: 'no'
gpu_ids: all
machine_rank: 0 #change r... |
[
0.043284032,
-0.03011648,
-0.02763542,
0.03011648,
0.022045562,
0.01968407,
-0.019205796,
0.008519302,
0.01690409,
0.013952225,
0.011605681,
-0.028666705,
0.027665311,
-0.047468953,
0.038441483,
0.029159926,
-0.008190487,
-0.027545743,
-0.02070041,
-0.0043306453,
0.0015917643... | accelerate launch \
./examples/pytorch/text-classification/run_glue.py \
--model_name_or_path google-bert/bert-base-cased \
--task_name $TASK_NAME \
--do_train \
--do_eval \
--max_seq_length 128 \
--per_device_train_batch_size 16 \
--learning_rate 5e-5 \
--num_train_epochs 3 \
--... |
[
0.020717314,
-0.0043536937,
-0.0126062175,
0.051586874,
0.010328078,
0.019096622,
-0.057763845,
0.017628828,
-0.009211942,
0.028606711,
-0.0045448127,
-0.044767745,
0.059139904,
0.016864352,
0.008088161,
0.057152264,
-0.031374115,
-0.04060899,
-0.038284983,
0.018760253,
0.014... |
accelerate launch --num_processes=2 \
--use_fsdp \
--mixed_precision=bf16 \
--fsdp_auto_wrap_policy=TRANSFORMER_BASED_WRAP \
--fsdp_transformer_layer_cls_to_wrap="BertLayer" \
--fsdp_sharding_strategy=1 \
--fsdp_state_dict_type=FULL_STATE_DICT \
./examples/pytorch/text-classification/run_... |
[
0.021152297,
0.010081869,
-0.011942689,
0.0101545565,
-0.033145867,
-0.013454606,
0.020498103,
-0.00084045855,
-0.0036380498,
0.06844331,
-0.0057278387,
0.013679939,
0.08362063,
-0.037274566,
0.0058841184,
0.017619645,
-0.0060658394,
-0.021399438,
-0.08513255,
-0.0006809986,
... | yml
compute_environment: LOCAL_MACHINE
deepspeed_config:
deepspeed_config_file: /home/user/configs/ds_zero3_config.json
zero3_init_flag: true
distributed_type: DEEPSPEED
downcast_bf16: 'no'
machine_rank: 0
main_training_function: main
num_machines: 1
num_processes: 4
rdzv_backend: static
same_network: true
tpu_env:... |
[
0.07099658,
0.011224846,
-0.01790176,
-0.016970808,
-0.033331156,
-0.028004885,
-0.025028888,
-0.004513594,
0.006161838,
0.05900103,
0.025715657,
0.015933024,
0.049691502,
-0.022174984,
-0.024754182,
0.03562038,
0.014254257,
-0.037024442,
-0.061961763,
0.0012056599,
-0.012346... |
Train with a script
Along with the 🤗 Transformers notebooks, there are also example scripts demonstrating how to train a model for a task with PyTorch, TensorFlow, or JAX/Flax.
You will also find scripts we've used in our research projects and legacy examples which are mostly community contributed. These scripts are ... |
[
0.053398464,
0.011622019,
0.003526583,
-0.018946461,
-0.011315049,
-0.06253617,
0.02727034,
0.0019060682,
-0.004065565,
0.021973325,
0.0073458585,
-0.00482942,
0.06556304,
-0.0056146914,
-0.02647079,
0.031981967,
0.017832803,
-0.026713511,
-0.038835246,
0.0025200078,
0.020074... | git clone https://github.com/huggingface/transformers
cd transformers
pip install .
For older versions of the example scripts, click on the toggle below:
Examples for older versions of 🤗 Transformers
v4.5.1
v4.4.2
v4.3.3
v4.2.2
v4.1.1
v4.0.1
v3.5.1
v3.4.0
v3.3.1
v3.2.0
v3.1.0
v3.0.2
v2.11.0
v2.10.0
v2.9.1
v2.8.0
v2.... |
[
0.038187563,
0.006361066,
0.021069488,
-0.03454662,
-0.0060858782,
-0.04676777,
0.028605392,
-0.036070738,
-0.0039126026,
0.034885313,
0.021972667,
-0.013695872,
0.050408714,
-0.010781707,
0.0011554349,
0.05622293,
-0.015904428,
-0.003494529,
-0.06373061,
-0.0006010907,
0.011... | Then switch your current clone of 🤗 Transformers to a specific version, like v3.5.1 for example:
git checkout tags/v3.5.1
After you've setup the correct library version, navigate to the example folder of your choice and install the example specific requirements:
pip install -r requirements.txt
Run a script |
[
0.062590264,
-0.0144610545,
-0.015748238,
0.022076298,
0.001966732,
-0.0136269005,
0.005468738,
-0.001706958,
0.0134686995,
0.03327984,
0.0088017555,
-0.0020835856,
-0.002417966,
-0.0143819535,
-0.03158277,
-0.005364469,
-0.0024125727,
-0.025628641,
-0.039118912,
0.049617738,
... | python examples/pytorch/summarization/run_summarization.py \
--model_name_or_path google-t5/t5-small \
--do_train \
--do_eval \
--dataset_name cnn_dailymail \
--dataset_config "3.0.0" \
--source_prefix "summarize: " \
--output_dir /tmp/tst-summarization \
--per_device_train_batch_size=4 ... |
[
0.07058443,
-0.0024149623,
0.020494148,
-0.0062429346,
-0.021668598,
-0.034499463,
-0.017836956,
-0.018336097,
-0.039285347,
0.045568652,
0.016662506,
0.011516948,
0.012779482,
0.006279636,
-0.025691088,
0.009637829,
-0.00049730606,
-0.030359527,
-0.054142136,
0.007432065,
-0... | pip install -r requirements.txt
Run a script
The example script downloads and preprocesses a dataset from the 🤗 Datasets library. Then the script fine-tunes a dataset with the Trainer on an architecture that supports summarization. The following example shows how to fine-tune T5-small on the CNN/DailyMail dataset. Th... |
[
0.05515875,
0.0033159126,
0.022228826,
-0.00018880975,
-0.02986388,
-0.032914896,
-0.004336049,
-0.023205752,
-0.032433946,
0.047463577,
0.012166487,
0.0048320265,
0.0044713155,
-0.014172943,
-0.038896687,
-0.0050574713,
0.0061771786,
-0.049146894,
-0.06757323,
0.008063397,
0... | The example script downloads and preprocesses a dataset from the 🤗 Datasets library. Then the script fine-tunes a dataset using Keras on an architecture that supports summarization. The following example shows how to fine-tune T5-small on the CNN/DailyMail dataset. The T5 model requires an additional source_prefix arg... |
[
0.035963006,
0.00028570008,
-0.03477868,
0.019555787,
-0.06609105,
-0.015078465,
0.0011825186,
0.012319857,
0.0004098194,
0.06262474,
0.018588107,
0.053150143,
0.03359436,
-0.014175779,
0.024986345,
0.06181593,
-0.002801937,
-0.049510516,
-0.06354909,
-0.008333596,
0.01452963... | Distributed training and mixed precision
The Trainer supports distributed training and mixed precision, which means you can also use it in a script. To enable both of these features:
Add the fp16 argument to enable mixed precision.
Set the number of GPUs to use with the nproc_per_node argument. |
[
0.058556184,
0.014054041,
-0.014625117,
0.026826644,
-0.011943846,
-0.02379019,
0.0067171073,
0.00036693373,
-0.005644599,
0.05100684,
-0.012312956,
0.018901223,
-0.0057769213,
-0.022369465,
-0.030921677,
-0.014054041,
-0.009903294,
-0.043652494,
-0.04919611,
0.02866523,
0.00... | python examples/tensorflow/summarization/run_summarization.py \
--model_name_or_path google-t5/t5-small \
--dataset_name cnn_dailymail \
--dataset_config "3.0.0" \
--output_dir /tmp/tst-summarization \
--per_device_train_batch_size 8 \
--per_device_eval_batch_size 16 \
--num_train_epochs 3... |
[
0.027955685,
-0.012650093,
-0.03846095,
0.013620372,
-0.01290543,
0.023622261,
-0.014634422,
-0.022557145,
0.012292623,
0.04669008,
0.0026518507,
0.010614698,
0.04444312,
-0.026088081,
-0.0064344765,
0.031720072,
0.016064305,
-0.036097266,
-0.0404161,
0.0067518232,
-0.0167500... | Tensor Processing Units (TPUs) are specifically designed to accelerate performance. PyTorch supports TPUs with the XLA deep learning compiler (see here for more details). To use a TPU, launch the xla_spawn.py script and use the num_cores argument to set the number of TPU cores you want to use. |
[
0.05871865,
-0.0010972107,
-0.040952496,
0.036014106,
-0.0032972782,
-0.010569357,
-0.01361821,
-0.0053411387,
0.011299577,
0.0556171,
0.014574271,
0.0013249337,
0.025158685,
-0.030368082,
-0.0071591586,
-0.009470264,
-0.00058765704,
-0.017961886,
-0.044927295,
-0.0049572093,
... |
torchrun \
--nproc_per_node 8 pytorch/summarization/run_summarization.py \
--fp16 \
--model_name_or_path google-t5/t5-small \
--do_train \
--do_eval \
--dataset_name cnn_dailymail \
--dataset_config "3.0.0" \
--source_prefix "summarize: " \
--output_dir /tmp/tst-summarization \
... |
[
0.05048873,
0.006471362,
0.007229975,
-0.00054625486,
0.015528419,
-0.0449042,
0.017522894,
-0.012778891,
0.011860008,
0.039974995,
-0.014616659,
0.002384467,
0.011161941,
-0.009872655,
-0.026782962,
-0.025586275,
0.008205843,
-0.031170808,
-0.040972233,
0.048836164,
-0.00150... | python xla_spawn.py --num_cores 8 \
summarization/run_summarization.py \
--model_name_or_path google-t5/t5-small \
--do_train \
--do_eval \
--dataset_name cnn_dailymail \
--dataset_config "3.0.0" \
--source_prefix "summarize: " \
--output_dir /tmp/tst-summarization \
--per_device_tra... |
[
0.05603788,
-0.0035757704,
-0.031374503,
0.032772653,
-0.010730807,
-0.0032279806,
0.0029064058,
-0.004365726,
0.013331368,
0.0334158,
-0.018120034,
0.015938919,
-0.0057918397,
-0.021671336,
-0.03800174,
-0.020007538,
0.0004301498,
-0.04840398,
-0.0433986,
0.028718017,
0.0052... | python run_summarization.py \
--tpu name_of_tpu_resource \
--model_name_or_path google-t5/t5-small \
--dataset_name cnn_dailymail \
--dataset_config "3.0.0" \
--output_dir /tmp/tst-summarization \
--per_device_train_batch_size 8 \
--per_device_eval_batch_size 16 \
--num_train_epochs 3 ... |
[
0.032397788,
0.014697194,
-0.03612423,
0.034400057,
-0.040045336,
-0.009330007,
-0.0015999016,
-0.025028335,
-0.020676186,
0.06101352,
-0.0071539325,
0.052754167,
0.037097555,
-0.021691224,
-0.017756213,
0.014961382,
0.03128542,
-0.035428997,
-0.040017527,
-0.013251113,
-0.00... | Tensor Processing Units (TPUs) are specifically designed to accelerate performance. TensorFlow scripts utilize a TPUStrategy for training on TPUs. To use a TPU, pass the name of the TPU resource to the tpu argument. |
[
0.03583982,
0.012930191,
-0.039745443,
-0.0016369149,
-0.025429616,
-0.021222457,
-0.0066015054,
0.012233784,
-0.0032325478,
0.043507475,
-0.049825393,
0.0026025511,
0.0074738087,
-0.023735266,
0.016900428,
0.0058189454,
-0.017446065,
-0.058814064,
-0.094022095,
0.03179061,
-... | Note: As Accelerate is rapidly developing, the git version of accelerate must be installed to run the scripts
pip install git+https://github.com/huggingface/accelerate
Instead of the run_summarization.py script, you need to use the run_summarization_no_trainer.py script. 🤗 Accelerate supported scripts will have a ta... |
[
-0.0075189434,
0.014448166,
-0.018308159,
0.021806277,
-0.006302644,
-0.02202072,
-0.0024024432,
0.006590803,
0.03881437,
0.020251557,
-0.0031395946,
0.043424916,
0.031040773,
-0.03768854,
0.04339811,
0.057899885,
-0.03420382,
-0.023789885,
-0.058489606,
0.008483942,
-0.02076... | accelerate config
Test your setup to make sure it is configured correctly:
accelerate test
Now you are ready to launch the training: |
[
0.04438753,
0.012086506,
0.001394387,
0.017362073,
0.019254,
-0.0062761055,
-0.012850554,
-0.01210106,
0.008659206,
0.034229334,
-0.029135684,
0.0088775065,
-0.0060578063,
-0.018919274,
-0.007946095,
-0.026356006,
-0.016445216,
-0.03885728,
-0.047909427,
0.029979775,
-0.03160... | accelerate test
Now you are ready to launch the training:
accelerate launch run_summarization_no_trainer.py \
--model_name_or_path google-t5/t5-small \
--dataset_name cnn_dailymail \
--dataset_config "3.0.0" \
--source_prefix "summarize: " \
--output_dir ~/tmp/tst-summarization
Use a custom dataset... |
[
0.038314175,
0.012811927,
-0.07565547,
-0.008033386,
-0.0060232515,
0.00582653,
-0.0020172885,
-0.008977649,
0.012761853,
0.039601803,
-0.013813418,
-0.0060017905,
0.03722684,
-0.05253534,
-0.0013323403,
0.021932647,
-0.012726085,
-0.06043281,
-0.07582715,
0.0097216135,
0.008... | Run a script with 🤗 Accelerate
🤗 Accelerate is a PyTorch-only library that offers a unified method for training a model on several types of setups (CPU-only, multiple GPUs, TPUs) while maintaining complete visibility into the PyTorch training loop. Make sure you have 🤗 Accelerate installed if you don't already have ... |
[
0.07161384,
0.0067781587,
0.010753845,
0.02536201,
-0.017697504,
-0.0033431072,
0.0026516833,
-0.0013230834,
0.012180826,
0.04969424,
0.011018646,
-0.019742353,
-0.009812332,
-0.0029293562,
-0.02870144,
-0.00010067938,
-0.0029955565,
-0.04210329,
-0.039455283,
0.04728161,
-0.... |
python examples/pytorch/summarization/run_summarization.py \
--model_name_or_path google-t5/t5-small \
--do_train \
--do_eval \
--train_file path_to_csv_or_jsonlines_file \
--validation_file path_to_csv_or_jsonlines_file \
--text_column text_column_name \
--summary_column summary_column_na... |
[
0.07739413,
0.012846508,
-0.00021415322,
0.034858864,
-0.008585821,
-0.015009075,
0.0018331695,
0.017801795,
0.0018331695,
0.056226745,
0.032739263,
-0.0022610284,
0.004479092,
0.00088167575,
-0.028858097,
0.014600908,
-0.009044114,
-0.038639776,
-0.02371663,
0.037236255,
0.0... |
python examples/pytorch/summarization/run_summarization.py \
--model_name_or_path google-t5/t5-small \
--max_train_samples 50 \
--max_eval_samples 50 \
--max_predict_samples 50 \
--do_train \
--do_eval \
--dataset_name cnn_dailymail \
--dataset_config "3.0.0" \
--source_prefix "sum... |
[
0.046986703,
-0.021383002,
-0.021019632,
0.042766005,
-0.018713621,
-0.045868635,
-0.03650484,
-0.039216146,
-0.0060620112,
0.07015861,
0.009300907,
0.0019688434,
0.018476032,
-0.013801121,
-0.016589297,
0.018378202,
-0.014241359,
-0.0126341395,
-0.07239474,
0.011795591,
-0.0... |
examples/pytorch/summarization/run_summarization.py -h
Resume training from checkpoint
Another helpful option to enable is resuming training from a previous checkpoint. This will ensure you can pick up where you left off without starting over if your training gets interrupted. There are two methods to resume training... |
[
0.056888547,
0.018784486,
0.03925873,
0.031654816,
-0.022600524,
-0.020234862,
0.004861577,
-0.004914382,
-0.011455157,
0.060380716,
-0.009293674,
0.00057029363,
-0.02595188,
0.015081097,
-0.018038176,
-0.026965735,
0.022586443,
-0.012736557,
-0.074236736,
0.023811517,
-0.023... | train_file and validation_file specify the path to your training and validation files.
text_column is the input text to summarize.
summary_column is the target text to output.
A summarization script using a custom dataset would look like this: |
[
0.018017206,
0.047434036,
-0.008090836,
0.0098358,
-0.02209885,
-0.0524576,
0.010735453,
0.019997649,
-0.011339246,
0.029006252,
0.041492708,
0.01946631,
-0.0052711195,
-0.027122416,
0.0040756078,
0.015795244,
-0.02511782,
-0.039995298,
-0.026687684,
0.006170772,
0.0029133048... | max_train_samples
max_eval_samples
max_predict_samples |
[
0.036294628,
0.033617157,
0.009430653,
-0.012613871,
-0.044505544,
-0.01694245,
0.014071606,
0.008887721,
0.0037466022,
0.01706145,
-0.003542073,
-0.008181166,
0.007251488,
-0.008329915,
-0.02255027,
0.017284572,
0.038496107,
-0.015722714,
-0.061165374,
-0.004942168,
0.025316... | huggingface-cli login
Then add the push_to_hub argument to the script. This argument will create a repository with your Hugging Face username and the folder name specified in output_dir.
To give your repository a specific name, use the push_to_hub_model_id argument to add it. The repository will be automatically listed... |
[
0.06302115,
-0.015284982,
-0.015064525,
0.022339588,
0.004603866,
-0.007230972,
-0.0036246718,
-0.007098698,
0.010648048,
0.03944701,
0.005379138,
0.009001972,
-0.0086786365,
-0.020840485,
-0.03600789,
-0.011926695,
-0.0062462664,
-0.017754095,
-0.04006429,
0.04238643,
0.0103... | python examples/pytorch/summarization/run_summarization.py
--model_name_or_path google-t5/t5-small \
--do_train \
--do_eval \
--dataset_name cnn_dailymail \
--dataset_config "3.0.0" \
--source_prefix "summarize: " \
--push_to_hub \
--push_to_hub_model_id finetuned-t5-cnn_dailymail \
... |
[
0.08373476,
-0.006571233,
-0.018850964,
0.045759194,
-0.011561417,
-0.016585806,
-0.023715727,
-0.0273643,
-0.009972768,
0.057799485,
0.004739345,
-0.0028751518,
-0.002219549,
-0.024399836,
-0.023761334,
-0.0034965496,
-0.0096611185,
-0.010330023,
-0.06676889,
0.03332364,
0.0... |
python examples/pytorch/summarization/run_summarization.py
--model_name_or_path google-t5/t5-small \
--do_train \
--do_eval \
--dataset_name cnn_dailymail \
--dataset_config "3.0.0" \
--source_prefix "summarize: " \
--output_dir /tmp/tst-summarization \
--per_device_train_batch_size=4 ... |
[
0.040380664,
0.0042507784,
-0.025082018,
-0.041184396,
-0.028199948,
-0.0138020385,
0.04827942,
0.018984731,
0.013330885,
0.056538474,
0.04794684,
-0.0071227388,
0.02534531,
-0.086138025,
-0.0040533096,
0.056150462,
0.006059178,
0.011633345,
-0.03356279,
0.02195023,
0.0293501... | Models in the transformers library itself generally follow the convention that they accept a config object
in their __init__ method, and then pass the whole config to sub-layers in the model, rather than breaking the
config object into multiple arguments that are all passed individually to sub-layers. Writing your mod... |
[
0.079009175,
-0.013967802,
-0.030805076,
0.012722845,
0.0034786658,
-0.012578613,
0.001587509,
-0.007143316,
0.020830244,
0.018234054,
-0.0025430508,
0.011348839,
-0.0073293005,
-0.030805076,
-0.035162423,
-0.0021767756,
0.004979825,
-0.03965641,
-0.06255146,
0.035921544,
0.0... |
python examples/pytorch/summarization/run_summarization.py
--model_name_or_path google-t5/t5-small \
--do_train \
--do_eval \
--dataset_name cnn_dailymail \
--dataset_config "3.0.0" \
--source_prefix "summarize: " \
--output_dir /tmp/tst-summarization \
--per_device_train_batch_size=4 ... |
[
0.03312802,
0.019524004,
-0.003316913,
-0.033486806,
-0.040483158,
0.001897648,
-0.010576748,
-0.014747649,
-0.037822153,
0.08598933,
0.035669427,
0.013858156,
0.06709321,
-0.041738912,
-0.0027264091,
0.017924411,
-0.032021757,
-0.020675113,
-0.07103987,
-0.017490877,
-0.0021... |
In our example, we will take a couple of arguments of the ResNet class that we might want to tweak. Different
configurations will then give us the different types of ResNets that are possible. We then just store those arguments,
after checking the validity of a few of them.
thon
from transformers import PretrainedCon... |
[
0.01929337,
0.0037160742,
-0.018954372,
-0.08348178,
0.020973617,
0.008467564,
-0.018409029,
-0.0100299,
0.0036386943,
0.061609082,
0.051616028,
-0.0032904851,
0.038999435,
-0.06028257,
0.01868907,
0.05176342,
0.013729393,
-0.019308109,
-0.07522793,
0.0045543555,
-0.007325290... |
Building custom models
The 🤗 Transformers library is designed to be easily extensible. Every model is fully coded in a given subfolder
of the repository with no abstraction, so you can easily copy a modeling file and tweak it to your needs.
If you are writing a brand new model, it might be easier to start from scratc... |
[
0.027305827,
0.016913846,
-0.014140261,
-0.031734962,
-0.01750153,
-0.0026230793,
-0.014383935,
-0.021299979,
-0.031591624,
0.096838936,
0.016612837,
0.013860753,
0.05120022,
-0.037497137,
0.018232552,
0.016756175,
-0.012090533,
-0.041481923,
-0.07115282,
-0.0029312554,
0.013... |
from transformers import PreTrainedModel
from timm.models.resnet import BasicBlock, Bottleneck, ResNet
from .configuration_resnet import ResnetConfig
BLOCK_MAPPING = {"basic": BasicBlock, "bottleneck": Bottleneck}
class ResnetModel(PreTrainedModel):
config_class = ResnetConfig
def __init__(self, config):
supe... |
[
0.013664399,
0.015793297,
-0.040564697,
-0.02075846,
0.018677173,
0.03732714,
-0.014854678,
0.007542966,
-0.0092909755,
0.055718645,
0.0111070005,
0.025410749,
0.009665063,
-0.04880823,
-0.011705541,
0.019153284,
-0.0025727022,
-0.044319175,
-0.03912276,
-0.024186462,
0.00461... | def forward(self, tensor):
return self.model.forward_features(tensor)
For the model that will classify images, we just change the forward method: |
[
0.023565203,
0.020688633,
0.02822723,
-0.04531661,
-0.04387124,
0.014722939,
0.023267627,
0.002621505,
-0.0225166,
0.050474595,
0.02243158,
-0.005041083,
0.0127886925,
-0.029077448,
-0.036616046,
0.011378748,
-0.06110232,
-0.033696964,
-0.04585508,
0.0154314535,
0.008750158,
... | self.block_type = block_type
self.layers = layers
self.num_classes = num_classes
self.input_channels = input_channels
self.cardinality = cardinality
self.base_width = base_width
self.stem_width = stem_width
self.stem_type = stem_type
self.avg_down = avg_down
super().__init__(**kwargs... |
[
0.029994527,
0.00020198683,
-0.049242117,
-0.04380992,
-0.0056414083,
0.0065810313,
-0.018219877,
0.0012938168,
-0.0089411,
0.06694814,
0.04049188,
-0.014189775,
0.045659803,
-0.054967944,
0.010372557,
0.028585093,
-0.0027014161,
0.0043677785,
-0.08292173,
0.0045329467,
0.017... |
The three important things to remember when writing you own configuration are the following:
- you have to inherit from PretrainedConfig,
- the __init__ of your PretrainedConfig must accept any kwargs,
- those kwargs need to be passed to the superclass __init__.
The inheritance is to make sure you get all the functio... |
[
0.0032134706,
-0.011930675,
-0.032298043,
-0.011646612,
0.0057096803,
0.015481472,
0.0036928281,
0.0025903059,
-0.016149022,
0.049483895,
0.020750852,
0.010418036,
0.0067997747,
-0.04107561,
0.0060683107,
0.010950656,
0.0005854375,
-0.006586727,
-0.038377006,
-0.04692732,
0.0... | def forward(self, tensor, labels=None):
logits = self.model(tensor)
if labels is not None:
loss = torch.nn.cross_entropy(logits, labels)
return {"loss": loss, "logits": logits}
return {"logits": logits} |
[
0.020041263,
-0.009874345,
-0.028306456,
-0.02750188,
-0.028803831,
-0.0068681557,
-0.0068498696,
0.01702776,
-0.010379034,
0.058309834,
0.028789202,
-0.02083121,
0.0010541778,
-0.05544262,
0.006206209,
0.0020992127,
-0.008711367,
-0.028247941,
-0.05617405,
-0.0001063435,
0.0... |
You can have your model return anything you want, but returning a dictionary like we did for
ResnetModelForImageClassification, with the loss included when labels are passed, will make your model directly
usable inside the [Trainer] class. Using another output format is fine as long as you are planning on using your ... |
[
0.01702199,
-0.029671893,
-0.021889579,
-0.033373594,
-0.0041753426,
0.028666314,
0.005410457,
-0.022370508,
-0.016220441,
0.043633424,
0.05844022,
0.0019984078,
0.031420726,
-0.06639742,
0.013174553,
0.040048312,
-0.019018576,
-0.001905501,
-0.065289825,
-0.0010948433,
0.022... | In both cases, notice how we inherit from PreTrainedModel and call the superclass initialization with the config
(a bit like when you write a regular torch.nn.Module). The line that sets the config_class is not mandatory, unless
you want to register your model with the auto classes (see last section).
If your model is... |
[
0.028973095,
-0.0013458906,
-0.011144046,
-0.028048575,
0.012580611,
0.028247701,
-0.020695068,
-0.020168802,
-0.03635505,
0.088355854,
0.034591343,
-0.0049888627,
0.06383468,
-0.044633076,
-0.0036518618,
0.017622812,
-0.037066218,
-0.035700772,
-0.056950554,
-0.012957531,
0.... |
import torch
class ResnetModelForImageClassification(PreTrainedModel):
config_class = ResnetConfig
def __init__(self, config):
super().__init__(config)
block_layer = BLOCK_MAPPING[config.block_type]
self.model = ResNet(
block_layer,
config.layers,
num_classes=config.num_classes... |
[
0.008932338,
0.0007727456,
-0.028705882,
-0.030046461,
-0.016800955,
0.019788116,
0.028807882,
-0.010280203,
0.023736997,
0.030104747,
0.025456434,
0.018068677,
0.029682174,
-0.040654525,
0.022265274,
0.026651299,
0.043947686,
0.01131478,
-0.08690451,
-0.0036046281,
-0.014564... |
Now let's see how to make sure that when we do [~PreTrainedModel.save_pretrained] or [~PreTrainedModel.push_to_hub], the
code of the model is saved.
Registering a model with custom code to the auto classes
If you are writing a library that extends 🤗 Transformers, you may want to extend the auto classes to include yo... |
[
0.011538169,
0.00021301166,
-0.012423506,
0.009421999,
-0.033110157,
0.0070467046,
0.040135264,
0.017735526,
0.010580854,
0.04324474,
0.039588228,
0.016828597,
0.013697527,
-0.039645813,
-0.009191667,
0.055452313,
0.044540357,
0.029482434,
-0.044050902,
0.0028233605,
0.000603... | Note that the first argument used when registering your custom config to [AutoConfig] needs to match the model_type
of your custom config, and the first argument used when registering your custom models to any auto model class needs
to match the config_class of those models.
Sending the code to the Hub
This API is exp... |
[
0.026853211,
0.0046552345,
0.005831758,
-0.029782651,
0.0014469202,
0.0101716705,
-0.0025310507,
-0.026527718,
-0.0085848905,
0.07725045,
0.034936298,
0.0044484106,
0.059077065,
-0.06303723,
0.016152613,
0.055930626,
0.010754847,
-0.016057678,
-0.052919813,
-0.011914417,
-0.0... | from transformers import AutoConfig, AutoModel, AutoModelForImageClassification
AutoConfig.register("resnet", ResnetConfig)
AutoModel.register(ResnetConfig, ResnetModel)
AutoModelForImageClassification.register(ResnetConfig, ResnetModelForImageClassification) |
[
0.03759042,
-0.017592205,
-0.0071720434,
-0.041072067,
-0.025914181,
-0.02113046,
-0.015511711,
-0.018073408,
-0.0016293665,
0.06063154,
0.015653241,
0.002689074,
0.042515676,
-0.04056256,
0.019545322,
0.043647915,
-0.00904378,
-0.044751853,
-0.07195396,
0.0055232164,
0.01209... | import timm
pretrained_model = timm.create_model("resnet50d", pretrained=True)
resnet50d.model.load_state_dict(pretrained_model.state_dict()) |
[
0.033091716,
0.003729033,
-0.010344614,
-0.039803974,
0.013700744,
0.035550114,
0.0006940145,
-0.019197615,
-0.009667863,
0.09104365,
0.023216683,
0.0044644815,
0.049002256,
-0.040798385,
-0.006449846,
0.05112919,
-0.013300218,
-0.033285074,
-0.0790555,
-0.0005369117,
-0.0019... |
First, make sure your model is fully defined in a .py file. It can rely on relative imports to some other files as
long as all the files are in the same directory (we don't support submodules for this feature yet). For our example,
we'll define a modeling_resnet.py file and a configuration_resnet.py file in a folder ... |
[
0.02197837,
0.0060681263,
0.026552478,
-0.019684236,
0.020703852,
0.006053965,
0.010868815,
-0.028308483,
0.027657062,
0.009013682,
0.02152521,
-0.0019578028,
0.052566826,
-0.06434905,
-0.009502247,
0.06973035,
0.02197837,
-0.019740883,
-0.035658207,
-0.02529212,
0.005926513,... | If copying a modeling files from the library, you will need to replace all the relative imports at the top of the file
to import from the transformers package. |
[
0.032628737,
0.0069755027,
-0.015841696,
-0.02987864,
-0.010062198,
0.002957785,
-0.009840185,
-0.0070256344,
0.0016409261,
0.0568926,
0.034061078,
-0.010627973,
0.061189625,
-0.028833032,
0.006642483,
0.032256328,
0.0057042995,
-0.011172263,
-0.08634154,
-0.01372183,
-0.0046... |
Note that you can re-use (or subclass) an existing configuration/model.
To share your model with the community, follow those steps: first import the ResNet model and config from the newly
created files:
py
from resnet_model.configuration_resnet import ResnetConfig
from resnet_model.modeling_resnet import ResnetModel,... |
[
0.010490341,
0.01307523,
-0.028735349,
-0.012292583,
-0.055977207,
0.0069576595,
0.010081067,
-0.005467758,
0.032196227,
0.05933756,
0.023307081,
0.028749708,
0.040640198,
-0.013527585,
-0.013398341,
-0.010009265,
0.03753833,
0.027715754,
-0.06565618,
0.009004029,
0.002925950... | Use register_for_auto_class() if you want the code files to be copied. If you instead prefer to use code on the Hub from another repo,
you don't need to call it. In cases where there's more than one auto class, you can modify the config.json directly using the
following structure:
json
"auto_map": {
"AutoCon... |
[
0.04625265,
-0.007108441,
-0.00767447,
0.0146579435,
-0.022626456,
-0.018730411,
0.05477984,
0.010063553,
0.0265372,
0.029962778,
-0.013114229,
0.011122101,
0.011857203,
-0.046517286,
-0.014393307,
0.015657684,
0.029271781,
-0.04043064,
-0.04666431,
0.018701008,
0.012129191,
... | Now to send the model to the Hub, make sure you are logged in. Either run in your terminal:
huggingface-cli login
or from a notebook:
from huggingface_hub import notebook_login
notebook_login() |
[
0.02455942,
0.0036277731,
-0.010147816,
-0.024189891,
-0.014781132,
-0.002583145,
0.008953955,
-0.004057705,
-0.016614562,
0.04798183,
0.045565683,
-0.03223424,
0.045480408,
-0.05355318,
0.022555439,
0.034337707,
-0.0022988925,
-0.022114849,
-0.051137034,
-0.034764085,
0.0244... | from transformers import AutoModelForImageClassification
model = AutoModelForImageClassification.from_pretrained("sgugger/custom-resnet50d", trust_remote_code=True) |
[
0.013898036,
-0.036360107,
-0.034404308,
-0.03707131,
-0.014779628,
-0.009616019,
0.024388239,
-0.015705671,
0.008215844,
0.062289283,
0.005467351,
-0.020491455,
0.036508273,
-0.03475991,
-0.02941109,
0.009971619,
0.019217221,
0.0051784264,
-0.043442477,
0.002409561,
0.035708... |
It is also strongly encouraged to pass a commit hash as a revision to make sure the author of the models did not
update the code with some malicious new lines (unless you fully trust the authors of the models).
py
commit_hash = "ed94a7c6247d8aedce4647f00f20de6875b5b292"
model = AutoModelForImageClassification.from_pr... |
[
0.03864961,
-0.0013042019,
-0.024693632,
-0.0017009315,
-0.021134188,
-0.00745629,
0.0129771335,
0.013207015,
0.006547891,
0.031085797,
0.013296001,
-0.0006581261,
0.02923192,
-0.05739601,
0.0028290153,
0.0060881297,
0.013859579,
-0.044582017,
-0.06768873,
0.0002134275,
0.015... | Next, let's create the config and models as we did before:
resnet50d_config = ResnetConfig(block_type="bottleneck", stem_width=32, stem_type="deep", avg_down=True)
resnet50d = ResnetModelForImageClassification(resnet50d_config)
pretrained_model = timm.create_model("resnet50d", pretrained=True)
resnet50d.model.load_sta... |
[
0.020449098,
-0.010803013,
-0.041739598,
-0.021831403,
0.00054418866,
0.020043422,
-0.003266071,
0.0030688671,
-0.00764024,
0.04678802,
0.025978317,
0.0022518798,
0.006960356,
-0.04817032,
-0.0044887345,
0.011133565,
0.017489161,
-0.005874796,
-0.062744625,
0.017459111,
0.031... |
You can then push to your own namespace (or an organization you are a member of) like this:
py
resnet50d.push_to_hub("custom-resnet50d")
On top of the modeling weights and the configuration in json format, this also copied the modeling and
configuration .py files in the folder custom-resnet50d and uploaded the result... |
[
0.021263367,
0.0038316676,
-0.012439143,
-0.0062232455,
-0.033915583,
-0.00023477229,
0.0026964948,
-0.015106249,
-0.007641293,
0.050285585,
0.02534852,
-0.036913324,
0.030418225,
-0.040234346,
0.010550862,
0.03068273,
-0.008927087,
0.03068273,
-0.08763975,
-0.01860361,
0.000... |
Load pretrained instances with an AutoClass
With so many different Transformer architectures, it can be challenging to create one for your checkpoint. As a part of 🤗 Transformers core philosophy to make the library easy, simple and flexible to use, an AutoClass automatically infers and loads the correct architecture ... |
[
0.012457602,
0.00544762,
-0.015251959,
-0.026966235,
-0.019064948,
-0.0025207717,
-0.029044796,
-0.0032468915,
-0.009945434,
0.04548057,
-0.017344285,
-0.015141836,
0.0022557895,
-0.030861815,
0.018500572,
0.046554264,
-0.00910575,
-0.04223196,
-0.056355163,
-0.029154917,
0.0... | AutoImageProcessor
For vision tasks, an image processor processes the image into the correct input format.
from transformers import AutoImageProcessor
image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
AutoBackbone
A Swin backbone with multiple stages for outputting a feature map. |
[
0.04405174,
-0.03966904,
-0.030060818,
-0.006040258,
0.00281469,
-0.020410452,
-0.0062509645,
-0.03295452,
-0.010949723,
0.0457093,
0.015339445,
-0.034078293,
0.022433236,
-0.06551573,
0.016378932,
0.04396746,
-0.04584977,
-0.01653345,
-0.059503563,
0.011174477,
0.023908183,
... | Remember, architecture refers to the skeleton of the model and checkpoints are the weights for a given architecture. For example, BERT is an architecture, while google-bert/bert-base-uncased is a checkpoint. Model is a general term that can mean either architecture or checkpoint.
In this tutorial, learn to:
Load a pr... |
[
0.013699291,
-0.013768725,
-0.032439478,
-0.020621842,
-0.005558177,
0.010192884,
-0.009297187,
-0.0066170422,
-0.01792781,
0.032550573,
0.0074919085,
-0.019149845,
0.0041695004,
-0.03599449,
0.00821402,
0.047464956,
-0.033605967,
-0.052908566,
-0.06026855,
-0.004294481,
-0.0... | Load a pretrained tokenizer.
Load a pretrained image processor
Load a pretrained feature extractor.
Load a pretrained processor.
Load a pretrained model.
Load a model as a backbone.
AutoTokenizer
Nearly every NLP task begins with a tokenizer. A tokenizer converts your input into a format that can be processed by the m... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.