mirror of
https://github.com/facebookresearch/fairseq.git
synced 2024-09-11 17:25:31 +03:00
Summary: Pull Request resolved: https://github.com/pytorch/translate/pull/283 Pull Request resolved: https://github.com/pytorch/fairseq/pull/428 Differential Revision: D13564190 Pulled By: myleott fbshipit-source-id: 3b62282d7069c288f5bdd1dd2c120788cee4abb5
49 lines
1.7 KiB
ReStructuredText
49 lines
1.7 KiB
ReStructuredText
Overview
|
|
========
|
|
|
|
Fairseq can be extended through user-supplied `plug-ins
|
|
<https://en.wikipedia.org/wiki/Plug-in_(computing)>`_. We support five kinds of
|
|
plug-ins:
|
|
|
|
- :ref:`Models` define the neural network architecture and encapsulate all of the
|
|
learnable parameters.
|
|
- :ref:`Criterions` compute the loss function given the model outputs and targets.
|
|
- :ref:`Tasks` store dictionaries and provide helpers for loading/iterating over
|
|
Datasets, initializing the Model/Criterion and calculating the loss.
|
|
- :ref:`Optimizers` update the Model parameters based on the gradients.
|
|
- :ref:`Learning Rate Schedulers` update the learning rate over the course of
|
|
training.
|
|
|
|
**Training Flow**
|
|
|
|
Given a ``model``, ``criterion``, ``task``, ``optimizer`` and ``lr_scheduler``,
|
|
fairseq implements the following high-level training flow::
|
|
|
|
for epoch in range(num_epochs):
|
|
itr = task.get_batch_iterator(task.dataset('train'))
|
|
for num_updates, batch in enumerate(itr):
|
|
task.train_step(batch, model, criterion, optimizer)
|
|
average_and_clip_gradients()
|
|
optimizer.step()
|
|
lr_scheduler.step_update(num_updates)
|
|
lr_scheduler.step(epoch)
|
|
|
|
where the default implementation for ``train.train_step`` is roughly::
|
|
|
|
def train_step(self, batch, model, criterion, optimizer):
|
|
loss = criterion(model, batch)
|
|
optimizer.backward(loss)
|
|
|
|
**Registering new plug-ins**
|
|
|
|
New plug-ins are *registered* through a set of ``@register`` function
|
|
decorators, for example::
|
|
|
|
@register_model('my_lstm')
|
|
class MyLSTM(FairseqModel):
|
|
(...)
|
|
|
|
Once registered, new plug-ins can be used with the existing :ref:`Command-line
|
|
Tools`. See the Tutorial sections for more detailed walkthroughs of how to add
|
|
new plug-ins.
|