Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert torch Tensor/ Storage to a lua table?

Tags:

torch

lua

If I have a tensor:

t1 = torch.Tensor(2, 2)

Is there any way get this data as a Lua table?

like image 235
Tom Avatar asked Aug 11 '14 16:08

Tom


1 Answers

There is a dedicated constructor to create a tensor from a table but so far there is no method out-of-the box to convert the other way around.

Of course you can do that manually:

-- This assumes `t1` is a 2-dimensional tensor!
local t2 = {}
for i=1,t1:size(1) do
  t2[i] = {}
  for j=1,t1:size(2) do
    t2[i][j] = t1[i][j]
  end
end

--

Update: as of commit 10f3323 there is now a dedicated torch.totable(object) converter.

like image 150
deltheil Avatar answered Sep 16 '22 11:09

deltheil