Saving 1D and 2D tensors to CSV with csvigo.

Let us establish some context on what we are doing in this notebook.
We will save a Tensor to CSV.

  • 1D tensor corresponds to 1 row of a CSV.
  • 2D tensor of NxM elements is written to a CSV of N rows and M columns.

In [1]:
require 'csvigo';

1D Tensor


In [2]:
x = torch.randn(5)

In [3]:
csvigo.save{path='test.csv', data={x:storage():totable()}, mode='raw', header=false}


Out[3]:
<csv>	writing to file: test.csv	
<csv>	writing done	


In [4]:
os.execute('cat test.csv');


Out[4]:
-0.92156727046993,0.063969336305798,0.53617085675568,-1.0906517515787,-0.66408870766704

1D Table of tensors


In [5]:
x = {}
for i=1,3 do table.insert(x, torch.randn(5)) end -- 3 rows

In [6]:
y = {}
for i=1,3 do table.insert(y, x[i]:totable()) end
csvigo.save{path='test.csv', data=y, mode='raw', header=false}


Out[6]:
<csv>	writing to file: test.csv	
<csv>	writing done	

In [7]:
os.execute('cat test.csv');


Out[7]:
-0.45249850299069,-0.38236193063481,-1.9773627342887,-0.45820720335177,-1.4173797526297
-0.33366199866588,0.019158580858199,0.21597690613389,-0.73669091233258,-0.35948891584342
1.8003581485223,-1.3115072980388,-1.8034916315555,1.4939233832523,0.34680344685565

2D Tensor


In [8]:
x = torch.randn(3,5)

In [9]:
csvigo.save{path='test.csv', data=x:totable(), mode='raw', header=false}


Out[9]:
<csv>	writing to file: test.csv	
<csv>	writing done	


In [10]:
os.execute('cat test.csv');


Out[10]:
0.78265112949061,0.1240632892343,-1.6179868819963,1.1427780559352,-0.81443350321836
-0.25514381570671,0.36336968601056,2.0435222445908,1.3057005793111,1.8757921782746
-0.30003541445729,0.79124701951781,0.84915824014005,-0.20883256099729,-0.79653516737091

3D Tensor???

If you think about a 3D tensor, there is no way to write it as CSV, CSV files have rows and columns, they do not have a third dimension.
However, let's say you have a 3D tensor, but want to write each 2D slice after the other, i.e.
if you have a tensor t which is of size 3x2x2, then you want to write t[1], then t[2] and then t[3].
This can be simply done using a tensor view, and writing as a 2D tensor, i.e.:


In [11]:
x = torch.randn(3,2,2)

In [12]:
y=x:view(3*2,2)

In [13]:
csvigo.save{path='test.csv', data=y:totable(), mode='raw', header=false}


Out[13]:
<csv>	writing to file: test.csv	
<csv>	writing done	


In [14]:
os.execute('cat test.csv');


Out[14]:
0.80939151228189,2.0465123158299
0.472215830434,0.42165457275287
0.266179276152,-0.62882089467651
-0.13670060957014,0.46700229912284
-0.42347449455605,0.1821819276652
-0.18885369742665,-0.23470594891685

In [ ]: