Reputation: 3470
I'm using a Torch command to insert data into a simple table, and it works fine:
completeProfile = {};
table.foreach(firstHalf, function(i,v)table.insert(completeProfile,v) end);
table.foreach(secondHalf, function(i,v)table.insert(completeProfile,v) end);
table.foreach(presentWord, function(i,v) table.insert(completeProfile,v) end);
Now someone made me notice that using Torch Tensors would make everything more efficient. So I replaced the first line with
completeProfile = torch.Tensor(CONSTANT_NUMBER);
but unfortunately I cannot find any Tensor function able to replace the table.insert() function.
Do you have any idea?
Upvotes: 0
Views: 5513
Reputation: 6251
There is no function which corresponds to the append functionality of insert since Tensor objects are a fixed size. What I see your code doing is concatenating three tables into one. If you are using Tensors:
firstHalf = torch.Tensor(firstHalf)
secondHalf = torch.Tensor(secondHalf)
presentWord = torch.Tensor(presentWord)
then concatenating them together is easy:
completeProfile = firstHalf:cat(secondHalf):cat(presentWord)
Another option is to store the last index you inserted to so you know where to "append" onto the tensor. The function below creates a closure that will keep track of that last index for you.
function appender(t)
local last = 0
return function(i, v)
last = last + 1
t[last] = v
end
end
completeProfile = torch.Tensor(#firstHalf + #secondHalf + #presentWord)
profile_append = appender(completeProfile)
table.foreach(firstHalf, profile_append)
table.foreach(secondHalf, profile_append)
table.foreach(presentWord, profile_append)
Upvotes: 2