当前位置: 首页 > news >正文

ccc-pytorch-基础操作(2)

文章目录

      • 1.类型判断isinstance
      • 2.Dimension实例
      • 3.Tensor常用操作
      • 4.索引和切片
      • 5.Tensor维度变换
      • 6.Broadcast自动扩展
      • 7.合并与分割
      • 8.基本运算
      • 9.统计属性
      • 10.高阶OP

大伙都这么聪明,注释就只写最关键的咯

1.类型判断isinstance

常见类型如下:
image-20230219203754818

 a = torch.randn(2,3)a.type()

image-20230219203400016

data = torch.FloatTensor()
isinstance(data,torch.cuda.FloatTensor)
data = data.cuda()
isinstance(data,torch.cuda.FloatTensor)

在这里插入图片描述

2.Dimension实例

Dim0:Loss

torch.tensor(1.0)
torch.tensor(1.3)
a=torch.tensor(2.2)
a.shape
a.size()

image-20230219203939483
Dim1:bias,linear input

a = torch.tensor([1.1])
a.size()
a
a.shape
b = torch.FloatTensor(1)
b.size()

image-20230219204608965
Dim2:linear input batch

 a = torch.randn(2,3)aa.shapea.size(0)a.size(1)a.shape[1]

在这里插入图片描述
Dim3:RNN input batch

a = toch.rand(1,2,3)
a
a.shape
a[0]

在这里插入图片描述
Dim4:图像输入[b, c, h, w]

a = torch.rand(2,3,28,28)
a
a.shape
list(a.shape)

在这里插入图片描述
Mixed:

a = torch.rand(2,3,28,28)
a.numel()
a.dim()
a = torch.tensor(1)
a.dim()

在这里插入图片描述

3.Tensor常用操作

Import from numpy

a = np.array([2,3.3])
torch.from_numpy(a)
a = np.ones([2,3])
torch.from_numpy(a)

在这里插入图片描述
Import from List

torch.tensor([2.,3.2])
torch.FloatTensor([2.,3.2])#不推荐
torch.tensor([[2.,3.2],[1.,22.3]])

在这里插入图片描述
uninitialized

torch.empty(1)
torch.Tensor(2,3)
torch.IntTensor(2,3)
torch.FloatTensor(2,3)

在这里插入图片描述
set default type

torch.tensor([1.2,3]).type()
torch.set_default_tensor_type(torch.DoubleTensor)
torch.tensor([1.2,3]).type()

在这里插入图片描述
rand/rand_like, randint,randn

torch.rand(3,3)#[0, 1]
a = torch.rand(3,3)
torch.rand_like(a)
torch.randint(1,10,[3,3])#[min, max)
torch.randn(3,3)#N(0,1)

image-20230219212106952
full&arange/range

torch.full([2,3],7)
torch.full([],7)
torch.full([1],7)
torch.arange(0,10)
torch.arange(0,10,2)
torch.range(0,10)#不建议
torch.normal(mean=torch.full([10],0), std=torch.arange(1,0,-0.1))#N(mean,std)

在这里插入图片描述
linspace/logspace

torch.linspace(0,10,steps=4)
torch.linspace(0,10,steps=11)
torch.logspace(0,10,steps=4)

在这里插入图片描述
Ones/zeros/eye

torch.ones(3,3)
torch.zeros(3,3)
torch.eys(3,4)
torch.eys(3,3)
#常用快捷操作
a = torch.zeros(3,3)
torch.ones_like(a)

在这里插入图片描述
randperm

a = torch.rand(2,3)
a
b = torch.rand(2,2)
b
a,b
torch.randperm(10)

在这里插入图片描述

4.索引和切片

Indexing:dim0 优先

a = torch.rand(4,3,28,28)
a[0].shape
a[0,0,2,4]#取值

在这里插入图片描述
select first/last N

a[:2].shape#第一维[0,2)
a[:2,:1,:,:].shape#第一维[0,2),第二维[0,1)
a[:2,1:,:,:].shape#第一维[0,2),第二维[1,_]
a[:2,-1:,:,:].shape#第一维[0,2),第二维(1,_]

在这里插入图片描述
select by steps

a[:,:,0:28:2,0:28:2].shape
a[:,:,::2,::2].shape

在这里插入图片描述
select by specific index

a.index_select(0,torch.tensor([0,2])).shape 
a.index_select(1,torch.tensor([1,2]).shape 
a.index_select(2,torch.arange(8)).shape 

在这里插入图片描述
…:表全部

a[...].shape
a[:,1,...].shape
a[...,:2].shape

在这里插入图片描述
select by mask

x = torch.randn(3,4)
mask = x.ge(0.5)
mask
x
torch.masked_select(x,mask)
torch.masked_select(x,mask).shape

在这里插入图片描述
select by flatten index

 b = torch.tensor([[4,3,5],[6,7,8]])
torch.take(b,torch.tensor([0,2,5]))#返回展开后的索引

在这里插入图片描述

5.Tensor维度变换

View reshape

a = torch.rand(4,1,28,28)
a.shape
a.view(4,28*28)
a.view(4*28,28).shape
b = a.view(4,784)#不推荐,合并尽量写成连乘帮助记忆原数据各维度作用

在这里插入图片描述
Squeeze v.s. unsqueeze

a.shpae
a.unsqueeze(0).shape#第一个空位
a.unsqueeze(-1).shape#最后一个空位
a.unsqueeze(4).shape#第五个空位
a.unsqueeze(-4).shape#倒数第4个空位
a.unsqueeze(-5).shape#导数第5个空位
a.unsqueeze(5).shape#第6个空位,没有

在这里插入图片描述

a = torch.tensor([1.2,2.3])
a.unsqueeze(-1)
a.unsqueeze(0)

image-20230219221608436

b = torch.rand(32)
f = torch.rand(4,32,14,14)
b = b.unsqueeze(1).unsqueeze(2).unsqueeze(0)#每次处理后重新排序
b.shape

image-20230219221758123

b.squeeze().shape#压缩所有为1
b.squeeze(0).shape
b.squeeze(-1).shape
b.squeeze(1).shape#不是1,所以不变
b.squeeze(-4).shape#导数第四个

image-20230219221946606
Expand ( broadcasting)/ repeat(memory copied)

a = torch.rand(4,32,14,14)
b.shape
b.expand(4,32,14,14).shape
b.repeat(4,32,1,1).shape

image-20230219222354763
.t:转置

a = torch.randn(3,4)
a
a.t()#注意高维转置不了

image-20230219222611746
Transpose

a.shape
a1 = a.transpose(1,3)#交换次序
a1.shape

image-20230219223234601
permute

b = torch.rand(4,3,28,32)
b.transpose(1,3).shape
b.transpose(1,3).transpose(1,2).shape
b.permute(0,2,3,1).shape#按索引排列

image-20230219223519170

6.Broadcast自动扩展

broadcast是不同size的tensor进行加减时自动进行的机制,其主要思想以及特点如下:

  • 从最右边的维度开始匹配,前面维度缺失的补1直到维度相同
  • 从最右边的维度开始匹配,维度不等但有一个是1则扩展到相同的值,实例如下:
    image-20230220142625094
  • 节约内存,核心是利用expand,只进行逻辑上的扩展而不会实际拷贝

7.合并与分割

Cat

a = torch.rand(4,3,32,32)
b = torch.rand(5,3,32,32)
a2= torch.rand(4,1,32,32)
torch.cat([a,a2],dim=1).shape
torch.cat([a,a2],dim=0).shape#Error,多个维度不同

image-20230220143834187
stack

a1=torch.rand(4,3,16,32)
a2=torch.rand(4,3,16,32)
torch.cat([a1,a2],dim=2).shape
torch.stack([a1,a2],dim=2).shape

image-20230220144151154
Cat与stack的区别:前者是合并dim,后者是增加dim;后者要求所有一摸一样
Split: by len

c = torch.rand(2,32,8)
aa, bb = c.split([1,1],dim=0)
aa.shape, bb.shape
aa, bb = c.split([2,0],dim=0)
aa.shape, bb.shape
aa, bb = c.split(1,dim=0)
aa.shape, bb.shape
aa, bb = c.split(2,dim=0)

在这里插入图片描述
Chunk: by num

c.shape
aa, bb = c.chunk(2,dim=0)

image-20230220145316617

8.基本运算

basic

a =torch.rand(3,4)
a
b =torch.rand(4)
b
a+b
torch.add(a,b)
torch.all(torch.eq(a-b,torch.sub(a,b)))
torch.all(torch.eq(a*b,torch.mul(a,b)))
torch.all(torch.eq(a/b,torch.div(a,b)))

image-20230220160151657
matmul

a
b = torch.ones(2,2)
b
torch.mm(a,b)#only for 2dim
torch.matmul(a,b)
a@b

image-20230220160506308

a = torch.rand(4,784)
x = torch.rand(4,784)
w = torch.rand(512,784)
(x@w.t()).shape

image-20230220160749310
>2d tensor matmul?

a =torch.rand(4,3,28,64)
b =torch.rand(4,3,64,32)
torch.mm(a,b).shape#用不了2D以上
torch.matmul(a,b).shape#只计算最后两维
b = torch.rand(4,1,64,32)
torch.matmul(a,b).shape#触发Broadcast机制

image-20230220161518890
Power

a = torch.full([2,2],3)
a.pow(2)
a**2
aa = a**2
aa.sqrt()
aa.rsqrt()#平方根的倒数
aa**(0.5)

在这里插入图片描述
Exp log

a = torch.exp(torch.ones(2,2))
a
torch.log(a)

image-20230220162031968
Approximation

a =torch.tensor(3.14)
a.floor(),a.ceil(),a.trunc(),a.frac()#分别是向下取整,向上取整,整数部分,小数部分
a =torch.tensor(3.4999)
a.round()#4舍5入
a = torch.tensor(3.5)
a.round()

在这里插入图片描述
clamp

grad = torch.rand(2,3)*15
grad
grad.max()
grad.median()
grad.clamp(10)
grad.clamp(0,10)

image-20230220163632170
简单解释一下:

torch.clamp(input, min=None, max=None, *, out=None)
限定一个范围,input tensor中数值低于min的返回min,高于max的返回max

9.统计属性

norm1&norm2

a =torch.full([8],1)
a
b=a.view(2,4)
c=a.view(2,2,2)
b
c
a.norm(1),b.norm(1),c.norm(1)#1范数,绝对值之和
a.norm(2),b.norm(2),c.norm(2)#2范数,欧几里得范数,平方和再开方
b.norm(1,dim=1)
b.norm(2,dim=1)
c.norm(1,dim=0)
c.norm(2,dim=0)

在这里插入图片描述
mean, sum, min, max, prod,argmin, argmax

a =torch.arange(8).view(2,4).float()
a
a.min(),a.max(),a.mean(),a.prod()
a.sum()
a.argmax(),a.argmin()#返回索引位置

image-20230220170104467

a=torch.rand(2,3,4)
a
a.argmax()
a=torch.randn(4,10)
a
a.argmax()
a.argmax(dim=1)#相应维度上索引

在这里插入图片描述
dim, keepdim

a
a.max(dim=1)
a.max(dim=1,keepdim=True)#保持输出的维度

在这里插入图片描述
Top-k or k-th

a
a.topk(3,dim=1)
a.topk(3,dim=1,largest=False)#1维最小的前3个
a.kthvalue(8,dim=1)
a.kthvalue(3)#默认选择最后一个维度

在这里插入图片描述
compare

a
a>0
torch.gt(a,0)
(a>0).type()
torch.gt(a,0)
a!=0
a=torch.ones(2,3)
b=torch.randn(2,3)
torch.eq(a,b)
torch.eq(a,a)#逐元素比较
torch.equal(a,a)# 张量比较

在这里插入图片描述

10.高阶OP

Where
在这里插入图片描述
condition成立则填充x否则填充y

cond = torch.rand(2,2)
cond
a =torch.zeros(2,2)
b =torch.ones(2,2)
a,b
torch.where(cond>0.5,a,b)
torch.where(cond<0.5,a,b)

在这里插入图片描述
gather
image-20230220173242533image-20230220173912124

  • 输入input和索引index不会Broadcast
  • output形状与index相同
  • 索引与dim方向相同
  • index.size(dim)<=input.size(dim),保证有元素可取
prob = torch.randn(4,10)
prob
idx = prob.topk(dim=1,k=3)
idx
idx=idx[1]
idx
label = torch.arange(10)+100
label 
torch.gather(label.expand(4,10),dim=1,index=idx.long())

在这里插入图片描述

#其中有
label.expand(4,10)
idx.long()

在这里插入图片描述

http://www.lryc.cn/news/13697.html

相关文章:

  • 独居老人一键式报警器
  • 软考案例分析题精选
  • 基于SpringBoot+vue的无偿献血后台管理系统
  • 详解js在事件中,如何传递复杂数据类型(数组,对象,函数)
  • 高并发架构 第一章大型网站数据演化——核心解释与说明。大型网站技术架构——核心原理与案例分析
  • VPP接口INPUT节点运行数据
  • RabbitMQ学习(九):延迟队列
  • TCP并发服务器(多进程与多线程)
  • 第1章 Memcached 教程
  • 【2022.12.9】Lammps+Python 在计算g6(r)时遇到的问题
  • MySQL使用C语言连接
  • JavaScript随手笔记---比较两个数组差异
  • 【C++修炼之路】21.红黑树封装map和set
  • 下载ojdbc14.jar的10.2.0.1.0版本的包
  • 关于欧拉角你需要知道几个点
  • git ssh配置
  • Linux进程概念(三)
  • 新手福利——x64逆向基础
  • 虚幻c++中的细节之枚举类型(enum)
  • 判断某个字符串在另一个字符串中的个数
  • 测试人员如何运用好OKR
  • CentOS7 Hive2.3.9 安装部署(mysql 8.0)
  • 【PTA Advanced】1142 Maximal Clique(C++)
  • 1. MySQL在金融互联网行业的企业级安装部署
  • 【C++修炼之路】19.AVL树
  • 项目管理工具dhtmlxGantt甘特图入门教程(十):服务器端数据集成(下)
  • LeetCode 793. 阶乘函数后 K 个零
  • maven打包顺序与jvm类加载顺序
  • ④ 链表
  • 小孩扁桃体肿大3度能自愈吗?6岁小孩扁桃体肥大怎么治效果好?