【问题标题】:In lua/torch, write tensor out without printing type在 lua/torch 中,写出张量而不打印类型
【发布时间】:2016-07-15 15:51:35
【问题描述】:

我是 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 不要返回类型?像这样:

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 的概念还是很陌生。

【问题讨论】:

    标签: lua torch


    【解决方案1】:

    您可以编写自己的转储函数,例如:

    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

    【讨论】:

      猜你喜欢
      • 2016-03-17
      • 2016-03-15
      • 2017-12-20
      • 2016-10-06
      • 2018-01-05
      • 2019-09-03
      • 2016-06-09
      • 2016-10-27
      • 2023-04-02
      相关资源
      最近更新 更多