pytorch数学操作


函数说明
torch.abs()计算张量中元素的绝对值
acos/arccos反余弦
acosh/arccosh反双曲余弦
angle计算复数张量中元素的角度(相位)
asin/arcsin计算张量中元素的反正弦
asinh/arcsinh计算张量中元素的反双曲正弦
atan/arctan计算张量中元素的反正切
atanh/arctanh张量中元素的反双曲正切
cos计算张量中元素的余弦值
cosh双曲余弦
exp/exp2指数以e(2.7)/以2
log张量中元素的自然对数(以e为底的对数)
log10计算张量中元素的以10为底的对数
lgamma计算张量中元素的对数伽玛函数
negative/neg张量元素中的负数
pow计算张量元素的幂运算
real提取复数张量中的实部
reciprocal计算张量元素的倒数
torch.remainder()计算两个张量元素之间的余数运算
torch.round()张量元素进行四舍五入操作
torch.rsqrt()计算张量元素的倒数的平方根
sin计算张量元素的正弦值
torch.sinh()算张量元素的双曲正弦值
torch.sqrt()计算张量元素的平方根
torch.square()计算张量元素的平方
tan计算张量元素的正切值
torch.tanh()计算张量元素的双曲正切值

1.torch.bitwise_not()

在 PyTorch 中,torch.bitwise_not() 是一个函数,用于执行逐元素的位非(bitwise NOT)操作。

torch.bitwise_not(input, out=None)
"""
input:输入张量。
out:可选参数,输出张量。
"""
import torch

x = torch.tensor([5, 2, 7], dtype=torch.uint8)

y = torch.bitwise_not(x)

print(y)
tensor([250, 253, 248], dtype=torch.uint8)

当我们使用 torch.bitwise_not() 函数时,它会对输入张量中的每个元素执行位非(bitwise NOT)操作。位非操作是对二进制表示的每一位进行取反的操作,即将 0 变为 1,将 1 变为 0。

例如,如果我们有一个输入张量 x 包含了整数值 [5, 2, 7],这些值的二进制表示分别是 [101, 010, 111]。使用 torch.bitwise_not() 函数对 x 进行位非操作,得到的结果张量 y 的元素将是对应位置上的二进制取反结果。

在示例中,输出张量 y 包含了 [250, 253, 248],这些值的二进制表示分别是 [11111010, 11111101, 11111000]。可以观察到,每个元素的二进制表示中的每一位都被取反。

需要注意的是,输入张量的数据类型对位非操作有影响。在示例中,我们使用了无符号8位整数 (torch.uint8) 的输入张量 x。位非操作会在每个元素的二进制表示中逐位取反,并且结果张量 y 的数据类型仍然是无符号8位整数 (torch.uint8)。

2.torch.bitwise_and()

在 PyTorch 中,torch.bitwise_and() 是一个函数,用于执行逐元素的位与(bitwise AND)操作。

torch.bitwise_and(input, other, out=None)
"""
input:第一个输入张量。
other:第二个输入张量。
out:可选参数,输出张量。
"""
import torch

x = torch.tensor([5, 3, 7], dtype=torch.uint8)
y = torch.tensor([3, 6, 5], dtype=torch.uint8)

z = torch.bitwise_and(x, y)

print(z)
tensor([1, 2, 5], dtype=torch.uint8)

在这个示例中,我们使用 torch.bitwise_and() 函数对张量 x 和 y 中的元素执行位与操作。输入张量 x 和 y 包含了无符号8位整数。torch.bitwise_and() 函数将 x 和 y 对应位置上的元素进行位与操作,得到了结果张量 z。

需要注意的是,位与操作将每个元素的二进制表示的对应位进行逻辑与操作,只有当对应位都为 1 时,结果位才为 1,否则为 0。输出张量 z 的数据类型与输入张量 x 和 y 相同。

3.torch.ceil()

在 PyTorch 中,torch.ceil() 函数用于执行逐元素的向上取整操作。它返回一个新的张量,其中的元素是输入张量中对应元素的向上取整结果。

torch.ceil(input, out=None)
"""
input:输入张量。
out:可选参数,输出张量。
"""
import torch

x = torch.tensor([1.2, 2.7, 3.5, 4.9])

y = torch.ceil(x)

print(y)
tensor([2., 3., 4., 5.])

3.torch.clamp()

在 PyTorch 中,torch.clamp() 函数用于对张量进行截断操作,将张量中的元素限制在指定范围内。它返回一个新的张量,其中的元素被限制在给定的范围内。

torch.clamp(input, min, max, out=None)
"""
input:输入张量。
min:指定的最小值,小于该值的元素会被替换为该值。
max:指定的最大值,大于该值的元素会被替换为该值。
out:可选参数,输出张量。
"""
返回值是一个新的张量,其元素被截断在 [min, max] 的范围内。
import torch

x = torch.tensor([1.2, -0.5, 3.7, 2.8])

y = torch.clamp(x, min=0, max=2)

print(y)
tensor([1.2000, 0.0000, 2.0000, 2.0000])

4.torch.torch.floor()

在 PyTorch 中,torch.floor() 函数用于执行逐元素的向下取整操作。它返回一个新的张量,其中的元素是输入张量中对应元素的向下取整结果。

torch.floor(input, out=None)
"""
input:输入张量。
out:可选参数,输出张量。
"""
import torch

x = torch.tensor([1.2, 2.7, 3.5, 4.9])

y = torch.floor(x)

print(y)
tensor([1., 2., 3., 4.])

5.torch.add()

torch.add()是PyTorch中的一个函数,用于执行元素级的张量相加操作。它可以用于执行张量之间的加法操作,也可以用于执行张量和标量之间的加法操作。

torch.add(input, other, out=None)
"""
input:输入张量,即第一个操作数。
other:要与输入张量进行相加的张量,即第二个操作数。
out(可选):输出张量,用于存储结果。如果指定了该参数,则结果将存储在该张量中。
"""
import torch

x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])

result = torch.add(x, y)

print(result)# tensor([5, 7, 9])
import torch

x = torch.tensor([1, 2, 3])
alpha = 2
y = torch.tensor([4, 5, 6])

result = torch.add(x, y, alpha=alpha)

print(result)# tensor([9, 12, 15])
import torch

x = torch.tensor([1, 2, 3])
scalar = 5

result = torch.add(x, scalar)

print(result)

6.torch.div()

torch.div()是PyTorch中的一个函数,用于执行元素级的除法操作。它接受两个张量作为输入,并返回一个张量,其中每个元素是对应位置上第一个输入张量元素除以第二个输入张量元素的结果。

torch.div(input, other, out=None)
"""
input:输入张量,即第一个操作数。
other:除数张量,即第二个操作数。
out(可选):输出张量,用于存储结果。如果指定了该参数,则结果将存储在该张量中。
"""
import torch

x = torch.tensor([4.0, 6.0, 8.0])
y = torch.tensor([2.0, 3.0, 4.0])

result = torch.div(x, y)

print(result)# tensor([2., 2., 2.])

2.如果要执行矩阵除法,即矩阵之间的除法操作,可以使用torch.matmul()函数或使用torch.div()函数结合逆矩阵操作来实现。

import torch

# 定义矩阵
A = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
B = torch.tensor([[2.0, 0.0], [1.0, 2.0]])

# 使用 torch.matmul() 执行矩阵除法
result = torch.matmul(A, torch.inverse(B))

print(result)
# tensor([[ 0.5000, -1.0000],
#        [ 0.5000,  1.0000]])

在上面的示例中,我们定义了两个矩阵A和B。然后,使用torch.matmul()函数将A与B的逆矩阵相乘,从而实现矩阵除法操作。结果存储在result中,并打印出来。

import torch

# 定义矩阵
A = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
B = torch.tensor([[2.0, 0.0], [1.0, 2.0]])

# 使用 torch.div() 结合逆矩阵操作执行矩阵除法
result = torch.div(A, torch.inverse(B))

print(result)

在上面的示例中,我们使用torch.div()函数将矩阵A与矩阵B的逆矩阵相除,从而实现矩阵除法操作。结果存储在result中,并打印出来。

7.torch.addcmul()

torch.addcmul()是PyTorch中的一个函数,用于执行按元素乘法和加法的组合操作。它接受三个张量作为输入,并返回一个张量,其中每个元素是按照以下公式计算得出的:

out = tensor1 + value* tensor2 * tensor3
torch.addcmul(input, tensor1, tensor2, *, value=1, out=None) → Tensor
"""
tensor1:第一个输入张量。
scalar:标量乘法因子。
tensor2:第二个输入张量,用于按元素乘法。
tensor3:第三个输入张量,用于按元素乘法。
out(可选):输出张量,用于存储结果。如果指定了该参数,则结果将存储在该张量中。
"""
import torch

x = torch.tensor([1.0, 2.0, 3.0])
y = torch.tensor([4.0, 5.0, 6.0])
z = torch.tensor([2.0, 3.0, 4.0])

result = torch.addcmul(x, y, z, value=0.5)

print(result) # tensor([ 5.0000,  9.5000, 15.0000])

8.torch.lerp()

torch.lerp()函数在许多数值计算和图形处理任务中都很有用,例如在颜色插值、动画过渡等方面。torch.lerp()是PyTorch中的一个函数,用于执行线性插值操作。它接受三个张量作为输入,并返回一个张量,其中每个元素是按照以下公式计算得出的:

out = start + weight * (end - start)
torch.lerp(start, end, weight, out=None)
"""
start:起始张量。
end:结束张量。
weight:权重张量。
out(可选):输出张量,用于存储结果。如果指定了该参数,则结果将存储在该张量中。
"""
import torch

start = torch.tensor([1.0, 2.0, 3.0])
end = torch.tensor([4.0, 5.0, 6.0])
weight = torch.tensor(0.5)

result = torch.lerp(start, end, weight)

print(result)# tensor([2.5000, 3.5000, 4.5000])

9.torch.frac()

torch.frac()是PyTorch中的一个函数,用于计算张量中每个元素的小数部分。它返回一个张量,其中每个元素表示对应输入张量元素的小数部分。

torch.frac(input, out=None)
"""
input:输入张量,表示要计算小数部分的张量。
out(可选):输出张量,用于存储结果。如果指定了该参数,则结果将存储在该张量中。
"""
import torch

x = torch.tensor([1.5, -2.7, 3.2, -4.8])

result = torch.frac(x)

print(result)
tensor([ 0.5000, -0.7000,  0.2000, -0.8000])

10.torch.trunc()

torch.trunc()是PyTorch中的一个函数,用于计算张量中每个元素的截断整数部分。它返回一个张量,其中每个元素表示对应输入张量元素的截断整数部分。

torch.trunc(input, out=None)
"""
input:输入张量,表示要计算截断整数部分的张量。
out(可选):输出张量,用于存储结果。如果指定了该参数,则结果将存储在该张量中。
"""
import torch

x = torch.tensor([1.5, -2.7, 3.2, -4.8])

result = torch.trunc(x)

print(result)
tensor([ 1., -2.,  3., -4.])

11.torch.fmod()

torch.fmod()是PyTorch中的一个函数,用于计算两个张量按元素取模的余数。它返回一个张量,其中每个元素表示对应输入张量元素的余数。

torch.fmod(input, divisor, out=None)
"""
input:输入张量,表示被除数。
divisor:除数张量,表示除数。
out(可选):输出张量,用于存储结果。如果指定了该参数,则结果将存储在该张量中。
"""
import torch

x = torch.tensor([7, -5, 12, -10])
y = torch.tensor([3, 2, 5, 3])

result = torch.fmod(x, y)

print(result)
tensor([ 1, -1,  2, -1])

12.torch.float_power()

torch.float_power()是PyTorch中的一个函数,用于计算输入张量的每个元素的指定次幂。它返回一个张量,其中每个元素表示对应输入张量元素的指定次幂。

torch.float_power(input, exponent, out=None)
"""
input:输入张量,表示要计算指定次幂的张量。
exponent:指数张量,表示要计算的指数。
out(可选):输出张量,用于存储结果。如果指定了该参数,则结果将存储在该张量中。
"""
import torch

x = torch.tensor([2.0, 3.0, 4.0])
exponent = torch.tensor([2.0, 0.5, 3.0])

result = torch.float_power(x, exponent)

print(result)
tensor([ 4.0000,  1.7321, 64.0000], dtype=torch.float64)

13.torch.hypot()

torch.hypot()是PyTorch中的一个函数,用于计算两个张量按元素的直角三角形斜边的长度。它返回一个张量,其中每个元素表示对应输入张量元素的直角三角形斜边的长度。

torch.hypot(input1, input2, out=None)
"""
input1:第一个输入张量,表示直角三角形的一个直角边。
input2:第二个输入张量,表示直角三角形的另一个直角边。
out(可选):输出张量,用于存储结果。如果指定了该参数,则结果将存储在该张量中。
"""
import torch

x = torch.tensor([3.0, 4.0, 5.0])
y = torch.tensor([4.0, 3.0, 12.0])

result = torch.hypot(x, y)

print(result)
tensor([ 5.,  5., 13.])

14.torch.multiply()

torch.mul()函数执行了输入张量x和y的元素级乘法操作。

torch.mul(input, other, out=None)
"""
input:输入张量,表示要进行乘法操作的张量。
other:另一个张量,表示与输入张量进行元素级乘法的张量。
out(可选):输出张量,用于存储结果。如果指定了该参数,则结果将存储在该张量中。
"""
import torch

x = torch.tensor([2, 3, 4])
y = torch.tensor([1, 2, 3])

result = torch.mul(x, y)

print(result)
tensor([ 2,  6, 12])

15.torch.sub()

torch.sub()是PyTorch中的一个函数,用于执行元素级的减法操作。它可以用于计算两个张量之间的元素差异。

torch.sub(input, other, out=None)
"""
input:输入张量,表示被减数。
other:另一个张量,表示减数。
out(可选):输出张量,用于存储结果。如果指定了该参数,则结果将存储在该张量中。
"""
import torch

x = torch.tensor([4, 5, 6])
y = torch.tensor([1, 2, 3])

result = torch.sub(x, y)

print(result)
tensor([3, 3, 3])

矩阵减法针对两个形状相同的矩阵进行减法运算,对应位置的元素相减得到结果矩阵。
逐元素减法可以处理形状不同但广播兼容的张量,对应位置的元素相减得到结果张量。

16.torch.matmul()

torch.matmul() 函数执行矩阵乘法操作。它可以用于执行两个张量之间的矩阵乘法,或者执行矩阵和向量之间的乘法。

torch.matmul(input, other, out=None)
"""
input: 输入张量,表示左侧矩阵或向量。
other: 另一个张量,表示右侧矩阵或向量。
out(可选): 输出张量,用于存储结果。如果指定了该参数,则结果将存储在该张量中。
"""
import torch

# 两个矩阵相乘
x = torch.tensor([[1, 2], [3, 4]])
y = torch.tensor([[5, 6], [7, 8]])

result = torch.matmul(x, y)

print(result)
tensor([[19, 22],
        [43, 50]])

17.torch.inverse()

torch.inverse()是PyTorch中的一个函数,用于计算矩阵的逆矩阵。它接受一个输入张量作为参数,并返回该输入张量的逆矩阵。

torch.inverse(input, out=None)
"""
input:输入张量,即要计算逆矩阵的矩阵。
out(可选):输出张量,用于存储结果。如果指定了该参数,则逆矩阵将存储在该张量中。
"""
import torch

A = torch.tensor([[1.0, 2.0], [3.0, 4.0]])

A_inv = torch.inverse(A)

print(A_inv)# tensor([[-2.0000,  1.0000],
        			#[ 1.5000, -0.5000]])
  • 13
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值