以视觉可读的方式将张量写入文件

Writing tensor to a file in a visually readable manner

在pytorch中,我想将一个tensor写到一个文件中,并可视化读取文件内容。例如,考虑 T = torch.tensor([3,4,5,6])。我想将张量 T 写入文件,比如 file_T.txt,并想直观地读取 file_T.txt 的内容,即 3、4、5 和 6。我该如何实现?

你可以使用 numpy:

import numpy as np
np.savetxt('my_file.txt', torch.Tensor([3,4,5,6]).numpy())