--- title: PL Trainer keywords: fastai sidebar: home_sidebar summary: "Implementation of trainer for training PyTorch Lightning models." description: "Implementation of trainer for training PyTorch Lightning models." nb_path: "nbs/trainers/pl_trainer.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}

#### `pl_trainer`[source]

> `pl_trainer`(`model`, `datamodule`, `max_epochs`=`10`, `val_epoch`=`5`, `gpus`=`None`, `log_dir`=`None`, `model_dir`=`None`)

{% endraw %} {% raw %}
{% endraw %}

Example

{% raw %}

    class Args:
        def __init__(self):
            self.data_dir = '/content/data'
            self.min_rating = 4
            self.num_negative_samples = 99
            self.min_uc = 5
            self.min_sc = 5
            self.val_p = 0.2
            self.test_p = 0.2
            self.num_workers = 2
            self.normalize = False
            self.batch_size = 32
            self.seed = 42
            self.shuffle = True
            self.pin_memory = True
            self.drop_last = False
            self.split_type = 'stratified'

    args = Args()

    from recohut.datasets.movielens import ML1mDataModule

    ds = ML1mDataModule(**args.__dict__)

    ds.prepare_data()

    Processing...

    Turning into implicit ratings
    Filtering triplets
    Densifying index

    Done!

{% endraw %} {% raw %}

    from recohut.models.nmf import NMF

    model = NMF(n_items=ds.data.num_items, n_users=ds.data.num_users, embedding_dim=20)

{% endraw %} {% raw %}

    pl_trainer(model, ds, max_epochs=5)

    GPU available: False, used: False
    TPU available: False, using: 0 TPU cores
    IPU available: False, using: 0 IPUs

      | Name               | Type      | Params
    -------------------------------------------------
    0 | user_embedding     | Embedding | 120 K
    1 | item_embedding     | Embedding | 62.5 K
    2 | user_embedding_gmf | Embedding | 120 K
    3 | item_embedding_gmf | Embedding | 62.5 K
    4 | gmf                | Linear    | 210
    5 | fc1                | Linear    | 820
    6 | fc2                | Linear    | 420
    7 | fc3                | Linear    | 210
    8 | fc_final           | Linear    | 21
    9 | dropout            | Dropout   | 0
    -------------------------------------------------
    368 K     Trainable params
    0         Non-trainable params
    368 K     Total params
    1.472     Total estimated model params size (MB)

    --------------------------------------------------------------------------------
    DATALOADER:0 TEST RESULTS
    {'Test Metrics': {'apak': tensor(0.0752),
                      'hr': tensor(0.2275),
                      'loss': tensor(0.1823),
                      'ncdg': tensor(0.1102)}}
    --------------------------------------------------------------------------------

    [{'Test Metrics': {'apak': tensor(0.0752),
       'hr': tensor(0.2275),
       'loss': tensor(0.1823),
       'ncdg': tensor(0.1102)}}]

{% endraw %}