Save tensor append mode

Hi, How to save several tensor appending using torch.save()?
for example:

for i in range(20):
       ......
    loss = criterion(scores, labels) 
    torch.save(loss,'loss.pt')

How to save these all 20 losses?

1 Like
for i in range(20):
       ......
    loss = criterion(scores, labels) 
    torch.save(loss,'loss'+i+'.pt')

this is what you are talking about? each file will be saved with a different name. i understood your question like this. or you want to append all the losses into a single tensor and then save it?
with torch.save(Tensor) you can save tensors

Hi, Thank you for your reply. I would like to save them in a list or tensor whatever for plotting afterwards, what are your suggestion?

okay, you can do something like:

Losses=[]
for i in range(20):
       ......
    loss = criterion(scores, labels) 
    Losses.append(loss.item())
StackedLossTensor=torch.stack(Losses)
torch.save(StackedLossTensor,'loss.pt')

i think this will work for you.

1 Like

is there a way where i can append them only upon saving. i dont have enough memory on my cpu so i was hoping i could save them iteratively through the data

1 Like

loss.item() should be a float32 scalar value. How large is the expected StackedLossTensor, that you are running out of memory?

The posted code snippet assumed that the loss is stored for each batch during training.
Let me know, if I misunderstood your question. :wink: