以下是运用Pytorch的一些方法进行矩阵运算的实例说明,简要介绍了矩阵的一些基本运算。
import torch
a = torch.tensor([
[0,1],
[2,3]
])
b = torch.tensor([
[5, 10]
])
# 加
print("torch.all(torch.eq(a+b, torch.add(a,b))):",
torch.all(torch.eq(a+b, torch.add(a,b))))
print("a+b:\n{}\n".format(a+b))
# 减
print("torch.all(torch.eq(a-b, torch.sub(a,b))):",
torch.all(torch.eq(a-b, torch.sub(a,b))))
print("a-b:\n{}\n".format(a-b))
# 乘(是点乘,不同于矩阵乘法)
print("torch.all(torch.eq(a*b, torch.mul(a,b))):",
torch.all(torch.eq(a*b, torch.mul(a,b))))
print("a*b:\n{}\n".format(a*b))
# 除
print("torch.all(torch.eq(a/b, torch.div(a,b))):",
torch.all(torch.eq(a/b, torch.div(a,b))))
print("a/b:\n{}\n".format(a/b))
运行结果:
PS E:\pytorch> & E:/Python/python.exe e:/pytorch/tensor.py
torch.all(torch.eq(a+b, torch.add(a,b))): tensor(True)
a+b:
tensor([[ 5, 11],
[ 7, 13]])
torch.all(torch.eq(a-b, torch.sub(a,b))): tensor(True)
a-b:
tensor([[-5, -9],
[-3, -7]])
torch.all(torch.eq(a*b, torch.mul(a,b))): tensor(True)
a*b:
tensor([[ 0, 10],
[10, 30]])
torch.all(torch.eq(a/b, torch.div(a,b))): tensor(True)
a/b:
tensor([[0.0000, 0.1000],
[0.4000, 0.3000]])
a * b,要求两个矩阵维度完全一致,即两个矩阵对应元素相乘,输出的维度也和原矩阵维度相同:
import torch
a = torch.randn(3,4)
b = torch.randn(3,4)
print(a)
print(b)
c = a * b
print(c.size())
print(c)
运行结果:
PS E:\pytorch> & E:/Python/python.exe e:/pytorch/tensor.py
tensor([[-1.3945, 1.6582, 0.7624, 0.3704],
[-1.0848, -1.1854, -1.4984, -0.3506],
[-0.2800, -0.2005, 1.3975, 0.4640]])
tensor([[ 0.3081, -0.1954, 1.3569, 0.8477],
[ 1.4227, -0.1329, -2.3404, 0.0841],
[ 0.6497, -0.8493, -0.0081, 0.3146]])
torch.Size([3, 4])
tensor([[-0.4296, -0.3240, 1.0346, 0.3139],
[-1.5435, 0.1575, 3.5067, -0.0295],
[-0.1819, 0.1703, -0.0113, 0.1460]])
torch.mm(a,b),要求两个矩阵维度是(n×m)和(m×p),即普通二维矩阵乘法:
import torch
a = torch.randn(3,4)
b = torch.randn(4,3)
print(a)
print(b)
c = torch.mm(a,b)
print(c.size())
print(c)
运行结果:
PS E:\pytorch> & E:/Python/python.exe e:/pytorch/tensor.py
tensor([[-1.0420, -0.8610, -0.6780, -0.5174],
[ 1.3692, 0.9278, 0.7649, 2.2765],
[ 0.0511, 1.9260, 0.1995, 1.6820]])
tensor([[-0.4900, -0.5941, 0.1763],
[-0.8002, -0.0556, -0.6381],
[-0.1235, 2.0824, 0.0786],
[ 0.6074, -0.3087, 2.2040]])
torch.Size([3, 3])
tensor([[ 0.9691, -0.5852, -0.8279],
[-0.1251, 0.0250, 4.7272],
[-0.5692, -0.2413, 2.5029]])
torch.matmul(a,b),matmul可以进行张量乘法,输入可以是高维,当输入是多维时,把多出的一维作为batch提出来,其他部分做矩阵乘法:
import torch
a = torch.randn(2,3,4)
b = torch.randn(4,3)
print(a)
print(b)
c = torch.matmul(a,b)
print(c.size())
print(c)
运行结果:
PS E:\pytorch> & E:/Python/python.exe e:/pytorch/tensor.py
tensor([[[ 1.3968, 1.3581, -0.3681, -0.5124],
[-2.3154, 0.2749, 0.4301, -1.1227],
[ 1.2104, 0.9626, 0.6384, -0.3354]],
[[ 0.8319, -1.4187, -2.1479, 0.9941],
[-0.5659, -0.8840, 0.4688, -0.0346],
[ 0.8974, -1.7360, -1.9097, 0.8564]]])
tensor([[-1.2399, -0.2424, 0.4228],
[-1.2637, -1.1116, -1.2583],
[ 0.2614, 0.8575, -0.4167],
[ 0.3284, -1.6039, -0.0701]])
torch.Size([2, 3, 3])
tensor([[[-3.7126, -1.3421, -0.9291],
[ 2.2671, 2.4252, -1.4253],
[-2.6603, -0.2781, -0.9420]],
[[ 0.5263, -2.0609, 2.9623],
[ 1.9300, 1.5773, 0.6802],
[ 0.8631, -1.2990, 3.2996]]])
幂运算主要演示了平方以及平方根,代码如下:
import torch
a = torch.tensor([
[10, 20],
[30, 40]
])
#平方
print("a.pow(2):\n{}\n".format(a.pow(2)))
print("a**2:\n{}\n".format(a**2))
#平方根
print("a.pow(0.5):\n{}\n".format(a.pow(0.5)))
print("a.sqrt():\n{}\n".format(a.sqrt()))
# 平方根的倒数
print("a.rsqrt():\n{}\n".format(a.rsqrt()))
print("1/(a**0.5):\n{}\n".format(1/(a**0.5)))
运行结果:
PS E:\pytorch> & E:/Python/python.exe e:/pytorch/tensor.py
a.pow(2):
tensor([[ 100, 400],
[ 900, 1600]])
a**2:
tensor([[ 100, 400],
[ 900, 1600]])
a.pow(0.5):
tensor([[3.1623, 4.4721],
[5.4772, 6.3246]])
a.sqrt():
tensor([[3.1623, 4.4721],
[5.4772, 6.3246]])
a.rsqrt():
tensor([[0.3162, 0.2236],
[0.1826, 0.1581]])
1/(a**0.5):
tensor([[0.3162, 0.2236],
[0.1826, 0.1581]])
指数运算以及取对数运算演示代码如下:
import torch
a = torch.tensor([
[10, 8],
[10, 5]
])
# e^x
a_exp = torch.exp(a)
print("torch.exp(a):\n{}\n".format(a_exp))
# log(x)
# 以x为底:logx
# 以10为底:log10
print("torch.log10(a):\n{}\n".format(torch.log10(a)))
运行结果:
PS E:\pytorch> & E:/Python/python.exe e:/pytorch/tensor.py
torch.exp(a):
tensor([[22026.4648, 2980.9580],
[22026.4648, 148.4132]])
torch.log10(a):
tensor([[1.0000, 0.9031],
[1.0000, 0.6990]])