Skip to content

Commit

Permalink
Make :totable work for empty tensors.
Browse files Browse the repository at this point in the history
  • Loading branch information
yotam committed Feb 6, 2016
1 parent f056171 commit 8895b68
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Tensor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,10 @@ torch.chunk = Tensor.chunk

function Tensor.totable(tensor)
local result = {}
if tensor:dim() == 1 then
local dim = tensor:dim()
if dim == 1 then
tensor:apply(function(i) table.insert(result, i) end)
else
elseif dim > 0 then
for i = 1, tensor:size(1) do
table.insert(result, tensor[i]:totable())
end
Expand Down
4 changes: 4 additions & 0 deletions test/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2947,6 +2947,10 @@ function torchtest.chunk()
end

function torchtest.totable()
local table0D = {}
local tensor0D = torch.Tensor(table0D)
mytester:assertTableEq(torch.totable(tensor0D), table0D, 'tensor0D:totable incorrect')

local table1D = {1, 2, 3}
local tensor1D = torch.Tensor(table1D)
local storage = torch.Storage(table1D)
Expand Down

0 comments on commit 8895b68

Please sign in to comment.