Torch和Numpy之——基本运算
1矩阵的四则运算
输入
import torch a = torch.arange(6).reshape(2,3) b = torch.arange(1,3).reshape(2,1) #将行向量转化为列向量便于广播
print(a)
print(b)
print(a+b) #对应位置的元素性加(减法同加法)
print(a*b) #对应位置的元素相乘(除法同乘法,注意:除数不能为0)
输出
tensor([[0, 1, 2], [3, 4, 5]]) tensor([[1], #此处运算时进行了广播,即复制成3列 [2]]) tensor([[1, 2, 3], [5, 6, 7]]) tensor([[ 0, 1, 2], [ 6, 8, 10]])
2数乘(叉乘)
2.1标量(Scalar)与向量(vector)
输入
import torch a = torch.arange(1,3) b = torch.tensor(3) print(a) print(b) print(a*b)
输出
tensor([1, 2]) tensor(3) tensor([3, 6])
2.2向量与向量
输入
import torch a = torch.arange(1,4) b = torch.arange(2,5) print(a) print(b) print(a*b)
输出
tensor([1, 2, 3]) tensor([2, 3, 4]) tensor([ 2, 6, 12])
2.3向量与矩阵
输入
import torch a = torch.arange(1,7).reshape(3,2) b = torch.arange(2,4) c = torch.arange(1,4).reshape(3,1) print(a) print(b) print(c) print("__________________________________") print(a*b) print(a*c)
输出
tensor([[1, 2], [3, 4], [5, 6]]) tensor([2, 3]) tensor([[1], [2], [3]]) __________________________________ tensor([[ 2, 6], [ 6, 12], [10, 18]]) tensor([[ 1, 2], [ 6, 8], [15, 18]])
2.4矩阵与标量
输入
import torch a = torch.arange(1,7).reshape(3,2) b = torch.tensor(3) print(a) print(b) print(a*b)
输出
tensor([[1, 2], [3, 4], [5, 6]]) tensor(3) tensor([[ 3, 6], [ 9, 12], [15, 18]])
3点乘
3.1向量与向量
输入
import torch a = torch.arange(1,7) b = torch.arange(3,9) print(a.dot(b))
输出
tensor(133)
3.2矩阵与向量
torch中dot()不可以做矩阵和向量的点乘,可以在numpy中实现该功能,在numpy中既可以做一维的点乘,也可以做多为的点乘,形状相同时做矩阵乘法
输入
import numpy as np a = np.arange(1,7).reshape(3,2) b = np.arange(1,3) print(a) print(b) print(a.dot(b))
输出
[[1 2] [3 4] [5 6]] [1 2] [ 5 11 17]
如果要在torch中实现该功能可使用如下方法:
输入
import torch a = torch.arange(1,7).reshape(3,2) b = torch.arange(1,3) print(a) print(b) print("________________________________________________") print(a@b) #方法1 print(a.matmul(b)) #方法2 print(torch.matmul(a,b)) #方法3
输出
tensor([[1, 2], [3, 4], [5, 6]]) tensor([1, 2]) ________________________________________________ tensor([ 5, 11, 17]) tensor([ 5, 11, 17]) tensor([ 5, 11, 17])
4求逆矩阵
输入
import torch import numpy as np #torch实现 a = torch.tensor([[3.,4.],[5.,6.]]) print(torch.inverse(a)) #numpy实现 b = np.array([[3.,4.],[5.,6.]]) print(np.linalg.inv(b))
输出
tensor([[-3.0000, 2.0000], [ 2.5000, -1.5000]]) [[-3. 2. ] [ 2.5 -1.5]]
5求矩阵的内积与外积
内积:对应元素相乘再求和
外积:只相乘
输入
import numpy as np a = np.array([3,2,2]) c = np.array([1,2,1]) print(a) print(c) f = np.sum(a*c) print(f) print(np.dot(a,c)) print(a*c)
输出
[3 2 2] [1 2 1] 9 9 [3 4 2]
6求矩阵特征值与特征向量
输入
import torch a = torch.tensor([[3.,4.,7.],[5.,6.,8.],[2.,5.,5.]]) b = torch.tensor([[2.,5.],[3.,5.]]) print(torch.eig(a)) #求特征值 print('_____________________________________________') print(torch.eig(a,eigenvectors=True)) #求特征向量 print("**********************************************") print(torch.eig(b,eigenvectors=True))
输出
torch.return_types.eig( eigenvalues=tensor([[14.8539, 0.0000], [-0.4269, 1.2251], [-0.4269, -1.2251]]), eigenvectors=tensor([])) _____________________________________________ torch.return_types.eig( eigenvalues=tensor([[14.8539, 0.0000], [-0.4269, 1.2251], [-0.4269, -1.2251]]), eigenvectors=tensor([[-0.5181, 0.6686, 0.0000], [-0.7157, -0.0399, -0.5309], [-0.4683, -0.3045, 0.4204]])) ********************************************** torch.return_types.eig( eigenvalues=tensor([[-0.6533, 0.0000], [ 7.6533, 0.0000]]), eigenvectors=tensor([[-0.8833, -0.6625], [ 0.4688, -0.7491]]))

浙公网安备 33010602011771号