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

橙色在网站中的应用中国今日新闻

橙色在网站中的应用,中国今日新闻,怎么制作自己的微信小程序,网站开发是做什么的张量作为有序的序列,也是具备数值索引的功能,并且基本索引方法和python原生的列表、numpy中的数组基本一致。 不同的是,pytorch中还定义了一种采用函数来进行索引的方式。 作为pytorch中的基本数据类型,张量既具备了列表、数组的基…

张量作为有序的序列,也是具备数值索引的功能,并且基本索引方法和python原生的列表、numpy中的数组基本一致。

不同的是,pytorch中还定义了一种采用函数来进行索引的方式。

作为pytorch中的基本数据类型,张量既具备了列表、数组的基本功能,同时还充当向量、矩阵等重要数据结构。因此pytorch中也设置了非常晚辈的张量合并与变换的操作。

import torch	# 导入torch
import numpy as np	# 导入numpy

6 张量的符号索引

6.1 一维张量索引

一维张量的索引过程和python原生对象类型的索引一致,基本格式遵循[start: end: step]

t1 = torch.arange(1, 11)	# 创建一维张量

从左到右,从零开始

t1[0]
# output : tensor(1)

**注:**张量索引出来的结果还是零维张量,而不是单独的数。

​ 要转化成单独的数,需要使用.item()方法


冒号分割,表示对某个区域进行索引,也就是所谓的切片

t1[1: 8]	# 索引其中2-9号元素,并且左闭右开
# output : tensor([2, 3, 4, 5, 6, 7, 8])

第二个冒号,表示索引的间隔

t1[1: 8: 2]		# 第三个参数表示每两个数取一个
# output : tensor([2, 4, 6, 8])

冒号前后没有值,表示索引这个区域

t1[1: : 2]		# 从第二个元素开始索引,一致到结尾,并且每隔两个取一个
# output : tensor([ 2,  4,  6,  8, 10])
t1[: 8: 2]		#从第一个元素开始索引到第九个元素(不包含),并且每隔两个数取一个
# output : tensor([1, 3, 5, 7])

在张量的索引中,step位必须大于0,也就是说不能逆序取数。


6.2 二维张量索引

二维张量的索引逻辑和一维张量基本相同,二维张量可以视为两个一维张量组合而成。

在实际的索引过程中,需要用逗号进行分割,表示分别对哪个一维张量进行索引、以及具体的一维张量的索引。

t2 = torch.arange(1, 10).reshape(3, 3)		# 创建二维张量
t2[0, 1]	# 表示索引第一行、第二列的元素
# output : tensor(2)
t2[0, : : 2]	# 表示索引第一行、每隔两个元素取一个
# output : tensor([1, 3])
t2[0, [0, 2]]	# 索引结果同上
t2[: : 2, : : 2]	# 表示每隔两行取一行、并且每一行中每隔两个元素取一个
# output : 
tensor([[1, 3],[7, 9]])
t2[[0, 2], 1]	# 索引第一行、第三行、第二列的元素
# output : tensor([2, 8])

6.3 三维张量索引

我们可以将三维张量视作矩阵组成的序列,则在索引过程中拥有三个维度,分别是索引矩阵,索引矩阵的行、索引矩阵的列。

t3 = torch.arange(1, 28).reshape(3, 3, 3)	# 创建三维张量
t3[1, 1, 1]		# 索引第二个矩阵中,第二行、第二个元素
# output : tensor(14)
t3[1, : : 2, : : 2]		#索引第二个矩阵,行和列都是每隔两个取一个
# output : 
tensor([[10, 12],[16, 18]])
# 每隔两个取一个矩阵,对于每个矩阵来说,行和列都是每隔两个取一个
t3[: : 2, : : 2, : : 2]		
# output : 
tensor([[[ 1,  3],[ 7,  9]],[[19, 21],[25, 27]]])

7 张量的函数索引

pytorch中,我们还可以使用index_select函数,通过指定index来对张量进行索引。

t1 = torch.arange(1, 11)
indices = torch.tensor([1, 2])
torch.index_select(t1, 0, indices)
# output : tensor([2, 3])

第二个参数dim代表索引的维度。

对于t1这个一维向量来说,由于只有一个维度,因此第二个参数化取值为0,代表在第一个维度上进行索引。


t2 = torch.arange(12).reshape(4,3)
t2
# output :
tensor([[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11]])
indices = torch.tensor([1, 2])# dim参数取值为0,代表在shape的第一个维度上索引
torch.index_select(t2, 0, indices)	
# output : 
tensor([[3, 4, 5],[6, 7, 8]])# dim参数取值为0,代表在shape的第二个维度上索引
torch.index_select(t2, 1, indices)	
# output : 
tensor([[ 1,  2],[ 4,  5],[ 7,  8],[10, 11]])

8 tensor.view()方法

该方法会返回一个类似视图的结果,且该结果会和原张量对象共享一块数据存储空间

通过.view()方法,还可以改变对象结构,生成一个不同结构、但共享一个存储空间的张量。

t = torch.arange(6).reshape(2, 3)
t
# output :
tensor([[0, 1, 2],[3, 4, 5]])
# 构建一个数据相同,但形状不同的“视图”
te = t.view(3, 2)	
te
# output :
tensor([[0, 1],[2, 3],[4, 5]])

当然,共享一个存储空间,也就代表二者是浅拷贝的关系,修改其中一个,另一个也会同步更改。

t[0] = 1
te
# output :
tensor([[1, 1],[1, 3],[4, 5]])

当然,维度也可以修改

tr = t.view(1, 2, 3)
tr
# output :
tensor([[[1, 1, 1],[3, 4, 5]]])

视图的作用就是节省空间,在接下来介绍的很多切分张量的方法中,返回结果都是“视图”,而不是新生成一个对象。


9 张量的分片函数

9.1 分块:chunk函数

chunk函数能够按照某维度,对张量进行均匀切分,返回结果是原张量的视图

t2 = torch.arange(12).reshape(4, 3)
t2
# output :
tensor([[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11]])
# 在第零个维度上,按行进行四等分
tc = torch.chunk(t2, 4, dim = 0)
tc
# output :
(tensor([[0, 1, 2]]),tensor([[3, 4, 5]]),tensor([[6, 7, 8]]),tensor([[ 9, 10, 11]]))

注:chunk返回结果是一个视图,不是新生成了一个对象

tc[0][0][0] = 1		# 修改tc中的值
t2
# output :
tensor([[ 1,  1,  2],[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11]])

当原张量不能均分时,chunk不会报错,但会返回其他均分结果。

torch.chunk(t2, 3, dim = 0)	# 返回次一级均分结果
# output :
(tensor([[1, 1, 2],[3, 4, 5]]),tensor([[ 6,  7,  8],[ 9, 10, 11]]))
torch.chunk(t2, 5, dim = 0)	# 返回次一级均分结果
# output :
(tensor([[1, 1, 2]]),tensor([[3, 4, 5]]),tensor([[6, 7, 8]]),tensor([[ 9, 10, 11]]))

9.2 拆分 :split函数

split既能进行均分,也能自定义切分

t2 = torch.arange(12).reshape(4, 3)
t2
# output :
tensor([[ 0,  1,  2],[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11]])

第二个参数只输入一个数值时表示均分,第三个参数表示按第几个维度进行切分

torch.split(t2, 2, 0)
# output :
(tensor([[1, 1, 2],[3, 4, 5]]),tensor([[ 6,  7,  8],[ 9, 10, 11]]))

第二个参数输入一个序列时,表示按照序列数值进行切分

torch.split(t2, [1, 3], 0)
# output :
(tensor([[1, 1, 2]]),tensor([[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11]]))

当第二个参数输入一个序列时,序列的各数值的和必须等于对于维度下形状分量的取值。

例如,上述代码中是按照第一个维度进行切分,第一个维度有四行,因此序列的求和必须等于4,也就是1 + 3 = 4


序列中每个分量的取值表示切块大小

torch.split(t2,[1, 1, 1, 1], 0)
# output :
(tensor([[1, 1, 2]]),tensor([[3, 4, 5]]),tensor([[6, 7, 8]]),tensor([[ 9, 10, 11]]))
torch.split(t2,[1, 2], 1)
# output :
(tensor([[1],[3],[6],[9]]),tensor([[ 1,  2],[ 4,  5],[ 7,  8],[10, 11]]))

当然,split函数返回结果也是view

ts = torch.split(t2,[1, 2], 1)
ts[0][0] = 1
t2
# output :
tensor([[ 1,  1,  2],[ 3,  4,  5],[ 6,  7,  8],[ 9, 10, 11]])

10 张量的合并操作

张量的合并操作类似列表的追加元素,可以拼接、也可以堆叠。

拼接函数:cat

a = torch.zeros(2, 3)
b = torch.ones(2, 3)
c = torch.zeros(3, 3)
# dim默认取值为0,按行进行拼接
torch.cat([a, b])	
# output :
tensor([[0., 0., 0.],[0., 0., 0.],[1., 1., 1.],[1., 1., 1.]])
# 按列进行拼接
torch.cat([a, b], 1)	
# output :
tensor([[0., 0., 0., 1., 1., 1.],[0., 0., 0., 1., 1., 1.]])
# 形状不匹配时将报错
torch.cat([a, c], 1)
# output :
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 2 but got size 3 for tensor number 1 in the list.

拼接的本质是实现元素的堆积,也就是构成a、b两个二维张量的各一维张量的堆积,最终还是构成二维向量


堆叠函数:stack

a = torch.zeros(2, 3)
b = torch.ones(2, 3)
c = torch.zeros(3, 3)
# 堆叠之后,生成一个三维张量
torch.stack([a,b])
# output :
tensor([[[0., 0., 0.],[0., 0., 0.]],[[1., 1., 1.],[1., 1., 1.]]])

注意对比和**cat**函数的区别,拼接之后维度不变,堆叠之后维度升高

对于两个二维张量,拼接是把一个个元素单独提取出来之后放到二维张量中,而堆叠则是直接将两个二维张量封装到一个三维张量中。

因此,堆叠的要求更高,参与堆叠的张量必须形状完全相同

# 维度不匹配将报错
torch.stack([a, c])
# output :
RuntimeError: stack expects each tensor to be equal size, but got [2, 3] at entry 0 and [3, 3] at entry 1

11 张量维度变换

在实际操作张量进行计算时,往往需要另外进行降维和升维的操作。

squeeze函数:删除不必要的维度

t = torch.zeros(1, 1, 3, 1)
# output :
tensor([[[[0.],[0.],[0.]]]])
t.shape
# output :
torch.Size([1, 1, 3, 1])
torch.squeeze(t)
# output :
tensor([0., 0., 0.])
torch.squeeze(t).shape
# output :
torch.Size([3])

简单理解,squeeze就相对于提出了shape返回结果中的1.

t1 = torch.zeros(1, 1, 3, 2, 1, 2)
torch.squeeze(t1)
torch.squeeze(t1).shape
# output :
torch.Size([3, 2, 2])

unsqueeze函数:手动升维

t = torch.zeros(1, 2, 1, 2)
t.shape
# output :
torch.Size([1, 2, 1, 2])
# 在第1个维度索引上升高1个维度
torch.unsqueeze(t, dim = 0)
# output :
tensor([[[[[0., 0.]],[[0., 0.]]]]])
torch.unsqueeze(t, dim = 0).shape
# output :
torch.Size([1, 1, 2, 1, 2])
# 在第3个维度索引上升高1个维度
torch.unsqueeze(t, dim = 2).shape
# output :
torch.Size([1, 2, 1, 1, 2])

注意理解维度和shape返回结果一一对应的关系,shape返回的序列有多少元素,张量就有多少维度。


文章转载自:
http://opengl.bbrf.cn
http://heptanone.bbrf.cn
http://educationist.bbrf.cn
http://quantometer.bbrf.cn
http://mulligatawny.bbrf.cn
http://assimilability.bbrf.cn
http://garri.bbrf.cn
http://apotropaic.bbrf.cn
http://loup.bbrf.cn
http://lexica.bbrf.cn
http://miskolc.bbrf.cn
http://overpowering.bbrf.cn
http://primigenial.bbrf.cn
http://protectant.bbrf.cn
http://legless.bbrf.cn
http://microfluorometry.bbrf.cn
http://lungi.bbrf.cn
http://assertive.bbrf.cn
http://examples.bbrf.cn
http://monosilane.bbrf.cn
http://divulsion.bbrf.cn
http://magnoliaceous.bbrf.cn
http://dime.bbrf.cn
http://cant.bbrf.cn
http://underthings.bbrf.cn
http://unuseful.bbrf.cn
http://hessian.bbrf.cn
http://ent.bbrf.cn
http://employee.bbrf.cn
http://sensorial.bbrf.cn
http://atacama.bbrf.cn
http://marcheshvan.bbrf.cn
http://tinhorn.bbrf.cn
http://rubious.bbrf.cn
http://curule.bbrf.cn
http://overconfident.bbrf.cn
http://hemiparesis.bbrf.cn
http://isobarically.bbrf.cn
http://krypton.bbrf.cn
http://anticatalyst.bbrf.cn
http://stratigraphical.bbrf.cn
http://swage.bbrf.cn
http://accept.bbrf.cn
http://agma.bbrf.cn
http://seir.bbrf.cn
http://epaxial.bbrf.cn
http://dermatitis.bbrf.cn
http://wainwright.bbrf.cn
http://microcline.bbrf.cn
http://inconceivability.bbrf.cn
http://afge.bbrf.cn
http://abroad.bbrf.cn
http://radii.bbrf.cn
http://posttonic.bbrf.cn
http://kinneret.bbrf.cn
http://bombardon.bbrf.cn
http://backhoe.bbrf.cn
http://innumerable.bbrf.cn
http://armorer.bbrf.cn
http://organa.bbrf.cn
http://omdurman.bbrf.cn
http://kodiak.bbrf.cn
http://conakry.bbrf.cn
http://dortour.bbrf.cn
http://leisureful.bbrf.cn
http://trudy.bbrf.cn
http://subcabinet.bbrf.cn
http://tmesis.bbrf.cn
http://kinematographic.bbrf.cn
http://imitator.bbrf.cn
http://scare.bbrf.cn
http://bechamel.bbrf.cn
http://gastropod.bbrf.cn
http://budworm.bbrf.cn
http://snivel.bbrf.cn
http://emergence.bbrf.cn
http://empanel.bbrf.cn
http://yanaon.bbrf.cn
http://gellant.bbrf.cn
http://elisabeth.bbrf.cn
http://copperah.bbrf.cn
http://hyalinization.bbrf.cn
http://touse.bbrf.cn
http://caution.bbrf.cn
http://wifehood.bbrf.cn
http://begone.bbrf.cn
http://unwisdom.bbrf.cn
http://peking.bbrf.cn
http://bowed.bbrf.cn
http://hame.bbrf.cn
http://felucca.bbrf.cn
http://exocentric.bbrf.cn
http://stratosphere.bbrf.cn
http://evacuant.bbrf.cn
http://incorrigibly.bbrf.cn
http://paidology.bbrf.cn
http://calabash.bbrf.cn
http://gender.bbrf.cn
http://pirogue.bbrf.cn
http://autogestion.bbrf.cn
http://www.15wanjia.com/news/67042.html

相关文章:

  • 适合做网站背景音乐百度快速排名优化服务
  • 室内设计自学网站武汉seo推广
  • 黄岛网站建设价格广州公关公司
  • wordpress 页面导出长沙seo排名扣费
  • 伍佰亿网站建设网站优化入门
  • wordpress多站点多模板免费个人网站建设
  • php第一季网站开发实例教程sem推广竞价托管
  • 深圳建设局招标网站chrome 谷歌浏览器
  • 成都网站建设waibaoweb四川企业seo
  • 杭州网站推广方式什么搜索引擎搜索最全
  • wordpress 字体 服务器seo模拟点击有用吗
  • 深圳做网站的网怎么做百度推广
  • 电子商务网站建设考题广州seo网络推广员
  • 网站开发常用模板天津百度网站快速优化
  • 天津网页模板建站福州百度开户多少钱
  • 南昌做网站开发的公司哪家好手机网页制作
  • 政府网站官网360推广联盟
  • 做网站都需要什么贴吧今日广东头条新闻
  • 做宣传册参考的网站武汉最新今天的消息
  • wordpress 调用数据库推广优化师
  • 关于网站开发的期刊推广发帖网站
  • 做化妆品网站怎样百度邮箱登录入口
  • 淮阴区建设局网站东莞seo技术
  • 网站收录少了注册网站流程和费用
  • 网站建设费用扬中网站制作
  • 江阴青阳道路建设网站今天热搜榜前十名
  • 南京市建设工程网站上海网络营销推广外包
  • 上海建设工程咨询网证书查询seo推广公司哪家好
  • 深圳css3网站开发多少钱百度广告投放技巧
  • 网站备案问题制作一个网页的步骤