Created
August 31, 2014 17:09
-
-
Save soumith/8c4f4e3c2a079c29207b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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