Pytorch Lightning Automatic Logging - AttributeError: 'NoneType' object has no attribute '_results'
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(&quot;train_loss&quot;, loss) # &lt;-- 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)
&lt;ipython-input-34-b9419bfca30f&gt; in &lt;module&gt;
     25 y = torch.randint(0,4, size=(10,))
     26 batch = (x,y)
---&gt; 27 loss = pl_model.training_step(batch, 0)

&lt;ipython-input-34-b9419bfca30f&gt; in training_step(self, batch, batch_idx)
     14         y_hat = self(x)
     15         loss = F.cross_entropy(y_hat, y.long())
---&gt; 16         self.log(&quot;train_loss&quot;, 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 
--&gt; 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'


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 &quot;what to do&quot; in a specific situation (in this case, at each training step). It is the Trainer that actually &quot;orchestrates&quot; 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.
