pytorch入门--矩阵基本运算

其他相关操作:https://blog.csdn.net/qq_43923588/article/details/108007534

本篇pytorch的矩阵基本运算进行展示,包含:

  • add/subtract/multiply/divide
  • matmul
  • pow 次方
  • sqrt/rsqrt 次方根
  • 近似运算
  • 上下界操作

使用方法和含义均在代码的批注中给出,因为有较多的输出,所以设置输出内容的第一个值为当前print()方法所在的行

矩阵基本运算

import torch
import numpy as np
import sys
loc = sys._getframe()
_ = '\n'


'''
add/subtract/multiply/divide
加减乘除既可以使用相应的方法进行运算
也可以使用重载的运算符进行运算
'''
a = torch.rand(3, 4)
b = torch.rand(4)
print(loc.f_lineno, _, a, _, b)
# 在进行相加时,若维度不能对应,则会自动进行维度扩展(复制到其他维度)
print(loc.f_lineno, _, a+b, _, torch.add(a, b))
# 使用eq()函数进行运算结果比较,会返回对应位置是否相等
print(loc.f_lineno, _, torch.eq(a+b, torch.add(a, b)))

# 减法运算
print(loc.f_lineno, _, torch.eq(a-b, torch.sub(a, b)))

# 乘法运算,这里是对应位置相乘
print(loc.f_lineno, _, torch.eq(a*b, torch.mul(a, b)))

# 除法运算
print(loc.f_lineno, _, torch.eq(a/b, torch.div(a, b)))


'''
matmul
矩阵相乘,数学中的点乘
两种方法.mm()和.matmul()
.matmul()的重载符号为 @
'''
# .mm()方法只适用于二维机制相乘
c = torch.rand(3, 4)
d = torch.rand(4, 3)
print(loc.f_lineno, _, c, _, d)
print(loc.f_lineno, _, torch.mm(c, d))
# .matmul()可以用于任意维度的矩阵相乘
print(loc.f_lineno, _, torch.matmul(c, d))
# 高维的运算,需要保证后面两位满足矩阵运算要求的形式
cc = torch.rand(2, 2, 1, 2)
dd = torch.rand(2, 2, 2, 3)
# 这里等价于 (1, 2)乘(2, 3)得到(1, 3),前面两个维度只是对数量的约束
print(loc.f_lineno, _, torch.matmul(cc, dd))
print(loc.f_lineno, _, torch.eq(torch.matmul(cc, dd), cc@dd))


'''
pow次方
只需要直接给定要次方的矩阵和次方数
'''
e = torch.full([3, 3], 4.)
# 对每个位置的数据均进行三次方
print(loc.f_lineno, _, torch.pow(e, 3))

'''sqrt/rsqrt次方根'''
# sqrt()的作用为开二次方
print(loc.f_lineno, _, e.sqrt())
# sqrt()可以用pow()代替,0.5次方即为开方
print(loc.f_lineno, _, torch.pow(e, 0.5))
# rsqrt()的作用是开二次方以后取倒数
print(loc.f_lineno, _, e.rsqrt())

# 一个指数和对数的例子
f = torch.exp(torch.ones(2, 2))
print(loc.f_lineno, _, f)
print(loc.f_lineno, _, torch.log(f))


'''近似运算'''
g = torch.tensor(3.14)
# 向下取整 .floor()
print(loc.f_lineno, _, g.floor())
# 向上取整 .ceil()
print(loc.f_lineno, _, g.ceil())
# 取整数位 .trunc()
print(loc.f_lineno, _, g.trunc())
# 取小数位 .frac()
print(loc.f_lineno, _, g.frac())
# 四舍五入 .round()
print(loc.f_lineno, _, g.round())


'''设置tensor的下界值'''
h = torch.rand(2, 3)*15
print(loc.f_lineno, _, h)
# h的最大值
print(loc.f_lineno, _, h.max())
# h的中位数
print(loc.f_lineno, _, h.median())
# 将下界设置为8.0
print(loc.f_lineno, _, h.clamp(8))
# 将上下界限制到5到10
print(loc.f_lineno, _, h.clamp(5, 10))

输出结果

15 
 tensor([[0.5408, 0.6276, 0.0154, 0.1031],
        [0.2101, 0.4961, 0.8017, 0.3849],
        [0.9705, 0.9825, 0.1135, 0.7718]]) 
 tensor([0.2989, 0.2533, 0.1966, 0.6949])
17 
 tensor([[0.8397, 0.8810, 0.2120, 0.7980],
        [0.5090, 0.7494, 0.9983, 1.0798],
        [1.2694, 1.2358, 0.3100, 1.4667]]) 
 tensor([[0.8397, 0.8810, 0.2120, 0.7980],
        [0.5090, 0.7494, 0.9983, 1.0798],
        [1.2694, 1.2358, 0.3100, 1.4667]])
19 
 tensor([[True, True, True, True],
        [True, True, True, True],
        [True, True, True, True]])
22 
 tensor([[True, True, True, True],
        [True, True, True, True],
        [True, True, True, True]])
25 
 tensor([[True, True, True, True],
        [True, True, True, True],
        [True, True, True, True]])
28 
 tensor([[True, True, True, True],
        [True, True, True, True],
        [True, True, True, True]])
40 
 tensor([[0.6761, 0.1204, 0.7883, 0.5553],
        [0.4680, 0.8676, 0.6789, 0.4738],
        [0.5281, 0.6717, 0.2215, 0.7359]]) 
 tensor([[0.6690, 0.2942, 0.1518],
        [0.3037, 0.3202, 0.2689],
        [0.1308, 0.1508, 0.0598],
        [0.2141, 0.5089, 0.7445]])
41 
 tensor([[0.7109, 0.6389, 0.5956],
        [0.7669, 0.7589, 0.6977],
        [0.7438, 0.7783, 0.8219]])
43 
 tensor([[0.7109, 0.6389, 0.5956],
        [0.7669, 0.7589, 0.6977],
        [0.7438, 0.7783, 0.8219]])
48 
 tensor([[[[0.7022, 0.5688, 0.9368]],

         [[0.6868, 0.4112, 0.0539]]],


        [[[1.0916, 1.0123, 0.4500]],

         [[0.5753, 0.5818, 0.6051]]]])
49 
 tensor([[[[True, True, True]],

         [[True, True, True]]],


        [[[True, True, True]],

         [[True, True, True]]]])
58 
 tensor([[64., 64., 64.],
        [64., 64., 64.],
        [64., 64., 64.]])
62 
 tensor([[2., 2., 2.],
        [2., 2., 2.],
        [2., 2., 2.]])
64 
 tensor([[2., 2., 2.],
        [2., 2., 2.],
        [2., 2., 2.]])
66 
 tensor([[0.5000, 0.5000, 0.5000],
        [0.5000, 0.5000, 0.5000],
        [0.5000, 0.5000, 0.5000]])
70 
 tensor([[2.7183, 2.7183],
        [2.7183, 2.7183]])
71 
 tensor([[1., 1.],
        [1., 1.]])
77 
 tensor(3.)
79 
 tensor(4.)
81 
 tensor(3.)
83 
 tensor(0.1400)
85 
 tensor(3.)
90 
 tensor([[ 9.6262, 13.9450,  2.0478],
        [ 1.0169,  0.5764, 13.7444]])
92 
 tensor(13.9450)
94 
 tensor(2.0478)
96 
 tensor([[ 9.6262, 13.9450,  8.0000],
        [ 8.0000,  8.0000, 13.7444]])
98 
 tensor([[ 9.6262, 10.0000,  5.0000],
        [ 5.0000,  5.0000, 10.0000]])

Process finished with exit code 0

posted @ 2020-08-14 15:57  博0_oer~  阅读(96)  评论(0编辑  收藏  举报