How to save training iterations output?

Hello,

So I have a training loop and I am printing the following:

Train Epoch: 1 [0/8812 (0%)]	Loss: 0.509592	Valid Loss: 0.463734
Train Epoch: 1 [1200/8812 (36%)]	Loss: 0.600266	Valid Loss: 0.387744
Train Epoch: 1 [2400/8812 (72%)]	Loss: 0.351144	Valid Loss: 0.368207
Epoch 1 took 595.4694926738739 seconds
Train Epoch: 2 [0/8812 (0%)]	Loss: 0.179442	Valid Loss: 0.367970
Train Epoch: 2 [1200/8812 (36%)]	Loss: 0.178893	Valid Loss: 0.362563
Train Epoch: 2 [2400/8812 (72%)]	Loss: 0.000000	Valid Loss: 0.372528
Epoch 2 took 535.0275015830994 seconds
Train Epoch: 3 [0/8812 (0%)]	Loss: 0.276426	Valid Loss: 0.365664
Train Epoch: 3 [1200/8812 (36%)]	Loss: 0.267340	Valid Loss: 0.367112
Train Epoch: 3 [2400/8812 (72%)]	Loss: 0.270740	Valid Loss: 0.384545
Epoch 3 took 548.3842833042145 seconds
Train Epoch: 4 [0/8812 (0%)]	Loss: 0.364331	Valid Loss: 0.365495
Train Epoch: 4 [1200/8812 (36%)]	Loss: 0.300914	Valid Loss: 0.363899
Train Epoch: 4 [2400/8812 (72%)]	Loss: 0.441467	Valid Loss: 0.360874
Epoch 4 took 539.1664731502533 seconds
Train Epoch: 5 [0/8812 (0%)]	Loss: 0.599065	Valid Loss: 0.358953
Train Epoch: 5 [1200/8812 (36%)]	Loss: 0.374723	Valid Loss: 0.358028
Train Epoch: 5 [2400/8812 (72%)]	Loss: 0.263893	Valid Loss: 0.361765

Is there any way to store this in a file (but also printing) or in a table or something?

I want to use these as results for a research…

Thanks.
BR,
Shweta.

You can use pickle library to do that.

First you need to create a empty list for storing results. Then you can append the needed infomation (epoch, loss , valid_loss) into that list in each training loop. It will be easy to append these information as a tuple in each iteration.

Then after training you can save this list to a file via pickle library. Which is a file you can load into python later, as the saved list

As a example–>

import pickle as pkl

results = []
.
.
for e in range(epochs):
             .
             .
             .
             .
             results.append((epoch, loss.item(), valid_loss.item()
             .
             .

with open('results.pkl', 'wb') as f:       #this will save the list as "results.pkl" which you can load in later 
             pkl.dump(results, f)          #as a list to python  

Loading the saved list ---->

with open('results.pkl', 'rb') as f:
    results = pkl.load(f)

Hope this helps

1 Like