1.Tensor的创建
python
import torch
import numpy as np
#使用list创建张量
a=torch.tensor([[1,2],[3,4]],dtype=torch.float64)
#使用array创建张量
b=torch.tensor(np.array([[1,2],[3,4]]),dtype=torch.uint8)
print(a)
print(b)
输出:
2.矩阵的叉乘和点乘
python
import torch
import numpy as np
a=torch.tensor([[1,2],[3,4]])
b=torch.tensor([[1,2],[3,4]])
#逐元素相乘(点乘)
c=a*b
print(c)
#矩阵乘法(叉乘)
c=torch.mm(a,b)
print(c)
输出:
- 直接创建数据
python
import torch
import numpy as np
#从range创建,规则与range相同
print(torch.arange(5))
print(torch.arange(1,7,2))
#linespace创建,前两个参数是起始与终止,最后一个是返回的个数(维度)
print(torch.linspace(0,5,10))
#创建全1矩阵,第一个是行,第二个是列
print(torch.ones(4,3))
#创建全0矩阵
print(torch.zeros(3,3))
输出:
4.一些特殊函数
python
import torch
import numpy as np
a=torch.tensor([[1,2],[3,4]])
#分段函数,小的放大,大的放小
print(torch.clamp(a,min=2,max=3))
#化成整数
a=torch.tensor([1.01,2.5,-3.44,5.99])
print(torch.round(a))
#计算双曲正切
a=torch.Tensor([-3,-2,-1,-0.5,0,0.5,1,2,3])
print(torch.tanh(a))
输出:
5.随机创建
python
import torch
import numpy as np
#从[0,1]中随机取
print(torch.rand(3,3))
#从正态分布中取
print(torch.randn(3,3))
#从指定范围内随机取(整数
print(torch.randint(0,9,[3,3]))
输出:
6.索引
python
import torch
import numpy as np
a=torch.arange(9).view(3,3)
'''
[[0,1,2]
[3,4,5]
[6,7,8]]
'''
#直接索引
print(a[1,2])
#整数索引
row=[1,0]
col=[1,2]
print(a[row,col])
#布尔索引
index=a>3
print(index)
print(a[index])
输出:
7.切片
python
import torch
import numpy as np
a=torch.arange(9).view(3,3)
'''
[[0,1,2]
[3,4,5]
[6,7,8]]
'''
#切片
print(a[1:,:2])
#带步长的切片(不支持负数)
print(a[::2])
输出:
8.搜索函数
python
import torch
import numpy as np
#nonzero寻找非负元素(返回索引)
a=torch.randint(0,2,(3,3))
print(a)
print(torch.nonzero(a))
#where寻找条件
a=torch.randn(3,3)
b=torch.ones(3,3)
print(a)
#如果满足条件就返回a元素,否则返回b元素
print(torch.where(a>0,a,b))
输出:
9.Tensor的查询
python
import torch
import numpy as np
a=torch.rand(1,2,3,4,5)
#元素个数=1*2*3*4*5=120
print(a.nelement())
#轴个数
print(a.ndimension())
#矩阵维度
print(a.size(),a.shape)
输出:
10.Tensor的变换
python
import torch
import numpy as np
a=torch.rand(1,2,3,4,5)
#矩阵重塑
b=a.view(2*3,4*5)
print(b.shape)
c=a.reshape(-1)
print(c.shape)
d=a.reshape(2*3,-1)
print(d.shape)
print()
#维度增减
a=torch.rand(1,2,3,4,5)
b=torch.squeeze(a)
print(b.shape)
b=torch.unsqueeze(b,0)
print(b.shape)
print()
#矩阵转置(只接受二维tensor)
a=torch.tensor([[2]])
b=torch.tensor([[2,3]])
print(torch.transpose(a,1,0,))
print(torch.t(a))
print(torch.transpose(b,1,0,))
print(torch.t(b))
print()
#维度转换(调位置)
a=torch.rand((1,224,224,3))
print(a.shape)
b=a.permute(0,3,1,2)
print(b.shape)
输出:
11.拼接
python
import torch
import numpy as np
a=torch.randn(2,3)
b=torch.randn(3,3)
#cat()
c=torch.cat((a,b))
d=torch.cat((b,b,b),dim=1)
print(c.shape)
print(d.shape)
#stack()
c=torch.stack((b,b),dim=1)
d=torch.stack((b,b),dim=0)
print(c.shape)
print(d.shape)
输出:
12.拆分
python
import torch
import numpy as np
a=torch.randn(10,3)
for x in torch.split(a,[1,2,3,4],dim=0):
print(x.shape)
print()
for x in torch.split(a,4,dim=0):
print(x.shape)
print()
for x in torch.chunk(a,4,dim=0):
print(x.shape)
输出:
13.Reduction
python
import torch
import numpy as np
a=torch.tensor([[1,2],[3,4]])
#最大值
print(torch.max(a))
#横轴累加
print(torch.cumsum(a,dim=0))
#纵轴累乘
print(torch.cumprod(a,dim=1))
a=torch.Tensor([[1,2],[3,4]])
#均值
print(a.mean())
#中值
print(a.median())
#协方差
print(a.std())
输出: