gpt4 book ai didi

python - torch.positive 的目的是什么?

转载 作者:行者123 更新时间:2023-12-04 14:05:55 24 4
gpt4 key购买 nike

来自documentation :

torch.positive(input)Tensor

Returns input. Throws a runtime error if input is a bool tensor.

如果它是 bool 张量,它只返回输入并抛出错误,但这不是检查张量是否为 bool 的有效方法,也不是可读的方法。

最佳答案

好像pytorch添加了pytorch.positive in parity with numpy它有一个 function of the same name .

回到你的问题,positive 是一个一元运算符,它基本上将所有内容乘以 +1。这不是一个特别有用的操作,但与 negative 对称,它将所有内容乘以 -1

>>> import numpy as np
>>> a = np.arange(10)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.positive(a)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.negative(a)
array([ 0, -1, -2, -3, -4, -5, -6, -7, -8, -9])

那么,为什么要制作一个有效地返回您传递的数组(的副本)的 positive 函数呢?它可用于编写通用代码,例如numpy.positivenumpy.negative 可以根据某些条件动态使用。

关于python - torch.positive 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68532510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com