推荐系统学习Pytorch笔记一:Pytorch的数据载体张量与线性回归_Miracle8070-CSDN博客
1、标量运算
import torch
import numpy as np
#加减乘除,幂,取余,比较
a=torch.tensor([[1,2],[3,4]])
b=torch.tensor([[0.2,3],[4,5]])
print(a+b)
print(a-b)
print(a*b)
print(a/b)
# tensor([[1.2000, 5.0000],
# [7.0000, 9.0000]])
# tensor([[ 0.8000, -1.0000],
# [-1.0000, -1.0000]])
# tensor([[ 0.2000, 6.0000],
# [12.0000, 20.0000]])
# tensor([[5.0000, 0.6667],
# [0.7500, 0.8000]])
print(a**2)
print(a**0.5)
print(a%2)
print(a//2)#地板除
# tensor([[ 1, 4],
# [ 9, 16]])
# tensor([[1.0000, 1.4142],
# [1.7321, 2.0000]])
# tensor([[1, 0],
# [1, 0]])
# tensor([[0, 1],
# [1, 2]])
print(a>2)
print(torch.sqrt(a))
print(torch.max(a,b))
# tensor([[False, False],
# [ True, True]])
# tensor([[1.0000, 1.4142],
# [1.7321, 2.0000]])
# tensor([[1., 3.],
# [4., 5.]])
#取整函数
x=torch.tensor([2.7,-2.7])
print(torch.round(x))#四舍五入
print(torch.floor(x))#向下归整
print(torch.ceil(x))#向上归整
print(torch.trunc(x))#向0归整
# tensor([ 3., -3.])
# tensor([ 2., -3.])
# tensor([ 3., -2.])
# tensor([ 2., -2.])
print(torch.fmod(x,2))#做除法取余数
print(torch.remainder(x,2))#做除法取剩余部分,结果恒为正
# tensor([ 0.7000, -0.7000])
# tensor([0.7000, 1.3000])
#赋值剪裁
x=torch.tensor([1.2,20.5,-19.3,0.3])
y=torch.clamp(x,min=-1,max=1)
z=torch.clamp(x,max=1)
print(y)
print(z)
# tensor([ 1.0000, 1.0000, -1.0000, 0.3000])
# tensor([ 1.0000, 1.0000, -19.3000, 0.3000])
2、向量运算
torch.max()详解torch.max()使用讲解 - 简书
#统计量
a=torch.arange(1,10).float()
print(a)
# tensor([1., 2., 3., 4., 5., 6., 7., 8., 9.])
print(torch.sum(a))
print(torch.mean(a))
print(torch.max(a))
print(torch.min(a))
# tensor(45.)
# tensor(5.)
# tensor(9.)
# tensor(1.)
print(torch.prod(a))#累乘
print(torch.std(a))#标准差
print(torch.var(a))#方差
print(torch.median(a))#中位数
# tensor(362880.)
# tensor(2.7386)
# tensor(7.5000)
# tensor(5.)
#指定维度上的扫描
b=a.view(3,3)
print(torch.max(b,dim=0))
print(torch.min(b,dim=1))
# torch.return_types.max(
# values=tensor([7., 8., 9.]),
# indices=tensor([2, 2, 2]))
# torch.return_types.min(
# values=tensor([1., 4., 7.]),
# indices=tensor([0, 0, 0]))
#cur扫描
print(torch.cumsum(a,0))
print(torch.cumprod(a,0))
print(torch.cummax(a,0).values)
print(torch.cummax(a,0).indices)
print(torch.cummin(a,0))
# tensor([ 1., 3., 6., 10., 15., 21., 28., 36., 45.])
# tensor([1.0000e+00, 2.0000e+00, 6.0000e+00, 2.4000e+01, 1.2000e+02, 7.2000e+02,
# 5.0400e+03, 4.0320e+04, 3.6288e+05])
# tensor([1., 2., 3., 4., 5., 6., 7., 8., 9.])
# tensor([0, 1, 2, 3, 4, 5, 6, 7, 8])
# torch.return_types.cummin(
# values=tensor([1., 1., 1., 1., 1., 1., 1., 1., 1.]),
# indices=tensor([0, 0, 0, 0, 0, 0, 0, 0, 0]))
#排序
x=torch.tensor([[3,2,5],[8,4,7],[1,6,9]])
print(torch.topk(x,2,dim=0))
print(torch.topk(x,2,dim=1))
print(torch.sort(x,dim=1))
# torch.return_types.topk(
# values=tensor([[8, 6, 9],
# [3, 4, 7]]),
# indices=tensor([[1, 2, 2],
# [0, 1, 1]]))
# torch.return_types.topk(
# values=tensor([[5, 3],
# [8, 7],
# [9, 6]]),
# indices=tensor([[2, 0],
# [0, 2],
# [2, 1]]))
# torch.return_types.sort(
# values=tensor([[2, 3, 5],
# [4, 7, 8],
# [1, 6, 9]]),
# indices=tensor([[1, 0, 2],
# [1, 2, 0],
# [0, 1, 2]]))
3.矩阵运算
#矩阵运算
#矩阵乘法
a=torch.tensor([[1.0,2.0],[3.0,4.0]])
b=torch.tensor([[2.0,0.0],[3.0,9.0]])
print(a@b)
print(torch.matmul(a,b))
print(torch.mm(a,b))
#矩阵转置
print(a.t())
#矩阵的逆
print(torch.inverse(a))
#矩阵的迹
print(torch.trace(a))
#矩阵的范数
print(torch.norm(a))
#矩阵的行列式
print(torch.det(a))
#特征值和特征向量
L_,V_=torch.eig(a,eigenvectors=True)
print(L_,V_)
#svd分解
u,s,v=torch.svd(a)
4.广播机制
Pytorch的广播规则和numpy是一样的:
(1)如果张量的维度不同,将维度较小的张量进行扩展,直到两个张量的维度都一样。
(2)如果两个张量在某个维度上的长度是相同的,或者其中一个张量在该维度上的长度为1, 那么我们就说这两个张量在该维度上是相容的。
(3)如果两个张量在所有维度上都是相容的,它们就能使用广播。
(4)广播之后,每个维度的长度将取两个张量在该维度长度的较大值。
(5)在任何一个维度上,如果一个张量的长度为1,另一个张量长度大于1,那么在该维度 上,就好像是对第一个张量进行了复制。