Logging a tensor of best model residuals

Hi, I try to log the residuals of the best model of the run and I cannot do it with the wandb_logger (lightning).

I do:

self.log("%s_residuals" % mode, residuals)

And I get:

Exception has occurred: ValueError
`self.log(val_residuals, tensor([ 922.9688,  892.0009, 1414.1432, 1549.3324, 1341.1199,  902.2733,
        1103.2998, 1066.6083,  837.4913,  921.2358, 1132.7634, 1488.9233,
        1219.2042,  640.6171,  875.3945, 1070.0031,  917.9532, 1288.4425,
        1154.5270, 1156.2522,  914.9966, 1250.5038, 1070.0439, 1272.7135,
         936.7913,  703.1593,  726.7241, 1387.6118, 1317.7161,  959.2494,
         727.7173,  912.6650], device='cuda:0'))` was called, but the tensor must have a single element. You can try doing `self.log(val_residuals, tensor([ 922.9688,  892.0009, 1414.1432, 1549.3324, 1341.1199,  902.2733,
        1103.2998, 1066.6083,  837.4913,  921.2358, 1132.7634, 1488.9233,
        1219.2042,  640.6171,  875.3945, 1070.0031,  917.9532, 1288.4425,
        1154.5270, 1156.2522,  914.9966, 1250.5038, 1070.0439, 1272.7135,
         936.7913,  703.1593,  726.7241, 1387.6118, 1317.7161,  959.2494,
         727.7173,  912.6650], device='cuda:0').mean())`
  File "C:\github\absdabsde\absdabsde\absdabsde.py", line 241, in _calculate_loss
    self.log("%s_residuals" % mode, residuals)
  File "C:\github\absdabsde\absdabsde\absdabsde.py", line 209, in validation_step
    self._calculate_loss(batch, mode="val")
  File "C:\github\absdabsde\absdabsde\absdabsde.py", line 270, in train_model
    trainer.fit(model, train_loader, val_loader)
  File "C:\github\absdabsde\absdabsde\absdabsde.py", line 290, in <module>
    model, results = train_model(
ValueError: `self.log(val_residuals, tensor([ 922.9688,  892.0009, 1414.1432, 1549.3324, 1341.1199,  902.2733,
        1103.2998, 1066.6083,  837.4913,  921.2358, 1132.7634, 1488.9233,
        1219.2042,  640.6171,  875.3945, 1070.0031,  917.9532, 1288.4425,
        1154.5270, 1156.2522,  914.9966, 1250.5038, 1070.0439, 1272.7135,
         936.7913,  703.1593,  726.7241, 1387.6118, 1317.7161,  959.2494,
         727.7173,  912.6650], device='cuda:0'))` was called, but the tensor must have a single element. You can try doing `self.log(val_residuals, tensor([ 922.9688,  892.0009, 1414.1432, 1549.3324, 1341.1199,  902.2733,
        1103.2998, 1066.6083,  837.4913,  921.2358, 1132.7634, 1488.9233,
        1219.2042,  640.6171,  875.3945, 1070.0031,  917.9532, 1288.4425,
        1154.5270, 1156.2522,  914.9966, 1250.5038, 1070.0439, 1272.7135,
         936.7913,  703.1593,  726.7241, 1387.6118, 1317.7161,  959.2494,
         727.7173,  912.6650], device='cuda:0').mean())

Any suggestion on how I can log my best model’s residuals will be helpful!!

Hi @sashapiv , happy to help. This error is stemming from the LightningModule which accept single value in self.log(val_residuals, <val>), thus the recommendation of taking the average across your tensor per log call, “You can try doing self.log(val_residuals, <tensor>).mean()”. How are you currently calculating your residuals?

Hi @sashapiv , since we have not heard back from you we are going to close this request. If you would like to re-open the conversation, please let us know!

Thank you @mohammadbakir , currently I calculate residuals inside a step:

predictions = self.model(data1, data2)
residuals = targets - predictions

I perform additional metric on the residuals and I want to make an analysis on the best model’s residuals after the run is finished, so logging the mean is not enough for me…