Pytorch第二课:package-torch(2) 之数学操作

本文详细介绍了PyTorch中涉及的数学操作,包括逐点计算、缩减、比较和其他操作,以及重点讲解了线性代数相关功能,帮助读者深入理解PyTorch在深度学习中的数值计算能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

微博:https://weibo.com/wangxiaocaoai/profile?rightmod=1&wvr=6&mod=personinfo
微信公众号:搜索"AI躁动街"


本节要点:

1 逐点计算操作

2 缩减操作

3 比较操作

4 其他操作

5 线性代数操作

1 逐点计算操作

# 导入包
import torch
# 1.计算绝对值
a = torch.FloatTensor([-1, -10])
a_abs = torch.abs(a)
print(a_abs)
tensor([  1.,  10.])
# 2.计算余弦
a = torch.randn(4)
a_cos = torch.cos(a)
print(a_cos)
tensor([ 0.3623,  0.0784,  0.9808,  0.8221])
# 3.计算双曲余弦
a = torch.randn(4)
a_cosh = torch.cosh(a)
print(a_cosh)
tensor([ 2.2139,  1.0241,  1.0118,  1.2354])
# 4.计算反余弦
a = torch.randn(4)
a_acos = torch.acos(a)
print(a_acos)
tensor([ 2.1772,  0.9836,  2.2749,  0.5994])
# 5.计算正弦
a = torch.randn(4)
a_sinh = torch.sinh(a)
print(a_sinh)
tensor([-0.9122,  0.2392,  1.2656,  0.4663])
# 6.计算双曲正弦
a = torch.randn(4)
a_sin = torch.sin(a)
print(a_sin)
# 7.计算反正弦
a = torch.randn(4)
a_asin = torch.asin(a)
print(a_asin)
tensor([-0.9362,     nan, -1.1308,  0.0407])
# 8.计算正切
a = torch.randn(4)
tan = torch.tan(a)
print(tan)
tensor([  0.0061,  -0.9945, -17.3586,  -0.3077])
# 9.计算双曲正切
a = torch.randn(4)
tanh = torch.tanh(a)
print(tanh)
tensor([ 0.9856, -0.8689, -0.6669,  0.4711])
# 10.计算一个张量的反正切
a = torch.randn(4)
atan = torch.atan(a)
print(atan)
tensor([ 0.3666,  1.0404, -0.5340,  0.0825])
# 11.计算两个张量的反正切
a = torch.randn(4)
b = torch.randn(4)
atan2 = torch.atan2(a, b)
print(atan2)
tensor([ 0.7203, -0.7093, -2.4139, -0.9148])
# 12.加法
# 直接加法 a+b
a = torch.randn(4)
b = 20
add = torch.add(a, b)
print(add)

# a + c * b
c = torch.rand(4)
add2 = torch.add(a, b, c)
print(add2)
tensor([ 20.6347,  19.7540,  21.7134,  20.2289])
tensor([ 15.7686,   8.5032,   4.6279,   5.1963])
# 13.先除后加:t + 0.5*(t1 / t2)
t = torch.randn(2, 3)
t1 = torch.randn(2, 3)
t2 = torch.randn(2, 3)
addcdiv = torch.addcdiv(t, 0.5, t1, t2)
print(addcdiv)  
tensor([[-0.9947, -1.5404, -0.5799],
        [-1.5395,  3.4531,  1.6741]])
# 14.先乘后加:t + 0.5*(t1 * t2)
addcmul = torch.addcmul(t, 0.5, t1, t2)
print(addcmul)
tensor([[-0.9904, -1.3817, -0.3505],
        [-1.2655,  3.0514,  1.3952]])
# 15.乘法计算
# 张量与标量相乘
a = torch.randn(2, 3)
a_mul = torch.mul(a, 5)
print('a:', a)
print('a_mul:', a_mul)

# 张量与张量相乘,对应位置的元素相乘
b = torch.randn(2, 3)
a_b_mul = torch.mul(a, b)
print('b:', b)
print('a_b_mul:', a_b_mul)
a: tensor([[ 0.0863,  1.7408,  0.8538],
        [ 0.8702,  0.1472,  0.2192]])
a_mul: tensor([[ 0.4315,  8.7038,  4.2688],
        [ 4.3508,  0.7359,  1.0962]])
b: tensor([[ 0.5903, -0.6919, -0.4070],
        [-0.3127,  0.1756,  0.9016]])
a_b_mul: tensor([[ 0.0510, -1.2044, -0.3474],
        [-0.2721,  0.0258,  0.1977]])
# 16.除法计算
# 张量除标量 
a = torch.randn(1,4)
a_div = torch.div(a, 2)  # a / 2
print(a_div)
tensor([[ 0.4224,  0.7171, -0.4719, -0.1562]])
# 17.张量除张量
a = torch.randn(1, 4)
b = torch.randn(1, 4)
div = torch.div(a, b)
print(div)
tensor([[ 2.2698,  0.7206,  1.0432,  0.2880]])
# 18.计算除法余数
a = torch.tensor([-1, 2, 3, 4])
a_fmod = torch.fmod(a, 2) # a%2
print(a_fmod)
tensor([-1,  0,  1,  0])
# 19.计算除法余数,余数与除数有相同的符号。
a = torch.tensor([-1, 2, 3, 4])
a_re = torch.remainder(a, 2) # a%2
print(a_re)
tensor([ 1,  0,  1,  0])
# 20.指数计算
a = torch.randn(1, 4)
a_exp = torch.exp(a)
print(a_exp)
tensor([[ 0.1983,  3.7585,  1.6955,  3.2236]])
# 21.自然对数计算
a = torch.randn(1, 4)
a_log = torch.log(a)
print(a_log)
tensor([[-0.7030,     nan,     nan, -5.1565]])
# 22.幂值计算
# 幂值为标量时
a = torch.Tensor([1, 2, 3, 4])
a_pow = torch.pow(a, 3)
print(a)
print(a_pow)

# 幂值为张量时
exp = torch.Tensor([1, 2, 3, 4])
a_pow = torch.pow(a, exp)
print(a_pow)
tensor([ 1.,  2.,  3.,  4.])
tensor([  1.,   8.,  27.,  64.])
tensor([   1.,    4.,   27.,  256.])
# 23.计算平方根的
a = torch.Tensor([1, 2, 3, 4])
a_sqrt = torch.sqrt(a)
print(a_sqrt)
te
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值