在lua/torch中,不用打印类型写出张量
In lua/torch, write tensor out without printing type
我是 lua 的新手,只是找不到这个看起来非常简单的问题的答案。
我想打印出一些张量,这些张量对应于 Word2Vec 样式的词嵌入。每行应以一个词开头,然后是张量元素。我有以下代码:
function Word2Vec:print_semantic_space()
if self.word_vecs_norm == nil then
self.word_vecs_norm = self:normalize(self.word_vecs.weight:double())
end
for word,_ in pairs(self.vocab) do
vec=self.word_vecs_norm[self.word2index[word]]
vec:resize(vec:size(1),1)
vec=vec:t()
io.write(word," ",tostring(vec))
end
end
一切都很好,但我也不断打印出张量类型和大小:
usually -0.2063 0.5654 0.1447 0.2765 -0.3903 0.2646 0.2254 0.5064 -0.1009 -0.0260
[torch.DoubleTensor of size 1x10]
go -0.5896 0.1330 0.1361 -0.0193 -0.5612 0.3529 0.3683 0.0141 0.0447 -0.1963
[torch.DoubleTensor of size 1x10]
如何告诉 lua 不要 return 类型?像这样:
usually -0.2063 0.5654 0.1447 0.2765 -0.3903 0.2646 0.2254 0.5064 -0.1009 -0.0260
go -0.5896 0.1330 0.1361 -0.0193 -0.5612 0.3529 0.3683 0.0141 0.0447 -0.1963
抱歉,如果答案在那里,我没有搜索正确的关键字。我对 lua 的概念还是陌生的。
您可以编写自己的转储函数,例如:
local dump = function(vec)
vec = vec:view(vec:nElement())
local t = {}
for i=1,vec:nElement() do
t[#t+1] = string.format('%.4f', vec[i])
end
return table.concat(t, ' ')
end
并用它代替 tostring
。
我是 lua 的新手,只是找不到这个看起来非常简单的问题的答案。
我想打印出一些张量,这些张量对应于 Word2Vec 样式的词嵌入。每行应以一个词开头,然后是张量元素。我有以下代码:
function Word2Vec:print_semantic_space()
if self.word_vecs_norm == nil then
self.word_vecs_norm = self:normalize(self.word_vecs.weight:double())
end
for word,_ in pairs(self.vocab) do
vec=self.word_vecs_norm[self.word2index[word]]
vec:resize(vec:size(1),1)
vec=vec:t()
io.write(word," ",tostring(vec))
end
end
一切都很好,但我也不断打印出张量类型和大小:
usually -0.2063 0.5654 0.1447 0.2765 -0.3903 0.2646 0.2254 0.5064 -0.1009 -0.0260
[torch.DoubleTensor of size 1x10]
go -0.5896 0.1330 0.1361 -0.0193 -0.5612 0.3529 0.3683 0.0141 0.0447 -0.1963
[torch.DoubleTensor of size 1x10]
如何告诉 lua 不要 return 类型?像这样:
usually -0.2063 0.5654 0.1447 0.2765 -0.3903 0.2646 0.2254 0.5064 -0.1009 -0.0260
go -0.5896 0.1330 0.1361 -0.0193 -0.5612 0.3529 0.3683 0.0141 0.0447 -0.1963
抱歉,如果答案在那里,我没有搜索正确的关键字。我对 lua 的概念还是陌生的。
您可以编写自己的转储函数,例如:
local dump = function(vec)
vec = vec:view(vec:nElement())
local t = {}
for i=1,vec:nElement() do
t[#t+1] = string.format('%.4f', vec[i])
end
return table.concat(t, ' ')
end
并用它代替 tostring
。