Torch : Save tensor to csv file
Asked Answered
P

3

7

I have been working with Torch. And my current program requires to export a Tensor containing reduced feature matrix. I tried doing the following:

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

and the output was:

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

Expected output:

1, 2, 3
4, 5, 6

I'm hoping somebody has an idea as to how I can do this?

Phina answered 22/3, 2016 at 15:7 Comment(0)
C
6

When saving tensor, torch saves not only data but also -- as you can see -- several other useful information for later deserialisation.

If you need csv serialisation, you are good to implement it yourself.

Fortunately, this is very straightforward.

Here is a quick example :

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()

The output on my computer for the matrix is :

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

and the file dumped content :

10,6
8,3
8,5

HTH

Citreous answered 24/3, 2016 at 8:19 Comment(0)
C
10

For simple tables, you can also export by converting the tensor to a Numpy array and then to a Pandas dataframe.

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")
Cratch answered 11/6, 2021 at 1:14 Comment(1)
question is probably about lua+torch, not pytorch:)Katabasis
C
6

When saving tensor, torch saves not only data but also -- as you can see -- several other useful information for later deserialisation.

If you need csv serialisation, you are good to implement it yourself.

Fortunately, this is very straightforward.

Here is a quick example :

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()

The output on my computer for the matrix is :

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

and the file dumped content :

10,6
8,3
8,5

HTH

Citreous answered 24/3, 2016 at 8:19 Comment(0)
H
-1

You can first convert the tensor to a Lua table using torch.totable. Then use the csvigo library to save the table as a csv file. This may be a workaround but I haven't had any problem with it.

Helpless answered 12/1, 2017 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.