Tom
Tom

Reputation: 6971

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

If I have a tensor:

t1 = torch.Tensor(2, 2)

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

Upvotes: 8

Views: 10431

Answers (1)

deltheil
deltheil

Reputation: 16121

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.

Upvotes: 22

Related Questions