火炬:将张量保存到 csv 文件

Torch : Save tensor to csv file

我一直在使用 Torch。而我当前的程序需要导出一个包含简化特征矩阵的张量。 我尝试执行以下操作:

torch.save('t.csv',torch.Tensor({{1,2},{3,4}}),'ascii')

输出为:

4
1
3
V 1
18
torch.DoubleTensor
2
2 3
3 1
1
4
2
3
V 1
19
torch.DoubleStorage
6
1 2 3 4 5 6

预期输出:

1, 2, 3
4, 5, 6

我希望有人知道我该怎么做?

保存张量时,torch 不仅保存数据,而且——如您所见——还有一些其他有用的信息,供以后反序列化。

如果需要csv序列化,最好自己实现。

幸运的是,这非常简单。

这是一个简单的例子:

require 'torch'

matrix = torch.Tensor(5,3) -- a 5x3 matrix

matrix:random(1,10) -- matrix initialized with random numbers in [1,10]

print(matrix) -- let's see the matrix content

subtensor = matrix[{{1,3}, {2,3}}] -- let's create a view on the row 1 to 3, for which we take columns 2 to 3 (the view is a 3x2 matrix, note that values are bound to the original tensor)

local out = assert(io.open("./dump.csv", "w")) -- open a file for serialization

splitter = ","
for i=1,subtensor:size(1) do
    for j=1,subtensor:size(2) do
        out:write(subtensor[i][j])
        if j == subtensor:size(2) then
            out:write("\n")
        else
            out:write(splitter)
        end
    end
end

out:close()

我的计算机上矩阵的输出是:

 10  10   6
  4   8   3
  3   8   5
  5   5   5
  1   6   8
[torch.DoubleTensor of size 5x3]

文件转储内容:

10,6
8,3
8,5

HTH

您可以先使用 torch.totable. Then use the csvigo 库将张量转换为 Lua table 以将 table 另存为 csv 文件。这可能是一种解决方法,但我没有遇到任何问题。

对于简单的表格,您还可以通过将张量转换为 Numpy 数组然后再转换为 Pandas 数据帧来导出。

import pytorch as torch
import numpy as np
import pandas as pd

t = torch.tensor([[1,2],[3,4]]) #dummy data

t_np = t.numpy() #convert to Numpy array
df = pd.DataFrame(t_np) #convert to a dataframe
df.to_csv("testfile",index=False) #save to file

#Then, to reload:
df = pd.read_csv("testfile")