入门Pytorch:对Tensor的操作

目录

前言

一、创建

list创建

numpy创建

填充创建

初始化

规律变化

指定类型创建

指定数据类型

转换数据类型

二、索引

直接索引

切片

用...表示多个被省略

三、维度变换

view,reshape维度变换

unsqueeze插入维度

squeeze删除维度

repeat复制维度

维度交换

四、广播机制

五、拼接和拆分

拼接

拆分

六、数学运算

四则运算

矩阵乘法

指数,对数运算

限制数据的上下限

逻辑运算

小数点

七、属性统计

基础属性

分维度

范数

总结


前言

在过去的一段时间里,我专注于图像处理和裂缝检测方面的研究。最近这段时间也把裂缝的二维检测做好了,效果也是比较令我满意,在项目的最后阶段,深度学习和PyQt的UI界面制作是需要重点学习的内容,我需要在这较短的时间内去好好学习,拿出成果来。

一、创建

import torch
import numpy as np

list创建

# list创建
a=torch.tensor([1,2])
b=torch.tensor([[1,2],[3,4]])

numpy创建

# numpy创建
c=torch.from_numpy(np.array([1,2]))

填充创建

#全1填充
print(torch.ones(2, 3))

#全0填充
print(torch.zeros(2, 3))

#full填充
print(torch.full([2, 3], 7))

#对角矩阵
print(torch.eye(3, 3))

初始化

#创建但不初始化
print(torch.empty(2, 3))

#标准正态分布
print(torch.randn(2, 3))

#0-1均匀分布
print(torch.rand(2, 3))

#1-9的整数均匀分布
print(torch.randint(1, 10, [2, 3]))

tensor([[0., 0., 0.],
            [0., 0., 0.]])
tensor([[-1.0020,  0.6792, -1.2098],
            [-2.6513, -1.4667, -0.0785]])
tensor([[0.8533, 0.4675, 0.9124],
            [0.3488, 0.5822, 0.0545]])
tensor([[2, 9, 7],
            [8, 6, 3]])

规律变化

#自增数列,0到9,步长是1
print(torch.arange(0, 10, 2))

#等差数列,0到10,4个数
print(torch.linspace(0, 10, 4))

tensor([0, 2, 4, 6, 8])
tensor([ 0.0000,  3.3333,  6.6667, 10.0000])

指定类型创建

#指定类型的创建
torch.FloatTensor([1, 2]), torch.LongTensor([1, 2])

(tensor([1., 2.]), tensor([1, 2]))

指定数据类型

#获取数据类型
torch.randn(2, 3).dtype

转换数据类型

#转换数据类型
torch.randn(2, 3).to(torch.float64).dtype

二、索引

创建数据,表示4张图片,3通道,28*28像素

import torch
a = torch.randn(4, 3, 28, 28)
print(a.shape)

torch.Size([4, 3, 28, 28])

直接索引

查看第0张图片

print(a[0].shape)

torch.Size([3, 28, 28])

查看第0张图片的第0个通道

print(a[0, 0].shape)

torch.Size([28, 28])

查看第0张图片的第0个通道的第2行

print(a[0, 0, 2].shape)

 torch.Size([28])

查看第0张图片的第0个通道的第2行第4列

print(a[0, 0, 2, 4].shape)

torch.Size([])  #输出是一个标量值,无形状

切片

切片操作的语法是start:stop:step,其中start表示起始位置,stop表示终止位置(不包含),step表示步长。

#查看0-1张图片
print(a[:2].shape)
#查看0-1张图片的0-1通道
print(a[:2, :2].shape)
#查看0-1张图片的所有通道的倒数5-正数24行
print(a[:2, :, -5:25].shape)
#有间隔的索引
print(a[:, :, :, ::2].shape)

torch.Size([2, 3, 28, 28])
torch.Size([2, 2, 28, 28])
torch.Size([2, 3, 2, 28])
torch.Size([4, 3, 28, 14])

用...表示多个被省略

#取所有图片的第0-2列
print(a[..., :2].shape)

#取所有图片的第0-2行
print(a[..., :2, :].shape)

#取第2张图片
print(a[2, ...].shape)

torch.Size([4, 3, 28, 2])
torch.Size([4, 3, 2, 28])
torch.Size([3, 28, 28])

三、维度变换

view,reshape维度变换

#4张图片,单通道,28*28像素
a = torch.randn(4, 1, 28, 28)
print(a.shape)

#转换为4,784维度,相当于打平了
print(a.reshape(4, 784).shape)

#转换为4,28,28维度,相当于重新展开
print((a.reshape(4, 784)).reshape(4, 28, 28).shape)

#view的用法和reshape不作区分
print(a.view(4, 784).shape)

torch.Size([4, 1, 28, 28])
torch.Size([4, 784])
torch.Size([4, 28, 28])
torch.Size([4, 784])

unsqueeze插入维度

#2*2的tensor
a = torch.randn(2, 2)
print(a.shape)

#插入维度在第0维
print(a.unsqueeze(0).shape)

#插入维度在倒数第1维
print(a.unsqueeze(-1).shape)

torch.Size([2, 2])
torch.Size([1, 2, 2])
torch.Size([2, 2, 1])

squeeze删除维度

只能删除为1的维度

#1*2*2*1的tensor
a = torch.randn(1, 2, 2, 1)
print(a.shape)

#删除第0维
print(a.squeeze(0).shape)

#删除倒数第1维
print(a.squeeze(-1).shape)

#删除所有为1的维度
print(a.squeeze().shape)

torch.Size([1, 2, 2, 1])
torch.Size([2, 2, 1])
torch.Size([1, 2, 2])
torch.Size([2, 2])

repeat复制维度

#分别复制2次和3次
print(torch.randn(2, 2).repeat(2, 3).shape)

torch.Size([4, 6])

维度交换

# t转置,只能操作2维tensor
print(torch.randn(1, 2).t().shape)
# transpose维度交换,指定要转换的维度,只能两两交换
print(torch.randn(1, 2, 3).transpose(0, 1).shape)
# permute维度交换,输入维度的顺序
print(torch.rand(1, 2, 3).permute(2, 1, 0).shape)

torch.Size([2, 1])
torch.Size([2, 1, 3])
torch.Size([3, 2, 1])

四、广播机制

import torch

a = torch.randn(2, 3)
b = torch.randn(1, 3)
c = torch.randn(1)

print((a + b).shape)
print((a + c).shape)

#手动boradcast
print(b.expand_as(a).shape)
print(c.expand_as(a).shape)

输出均为torch.Size([2, 3])

五、拼接和拆分

拼接

import torch

a = torch.rand(4, 32, 8)
b = torch.rand(5, 32, 8)

torch.cat([a, b], dim=0).shape

torch.Size([9, 32, 8])

cat拼接,dim=0是指定要拼接的维度。

a = torch.rand(4, 32, 8)
b = torch.rand(4, 32, 8)

torch.stack([a, b], dim=0).shape

torch.Size([2, 4, 32, 8])

stack组合,会创建一个新的维度,用以区分组合后的两个tensor。

拆分

a = torch.rand(4, 32, 8)

_1, _2 = a.split(2, dim=0)
print(_1.shape)
print(_2.shape)

_1, _2, _3 = a.split([1, 2, 1], dim=0)
print(_1.shape)
print(_2.shape)
print(_3.shape)

#split拆分,在0维度上拆分,每2个元素1拆

torch.Size([2, 32, 8])
torch.Size([2, 32, 8])               

#在第0维(行维)上按照切分尺寸 [1, 2, 1] 进行切分。其中,第一个切分尺寸为1,第二个切分尺寸为2,第三个切分尺寸为1。
torch.Size([1, 32, 8])         
torch.Size([2, 32, 8])
torch.Size([1, 32, 8])

a = torch.rand(4, 32, 8)

_1, _2 = a.chunk(2, dim=0)
print(_1.shape)
print(_2.shape)

torch.Size([2, 32, 8])
torch.Size([2, 32, 8])              #chunk拆分,在0维度上拆分,拆成2个

六、数学运算

import torch

#测试数据
a = torch.FloatTensor([[0, 1, 2], [3, 4, 5]])
b = torch.FloatTensor([0, 1, 2])

(tensor([[0., 1., 2.],
             [3., 4., 5.]]),
tensor([0., 1., 2.]))

四则运算

print(a + b)
print(a - b)
print(a * b)
print(a / b)

矩阵乘法

print(a @ b)
print(a.matmul(b))

计算过程0 * 0 + 1 * 1 + 2 * 2, 0 * 3 + 1 * 4 + 2 * 5

指数,对数运算

#求指数
print(a**2)
#开根号
print(a**0.5)
#求e的n次方
print(a.exp())
#以e为底,求对数
print(a.log())
#以2为底,求对数
print(a.log2())

限制数据的上下限

a.clamp(2, 4)

tensor([[2., 2., 2.],
            [3., 4., 4.]])

逻辑运算

#大于
print(a > b)
#小于
print(a < b)
#等于
print(a == b)
#不等于
print(a != b)

小数点

#四舍五入
c = torch.FloatTensor([3.14])
#向下取整
print(c.floor())
#向上取整
print(c.ceil())
#四舍五入
print(c.round())

tensor([3.])
tensor([4.])
tensor([3.])

七、属性统计

import torch

#测试数据
a = torch.FloatTensor([[0., 1., 2.], [3., 4., 5.]])

tensor([[0., 1., 2.],
            [3., 4., 5.]])

基础属性

#求最小
print(a.min())
#求最大
print(a.max())
#求平均
print(a.mean())
#求积
print(a.prod())
#求和
print(a.sum())
#求最大值下标
print(a.argmax())
#求最小值下标
print(a.argmin())

tensor(0.)
tensor(5.)
tensor(2.5000)
tensor(0.)
tensor(15.)
tensor(5)
tensor(0)

分维度

#分维度求最大
a.max(dim=0)
#分维度求最大值下标
a.argmax(dim=0)

#求前2个最小值
a.topk(2, dim=1, largest=False)
#求第2个小的值
a.kthvalue(2, dim=1)

torch.return_types.max(
values=tensor([3., 4., 5.]),
indices=tensor([1, 1, 1]))

tensor([1, 1, 1])

torch.return_types.topk( values=tensor([[0., 1.], [3., 4.]]), indices=tensor([[0, 1], [0, 1]]))

torch.return_types.kthvalue( values=tensor([1., 4.]), indices=tensor([1, 1]))

范数

#求1范数
print(a.norm(1))
print(a.norm(1, dim=0))

#求2范数
print(a.norm(2))
print(a.norm(2, dim=0))

tensor(15.)
tensor([3., 5., 7.])
tensor(7.4162)
tensor([3.0000, 4.1231, 5.3852])

总结

这篇博客的主要的目的是为了方便我个人查找api,在不断的查询中熟悉pytorch的基础操作。

猜你喜欢

转载自blog.csdn.net/m0_62919535/article/details/131732445
今日推荐