Save tensor to txt file with full data precision

Hi,

I could save tensor to txt file like this:
fp.write('module.weight.data == {}\n'.format(module.weight.data))
however, the data in file use scientific value:
[[-1.1359e+00, 1.4675e+00, 2.6742e+00, ..., -6.3853e-01, -8.0900e-01, -1.1815e+00], [ 5.4580e+00, 1.1344e+00, -6.1044e-01, ..., -1.0242e-01, 6.5639e-01, -4.6325e-01], [-3.2800e+00, -1.1235e+00, -9.9800e-01, ..., -7.8122e-01, -2.4848e-01, -9.8628e-01],

I would like to save the data with full precision.
is threre any simple way to save full precision data to txt file?

Store the data in a binary format via torch.save to keep the full precision.
If you really want to write the data to a text file, format the string. Something like '{:.25f}'.format(tensor) might work (assuming you don’t have values really close to zero).

@ptrblck ,
Thank you!

code:
fp.write('module.weight.data == {:.9f}\n'.format(module.weight.data))

  • module is the layer(sub-class of nn.Module) of network.

Error:

Exception has occurred: TypeError       (note: full exception trace is shown but execution is paused at: hook_save_data_student)
unsupported format string passed to Tensor.__format__

It seems the tensor cannot be passed to the format directly (a scalar can), so a workaround could be:

', '.join(['{:.25f}'.format(x_) for x_ in x])

However, I think grabbing the numpy array and using np.savetxt might be the better idea.