【问题标题】:Why attempting "1 / torch.Tensor" triggers an error?为什么尝试“1 / torch.Tensor”会触发错误?
【发布时间】:2016-07-12 05:17:59
【问题描述】:

这很尴尬,但我似乎很难弄清楚如何在 Torch 中执行一个相当简单的除法操作。 这是我尝试做的:

th> a
 0.5058  0.2460  0.9038  0.6348
 0.6324  0.3435  0.2530  0.9692
 0.1531  0.1178  0.5588  0.5323
[torch.DoubleTensor of size 3x4]

                                                                      [0.0004s]
th> 1/a
bad argument #1 to '?' (torch.DoubleTensor expected, got boolean)
stack traceback:
    [C]: at 0x7ff5e513e9d0
    [C]: in function '__div'
    [string "_RESULT={1/a}"]:1: in main chunk
    [C]: in function 'xpcall'
    /home/tasty/torch/install/share/lua/5.1/trepl/init.lua:651: in function 'repl'
    ...asty/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:199: in main chunk
    [C]: at 0x00406670
                                                                      [0.0002s]
th>

numpy 中的 1/a 操作很简单,按预期工作,为什么在 Torch 中必须有所不同?

【问题讨论】:

    标签: lua torch


    【解决方案1】:

    好像不是这样实现的。 如果你不知道怎么做,试试这个:

    x = torch.Tensor(2, 2):fill(2)
    
    z = torch.div(x, 2) --will return a new Tensor with the result of x / 2.
    torch.div(z, x, 2) --will put the result of x / 2 in z.
    x:div(2) --will divide all elements of x with 2 in-place.
    z:div(x, 2) --puts the result of x / 2 in z.
    

    【讨论】:

      【解决方案2】:

      Torch 已经为此提供了 methods

      local z= torch.cinv(x) -- to make a copy
      x:cinv() -- for an in place element wise inverse
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-29
        • 1970-01-01
        • 2013-12-15
        • 2012-07-26
        • 2011-05-09
        • 2019-07-07
        相关资源
        最近更新 更多