5

In pytorch, I want to write a tensor to a file and visually read the file contents. For example, consider T = torch.tensor([3,4,5,6]). I want to write the tensor T to a file, say file_T.txt, and want to visually read the contents of the file_T.txt, which will be 3,4,5 and 6. How can I achieve this?

Kiran
  • 87
  • 1
  • 1
  • 5

2 Answers2

9

You can use numpy:

import numpy as np
np.savetxt('my_file.txt', torch.Tensor([3,4,5,6]).numpy())
guibs35
  • 237
  • 2
  • 11
0

You can use numpy.savetxt() but it won't work if your tensor's dimensions are higher than 2. Moreover, this method could be used only if your tensor is on a CPU.

Tony.sy
  • 11
  • 2