小白学Pytorch系列-- Torch API (5)
创始人
2025-05-29 01:36:04

小白学Pytorch系列-- Torch API (5)

Math operations

Pointwise Ops

TORCH.ABS

计算输入中每​​个元素的绝对值。

>>> torch.abs(torch.tensor([-1, -2, 3]))
tensor([ 1,  2,  3])

TORCH.ABSOLUTE

torch.abs() 的别名

TORCH.ACOS

计算输入中每个元素的逆余弦。

a = torch.randn(4)
a
torch.acos(a)

TORCH.ARCCOS

torch.acos()的别名。

TORCH.ACOSH

返回具有输入元素的反双曲余弦值的新张量。

TORCH.ARCCOSH

Torch.acosh() 的别名。

TORCH.ADD

将按 alpha 缩放的other添加到input。
![]](https://img-blog.csdnimg.cn/992cd8fd81ba4b209a07df9c0a3360a0.png)

>>> a = torch.randn(4)
>>> a
tensor([ 0.0202,  1.0985,  1.3506, -0.6056])
>>> torch.add(a, 20)
tensor([ 20.0202,  21.0985,  21.3506,  19.3944])>>> b = torch.randn(4)
>>> b
tensor([-0.9732, -0.3497,  0.6245,  0.4022])
>>> c = torch.randn(4, 1)
>>> c
tensor([[ 0.3743],[-1.7724],[-0.5811],[-0.8017]])
>>> torch.add(b, c, alpha=10)
tensor([[  2.7695,   3.3930,   4.3672,   4.1450],[-18.6971, -18.0736, -17.0994, -17.3216],[ -6.7845,  -6.1610,  -5.1868,  -5.4090],[ -8.9902,  -8.3667,  -7.3925,  -7.6147]])

TORCH.ADDCDIV

执行 tensor1 除以 tensor2 的逐元素除法,将结果乘以标量值并将其添加到input。

t = torch.randn(1, 3)
t1 = torch.randn(3, 1)
t2 = torch.randn(1, 3)
torch.addcdiv(t, t1, t2, value=0.1)

TORCH.ADDCMUL

执行 tensor1 与 tensor2 的逐元素乘法,将结果乘以标量值并将其添加到input。

t = torch.randn(1, 3)
t1 = torch.randn(3, 1)
t2 = torch.randn(1, 3)
torch.addcmul(t, t1, t2, value=0.1)

TORCH.ANGLE

计算给定输入张量的元素角度(以弧度为单位)。

torch.angle(torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j]))*180/3.14159

TORCH.ASIN

返回一个新的张量与输入元素的反正弦值。

a = torch.randn(4)
a
torch.asin(a)

TORCH.ARCSIN

torch.asin() 的别名。

TORCH.ASINH

返回具有输入元素的反双曲正弦值的新张量。

a = torch.randn(4)
a
torch.asinh(a)

TORCH.ARCSINH

torch.asinh()的别名

TORCH.ATAN

返回一个新的张量与输入元素的反正切值。

a = torch.randn(4)
a
torch.atan(a)

TORCH.ARCTAN

torch.atan().别名

TORCH.ATANH

返回一个新的张量,该张量具有输入元素的反双曲正切。

>>> a = torch.randn(4).uniform_(-1, 1)
>>> a
tensor([ -0.9385, 0.2968, -0.8591, -0.1871 ])
>>> torch.atanh(a)
tensor([ -1.7253, 0.3060, -1.2899, -0.1893 ])

TORCH.ARCTANH

torch.atanh().别名

TORCH.ATAN2

考虑象限的input/other元素的反正切。 返回一个新的张量,在向量(other,input)和向量(1,0)之间以弧度表示有符号的角度(注意第二个参数 other 是 x 坐标,而第一个参数 input 是 y-坐标。)

>>> a = torch.randn(4)
>>> a
tensor([ 0.9041,  0.0196, -0.3108, -2.4423])
>>> torch.atan2(a, torch.randn(4))
tensor([ 0.9833,  0.0811, -1.9743, -1.4151])

TORCH.ARCTAN2

torch.atan2() 别名。

TORCH.BITWISE_NOT

计算给定输入张量的按位非。 输入张量必须是整数或布尔类型。 对于 bool 张量,它计算逻辑 NOT。

>>> torch.bitwise_not(torch.tensor([-1, -2, 3], dtype=torch.int8))
tensor([ 0,  1, -4], dtype=torch.int8)

TORCH.BITWISE_AND

计算输入和其他的按位与。 输入张量必须是整数或布尔类型。 对于布尔张量,它计算逻辑与。

>>> torch.bitwise_and(torch.tensor([-1, -2, 3], dtype=torch.int8), torch.tensor([1, 0, 3], dtype=torch.int8))
tensor([1, 0,  3], dtype=torch.int8)
>>> torch.bitwise_and(torch.tensor([True, True, False]), torch.tensor([False, True, False]))
tensor([ False, True, False])

TORCH.BITWISE_OR

计算输入和其他的按位或。 输入张量必须是整数或布尔类型。 对于布尔张量,它计算逻辑或。

>>> torch.bitwise_or(torch.tensor([-1, -2, 3], dtype=torch.int8), torch.tensor([1, 0, 3], dtype=torch.int8))
tensor([-1, -2,  3], dtype=torch.int8)
>>> torch.bitwise_or(torch.tensor([True, True, False]), torch.tensor([False, True, False]))
tensor([ True, True, False])

TORCH.BITWISE_XOR

计算输入和其他的按位异或。 输入张量必须是整数或布尔类型。 对于 bool 张量,它计算逻辑 XOR。

>>> torch.bitwise_xor(torch.tensor([-1, -2, 3], dtype=torch.int8), torch.tensor([1, 0, 3], dtype=torch.int8))
tensor([-2, -2,  0], dtype=torch.int8)
>>> torch.bitwise_xor(torch.tensor([True, True, False]), torch.tensor([False, True, False]))
tensor([ True, False, False])

TORCH.BITWISE_LEFT_SHIFT

计算输入的其他位的左算术移位。 输入张量必须是整数类型。 此运营商支持广播到一个共同的形状和类型推广。

>>> torch.bitwise_left_shift(torch.tensor([-1, -2, 3], dtype=torch.int8), torch.tensor([1, 0, 3], dtype=torch.int8))
tensor([-2, -2, 24], dtype=torch.int8)

TORCH.BITWISE_RIGHT_SHIFT

计算其他位输入的算术右移。输入张量必须是积分类型。该运营商支持以普通形状和类型进行广播推广。

>>> torch.bitwise_right_shift(torch.tensor([-2, -7, 31], dtype=torch.int8), torch.tensor([1, 0, 3], dtype=torch.int8))
tensor([-1, -7,  3], dtype=torch.int8)

TORCH.CEIL

返回一个新的张量,其中包含输入元素的 ceil,即大于或等于每个元素的最小整数。

对于整数输入,遵循返回输入张量副本的 array-api 约定。

>>> a = torch.randn(4)
>>> a
tensor([-0.6341, -1.4208, -1.0900,  0.5826])
>>> torch.ceil(a)
tensor([-0., -1., -1.,  1.])

TORCH.CLAMP

将 input 中的所有元素限制在 [ min, max ] 范围内。 令 min_value 和 max_value 分别为最小值和最大值,返回:

>>> a = torch.randn(4)
>>> a
tensor([-1.7120,  0.1734, -0.0478, -0.0922])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000,  0.1734, -0.0478, -0.0922])>>> min = torch.linspace(-1, 1, steps=4)
>>> torch.clamp(a, min=min)
tensor([-1.0000,  0.1734,  0.3333,  1.0000])

TORCH.CLIP

torch.clamp() 的别名。

TORCH.CONJ_PHYSICAL

计算给定输入张量的逐元素共轭。 如果输入有一个非复杂数据类型,这个函数只返回输入。

>>> torch.conj_physical(torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j]))
tensor([-1 - 1j, -2 - 2j, 3 + 3j])

TORCH.COPYSIGN

创建一个新的浮点张量,具有输入的大小和其他元素的符号。

>>> a = torch.randn(5)
>>> a
tensor([-1.2557, -0.0026, -0.5387,  0.4740, -0.9244])
>>> torch.copysign(a, 1)
tensor([1.2557, 0.0026, 0.5387, 0.4740, 0.9244])
>>> a = torch.randn(4, 4)
>>> a
tensor([[ 0.7079,  0.2778, -1.0249,  0.5719],[-0.0059, -0.2600, -0.4475, -1.3948],[ 0.3667, -0.9567, -2.5757, -0.1751],[ 0.2046, -0.0742,  0.2998, -0.1054]])
>>> b = torch.randn(4)
tensor([ 0.2373,  0.3120,  0.3190, -1.1128])
>>> torch.copysign(a, b)
tensor([[ 0.7079,  0.2778,  1.0249, -0.5719],[ 0.0059,  0.2600,  0.4475, -1.3948],[ 0.3667,  0.9567,  2.5757, -0.1751],[ 0.2046,  0.0742,  0.2998, -0.1054]])
>>> a = torch.tensor([1.])
>>> b = torch.tensor([-0.])
>>> torch.copysign(a, b)
tensor([-1.])

TORCH.COS

返回一个新的张量与输入元素的余弦值。

>>> a = torch.randn(4)
>>> a
tensor([ 1.4309,  1.2706, -0.8562,  0.9796])
>>> torch.cos(a)
tensor([ 0.1395,  0.2957,  0.6553,  0.5574])

TORCH.COSH

返回具有输入元素的双曲余弦值的新张量。

>>> a = torch.randn(4)
>>> a
tensor([ 0.1632,  1.1835, -0.6979, -0.7325])
>>> torch.cosh(a)
tensor([ 1.0133,  1.7860,  1.2536,  1.2805])

TORCH.DEG2RAD

返回一个新的张量,其中输入的每个元素都从以度为单位的角度转换为弧度。

>>> a = torch.tensor([[180.0, -180.0], [360.0, -360.0], [90.0, -90.0]])
>>> torch.deg2rad(a)
tensor([[ 3.1416, -3.1416],[ 6.2832, -6.2832],[ 1.5708, -1.5708]])

TORCH.DIV

将输入 input 的每个元素除以 other 的对应元素。

>>> x = torch.tensor([ 0.3810,  1.2774, -0.2972, -0.3719,  0.4637])
>>> torch.div(x, 0.5)
tensor([ 0.7620,  2.5548, -0.5944, -0.7438,  0.9274])>>> a = torch.tensor([[-0.3711, -1.9353, -0.4605, -0.2917],
...                   [ 0.1815, -1.0111,  0.9805, -1.5923],
...                   [ 0.1062,  1.4581,  0.7759, -1.2344],
...                   [-0.1830, -0.0313,  1.1908, -1.4757]])
>>> b = torch.tensor([ 0.8032,  0.2930, -0.8113, -0.2308])
>>> torch.div(a, b)
tensor([[-0.4620, -6.6051,  0.5676,  1.2639],[ 0.2260, -3.4509, -1.2086,  6.8990],[ 0.1322,  4.9764, -0.9564,  5.3484],[-0.2278, -0.1068, -1.4678,  6.3938]])>>> torch.div(a, b, rounding_mode='trunc')
tensor([[-0., -6.,  0.,  1.],[ 0., -3., -1.,  6.],[ 0.,  4., -0.,  5.],[-0., -0., -1.,  6.]])>>> torch.div(a, b, rounding_mode='floor')
tensor([[-1., -7.,  0.,  1.],[ 0., -4., -2.,  6.],[ 0.,  4., -1.,  5.],[-1., -1., -2.,  6.]])

TORCH.DIVIDE

torch.div() 的别名

TORCH.DIGAMMA

Torch.special.digamma()的别名。

TORCH.ERF

torch.special.erf()的别名。

TORCH.ERFC

Torch.special.erfc() 的别名。

TORCH.ERFINV

torch.special.erfinv()的别名。

Reduction Ops

Comparison Ops

Spectral Ops

Other Operations

BLAS and LAPACK Operations

相关内容

热门资讯

百亿身家富豪俞发祥被采取刑事强... 红星资本局12月23日消息,12月22日晚间,祥源文旅(600586.SH)、交建股份(603815...
百亿富豪俞发祥,被警方采取刑事... 2025.12.23“祥源系”实控人俞发祥,已被警方采取刑事强制措施。12月22日晚,祥源文旅、交建...
“火腿大王”二代接班,豪赌芯片... 从“火腿大王”到“牛散岳父”,再到“莆田卖车富豪”,这家“火腿第一股”的实控人座位,烫得没人能坐久。...
真相要浮出水面?南博前院长徐湖... 实名举报!80岁的南京博物院原院长徐湖平估计要晚节不保了。南博事件又有了炸裂剧情,一名自叫郭礼典的网...