Export torch.tensor to csv

Hello everyone.
Very simple and straightforward question.

I have a model that at the end of every training epoch generates a torch.tensor of dim[6].
[[a,b,c,d,e,f,g]]

I want to append it to a .csv file (at the end of each epoch) in order to the plot how the tensor evolves in time during training.

Is there a simple way to do that? Didn’t find anything online so far.

Thanks in advance. Best, kim

I guess you could just use Numpy for that

import numpy as np        

your_file = open('filename', 'ab')
for epoch in range(num_epochs):
    # ...
    np.savetxt(your_tensor.numpy())
your_file.close()
4 Likes

Can you elaborate (‘filename’, ‘ab’). What is ab?

ab opens a file for appending in binary format. You can find the different modes here.

2 Likes