Pytorch-lightning: How to store test_step outputs to file?

Created on 28 Sep 2020  路  9Comments  路  Source: PyTorchLightning/pytorch-lightning

Is there an approach to save to one file all the outputs during test_step?

    def test_step(self, batch, batch_idx):
        x1, x2 = batch["x1"], batch["x2"]
        r1, r2 = self(x1, x2)

        test_loss = self.loss_fn(predict)
        test_mrr = self.mrr(r1, r2)

        return {'test_loss': test_loss, 'test_mrr': test_mrr}

My LightningModule outputs two dense vectors for representation r1 and r2. They must be saved to be used in a downstream task and for the measurement of some metrics.

question won't fix

All 9 comments

EvalResult.write_dict()

I am sorry but I could not understand how to use TrainResult or EvalResult to save on file the outputs of the LightningModule.

It would be something like:


def test_step(self, batch, batch_idx):
        x1, x2 = batch["x1"], batch["x2"]
        r1, r2 = self(x1, x2)

        # saving prediction to file?
        predictions_to_write = {'r1': r1, 'r2': r2}
        result.write_dict(predictions_to_write)

        # loss and metric  
        test_loss = self.loss_fn(predict)
        test_mrr = self.mrr(r1, r2)

        return {'test_loss': test_loss, 'test_mrr': test_mrr}


def test_step(self, batch, batch_idx):
        x1, x2 = batch["x1"], batch["x2"]
        r1, r2 = self(x1, x2)

        # saving prediction to file?
        predictions_to_write = {'r1': r1, 'r2': r2}
        result.write_dict(predictions_to_write, filename)

        # log or attach other metrics
        result.log('test_loss', test_loss)
        # or
        result.test_loss = test_loss
        return result

This issue has been automatically marked as stale because it hasn't had any recent activity. This issue will be closed in 7 days if no further activity occurs. Thank you for your contributions, Pytorch Lightning Team!

Closing it as it is resolved, feel free to reopen if needed

Hello @rohitgr7,

As EvalResult and TrainResult is not supported anymore (after pr 3938), how could someone save predictions to file?

Thank you @rohitgr7,
Then, could I use test_step to save predictions_dict, reload it from disk in test_epoch_end, and use reloaded predictions_dict to calculate a metric?

@Ceceu I think it aggregates everything and saves it after epoch_end, but not sure if I am correct here. Maybe try and tell??

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DavidRuhe picture DavidRuhe  路  3Comments

williamFalcon picture williamFalcon  路  3Comments

chuong98 picture chuong98  路  3Comments

remisphere picture remisphere  路  3Comments

edenlightning picture edenlightning  路  3Comments