Skip to content

Instantly share code, notes, and snippets.

@soumith
Created August 31, 2014 17:09
Show Gist options
  • Save soumith/8c4f4e3c2a079c29207b to your computer and use it in GitHub Desktop.
Save soumith/8c4f4e3c2a079c29207b to your computer and use it in GitHub Desktop.
numStrings = 10 -- for example, lets do 10, but this number can be anything upto memory limits
maxStringLength = 100 -- this has to be predetermined
-- allocate CharTensor
bigStringTensor = torch.CharTensor(numStrings, maxStringLength)
bst_data=torch.data(bigStringTensor) -- raw C pointer using torchffi
-- load some strings into the stringTensor
str='hello world'
-- write strings to tensor
for i=1,bigStringTensor:size(1) do
local stri = str .. ' ' .. i
ffi.copy(bst_data + ((i-1) * maxStringLength), stri)
end
-- read back data gotcha (torchffi seems to always return the parent tensor's data pointer)
for i=1,bigStringTensor:size(1) do
print(ffi.string(torch.data(bigStringTensor[i])))
end
-- read back properly
for i=1,bigStringTensor:size(1) do
print(ffi.string(bst_data + ((i-1) * maxStringLength)))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment