Pytorch 数学运算

Pytorch 数学运算

basic:add sub div matmul
import torch
a = torch.rand(3, 4)
b = torch.rand(4)

print("a+b:\t", a+b)

c = torch.add(a, b)
print("c:\t", c)
# a-b == sub(a, b)
d = torch.all(torch.eq(a-b, torch.sub(a, b)))
print("d:\t", d)
# a/b  == div(a, b)
e = torch.all(torch.eq(a/b, torch.div(a, b)))
print("e:\t", e)
a+b:	 tensor([[1.0492, 1.2809, 1.5111, 0.8383],
        [1.4602, 1.6011, 1.7691, 1.4479],
        [1.1072, 1.5970, 0.9700, 1.3284]])
c:	 tensor([[1.0492, 1.2809, 1.5111, 0.8383],
        [1.4602, 1.6011, 1.7691, 1.4479],
        [1.1072, 1.5970, 0.9700, 1.3284]])
d:	 tensor(True)
e:	 tensor(True)
import torch
# 需要将列表中数据转换为float型
a = torch.tensor([[3., 3.],[3., 3.]])
b = torch.ones(2, 2)
print("b:\t", b)
# torch.mm : only for 2d
c = torch.mm(a, b)
print("c:\t", c)
# torch.matmul
d = torch.matmul(a, b)
print("d:\t", d)

print("a@b:\t", a@b)
b:	 tensor([[1., 1.],
        [1., 1.]])
c:	 tensor([[6., 6.],
        [6., 6.]])
d:	 tensor([[6., 6.],
        [6., 6.]])
a@b:	 tensor([[6., 6.],
        [6., 6.]])
import torch
# 将784 降维至 512
a = torch.rand(4, 784)
x = torch.rand(4, 784)

w = torch.rand(512, 784)
c = (x@w.t()).shape
print("c:\t", c)
c:	 torch.Size([4, 512])
>2d tensor matmul
import torch
a = torch.rand(4, 3, 28, 64)
b = torch.rand(4, 3, 64, 32)
# torch.mm()是不能用于>2维的
"""
c = torch.mm(a, b).shape
print("C:\t", c)
"""
d =torch.matmul(a, b).shape
print("d:\t", d)

e = torch.rand(4, 1, 64, 32)
# 矩阵相乘
f = torch.matmul(a, e).shape
print("e:\t", f)
d:	 torch.Size([4, 3, 28, 32])
e:	 torch.Size([4, 3, 28, 32])
Power:
import torch
a = torch.full([2, 2], 3)
print("a.pow(2):\t", a.pow(2))
# 平方
b = a**2
print("b:\t", b)
# 开方
c = b.sqrt()
print("c:\t", c)
# 倒数
d = b.rsqrt()
print("d:\t", d)
# 开根号
e = b**(0.5)
print("e:\t", e)
a.pow(2):	 tensor([[9., 9.],
        [9., 9.]])
b:	 tensor([[9., 9.],
        [9., 9.]])
c:	 tensor([[3., 3.],
        [3., 3.]])
d:	 tensor([[0.3333, 0.3333],
        [0.3333, 0.3333]])
e:	 tensor([[3., 3.],
        [3., 3.]])
Exp log:
import torch
a = torch.exp(torch.ones(2, 2))
print("a:\t", a)

b = torch.log(a)
print("b:\t", b)
Approximation:
import torch
a = torch.tensor(3.14)
# 往下走
print("a.floor():\t", a.floor())
# 往上走
print("a.ceil():\t", a.ceil())
# 裁出整数部分
print("a.trunc():\t", a.trunc())
# 裁出小数部分
print("a.frac():\t", a.frac())

b = torch.tensor(3.499)
# 四舍五入的方法
print("b.round():\t", b.round())

c = torch.tensor(3.5)
# 四舍五入的方法
print("c.round():\t", c.round())
a.floor():	 tensor(3.)
a.ceil():	 tensor(4.)
a.trunc():	 tensor(3.)
a.frac():	 tensor(0.1400)
b.round():	 tensor(3.)
c.round():	 tensor(4.)
clamp:
import torch
grad = torch.rand(2,3)*15
print("grad:\t", grad)
a = grad.max()
print("a:\t", a)

b = grad.median()
print("b:\t", b)
# clamp(min)
c = grad.clamp(10)
print("c:\t", c)
# clamp(min, max)
d = grad.clamp(0, 10)
print("d:\t", d)
a:	 tensor(13.3701)
b:	 tensor(4.5926)
c:	 tensor([[10.0000, 13.3701, 10.0000],
        [12.8315, 10.0000, 10.0000]])
d:	 tensor([[ 4.0652, 10.0000,  6.8409],
        [10.0000,  4.5926,  2.1485]])
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值