# Trainer

## Class: Trainer

<pre class="language-python"><code class="lang-python">class Trainer(pl.Trainer):
<strong>    def __init__(self, config, model_mode, extra_epoch=0):
</strong></code></pre>

### Description

The `Trainer` class is an extended implementation of the `pytorch_lightning.Trainer` that includes configurable options for training a model. It allows to initialize or load configuration parameters and save the best performing model checkpoints during the training process.

### Inheritance

This class inherits from `Trainer` class in `pytorch_lightning`.

### Parameters

* `config` (dict): A dictionary containing configuration parameters for the training process.
* `model_mode` (str): Indicates whether to start a new training session ('start') or continue a previous one ('continue').
* `extra_epoch` (int, default=0): Extra number of epochs to train if `model_mode` is 'continue'.

### Methods

* `__init__(self, config, model_mode, extra_epoch=0)`: Initializes the `Trainer` object.
* `initialize_config(self)`: Saves the current configuration to a YAML file.
* `load_config(self)`: Loads a previously saved configuration from a YAML file, and updates the 'max\_epochs' parameter based on the `extra_epoch`.
* `fit(self, model, dataloader, ckpt_path=None)`: Fits the model using the given dataloader.

#### fit Function

```python
def fit(self, model, dataloader, ckpt_path=None):
```

The `fit` function initiates the training process for the model on the given dataloader. It supports resuming training from the latest saved checkpoint when `model_mode` is set to 'continue' and `ckpt_path` is set to 'latest'.

**Parameters**

* `model` (nn.Module): The PyTorch model to be trained.
* `dataloader` (DataLoader): The DataLoader object for the training data.
* `ckpt_path` (str, default=None): The path to a checkpoint from which to load model weights. If 'latest', it will load the most recent checkpoint from the directory specified in the configuration.

**Returns**

This function doesn't return a value; it initiates the training process on the specified model using the given data.

**Example**

Here's an example of how to use the `fit` function:

```python
trainer = Trainer(config, "start")
trainer.fit(model, dataloader)
```

This code initiates a new training process for the `model` on the data from `dataloader` according to the parameters specified in `config`. Model checkpoints and the training log will be saved in the directory specified in `config`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://autossl.gitbook.io/autossl/api/train/trainer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
