【问题标题】:Torch tensor set the negative numbers to zeroTorch 张量将负数设置为零
【发布时间】:2017-05-27 07:09:47
【问题描述】:

x=torch.Tensor({1,-1,3,-8})

如何转换x,以便在不使用循环的情况下将 x 中的所有负值替换为零,从而使张量看起来像

th>x 1 0 3 0

【问题讨论】:

标签: lua torch


【解决方案1】:

其实这个操作相当于应用ReLU非线性激活。

只要这样做,你就可以开始了

output = torch.nn.functional.relu(a)

您也可以就地执行以加快计算速度:

torch.nn.functional.relu(a, inplace=True)

【讨论】:

    【解决方案2】:

    Pytorch 支持运算符索引

    a = torch.Tensor([1,0,-1]) a[a < 0] = 0 a tensor([1., 0., 0.])

    【讨论】:

      【解决方案3】:

      Pytorch 在这里负责广播:

      x = torch.max(x,torch.tensor([0.]))
      

      【讨论】:

        猜你喜欢
        • 2018-02-13
        • 1970-01-01
        • 1970-01-01
        • 2017-09-11
        • 2021-02-02
        • 2017-07-21
        • 2021-01-14
        • 2020-12-12
        • 2020-02-11
        相关资源
        最近更新 更多