将字符串字符拆分为火炬张量

时间:2016-01-13 06:35:55

标签: string split lua torch

我在Lua中有2个字符串:

a = '01234'
b = '12345'

我想将它们分成几个字符并将它们加载到Torch Tensor中,如下所示:

tens = torch.Tensor{{0, 1, 2, 3, 4}, {1, 2, 3, 4, 5}} 

感谢。

1 个答案:

答案 0 :(得分:2)

function split(str)
    tbl = {}
    for c in str:gmatch('.') do
         table.insert(tbl, c)
    end
    return tbl
end

a = '01234'
b = '12345'

tens = torch.Tensor{split(a), split(b)}