From 8895b689804ee868bac33fd9bfb89ec5fa78acc7 Mon Sep 17 00:00:00 2001 From: Yotam Doron Date: Sat, 6 Feb 2016 12:05:09 +0000 Subject: [PATCH] Make :totable work for empty tensors. --- Tensor.lua | 5 +++-- test/test.lua | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Tensor.lua b/Tensor.lua index 068768b0..0d573aae 100644 --- a/Tensor.lua +++ b/Tensor.lua @@ -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 diff --git a/test/test.lua b/test/test.lua index 4e431201..73b07bda 100644 --- a/test/test.lua +++ b/test/test.lua @@ -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)