1

Unable to use Automatic Logging (self.log) when calling training_step() on Pytorch Lightning, what am I missing? Here is a minimal example:

import pytorch_lightning as pl
import torch
import torch.nn as nn
import torch.nn.functional as F

class LitModel(pl.LightningModule):
    def __init__(self):
        super().__init__()
        self.l1 = nn.Linear(100, 4)

    def forward(self, x):
        return torch.relu(self.l1(x.view(x.size(0), -1)))

    def training_step(self, batch, batch_idx):
        x, y = batch
        y_hat = self(x)
        loss = F.cross_entropy(y_hat, y.long())
        self.log("train_loss", loss) # <-- error
        return loss

    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=0.02)

pl_model = LitModel()
x = torch.rand((10,100))
y = torch.randint(0,4, size=(10,))
batch = (x,y)
loss = pl_model.training_step(batch, 0)

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-34-b9419bfca30f> in <module>
     25 y = torch.randint(0,4, size=(10,))
     26 batch = (x,y)
---> 27 loss = pl_model.training_step(batch, 0)

<ipython-input-34-b9419bfca30f> in training_step(self, batch, batch_idx)
     14         y_hat = self(x)
     15         loss = F.cross_entropy(y_hat, y.long())
---> 16         self.log("train_loss", loss)
     17         return loss
     18 

D:\programs\anaconda3\lib\site-packages\pytorch_lightning\core\lightning.py in log(self, name, value, prog_bar, logger, on_step, on_epoch, reduce_fx, tbptt_reduce_fx, tbptt_pad_token, enable_graph, sync_dist, sync_dist_op, sync_dist_group, add_dataloader_idx, batch_size, metric_attribute, rank_zero_only)
    405         on_epoch = self.__auto_choose_log_on_epoch(on_epoch)
    406 
--> 407         results = self.trainer._results
    408         assert results is not None
    409         assert self._current_fx_name is not None

AttributeError: 'NoneType' object has no attribute '_results'
3
  • Seems like a bug was introduced in 1.4.2, I have the same issue. I will revert to a previous version and let you know Commented Aug 23, 2021 at 11:50
  • update: 1.4.0, 1.4.1 produce the same error. Commented Aug 23, 2021 at 14:27
  • I think you must pass a logger object through a Trainer for self.log to work. I was thrown off by this too. Hope someone else can provide a better answer. Commented Aug 23, 2021 at 15:01

1 Answer 1

2

This is NOT the correct usage of LightningModule class. You can't call a hook (namely .training_step()) manually and expect everything to work fine.

You need to setup a Trainer as suggested by PyTorch Lightning at the very start of its tutorial - it is a requirement. The functions (or hooks) that you define in a LightningModule merely tells Lightning "what to do" in a specific situation (in this case, at each training step). It is the Trainer that actually "orchestrates" the training by instantiating the necessary environment (including Logging functionality) and feeding it into the Lightning Module whenever needed.

So, do it the way Lightning suggests and it will work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.