当前位置:网站首页>Pytorch学习(四)
Pytorch学习(四)
2022-07-04 17:54:00 【马少爷】
1、range和arange
创建1维张量
这里给出一段示例:
z = torch.range(1,10)
print(z,z.shape)
z = torch.arange(1,10)
print(z,z.shape)
torch.range(1,10)和 torch.arange(10)都产生了一个1维的数组,类型是 <class ‘torch.Tensor’>
二者不同的是
range产生的长度是10-1+1=10 是由1到10组成的1维张量,类型float
而arange产生的是10-1=9 由1-9组成的1维度张量 ,类型int
他们的输出分别是
需要注意的是,range第一个值不能缺省,arange可以,默认0,其输出就是:tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) torch.Size([10])
# z = torch.range(10)
# print(z,z.shape)
z = torch.arange(10)
print(z,z.shape)

二、repeat复制张量
z = torch.arange(10)
print(z,z.shape)
z = torch.arange(10).repeat(10,1)
print(z,z.shape)

z = torch.arange(10).repeat(10,2,1)
三、View:改变张量形状
View的机制顺序地拿数据填充形状,注意,定义的形状所需要的数据的数量必须=能提供的数据数量
比如:[10x10] 的张量可以.view(1,1,10,10) 也可以.view(5,20) 但是不可以.view(10,11) ,一旦数据数量不同,就会报数据无效的错误
代码示例
z = torch.arange(10)
print(z,z.shape)
z = torch.arange(10).repeat(10,1)
print(z,z.shape)
z = torch.arange(10).repeat(10,1).view(1,1,10,10)
print(z,z.shape)

z = torch.arange(10).view(1,10).repeat(10,2)
print(z,z.shape)

四、Concat和add操作
Concat:张量拼接,会扩充两个张量的维度,
add:张量相加,张量直接相加,不会扩充维度。
一般情况下,feature maps的结合有两种方法,一种是元素对应相加,简称add,另一种就是把特征图堆到一起来,简称concatenate。
假设feature map 1 的维度为B1∗ C1 ∗ H1 ∗ W1
feature map 2 的维度为B2 ∗ C2 ∗ H2 ∗ W2
1)在add情况下,就是两个四维矩阵的按元素相加,那么这时候我们需要两个矩阵维度全部相等。并且相加后矩阵维度不变。
例如26 * 26 * 256和26 * 26 * 256相加,结果还是26 * 26 * 256
2)在concatenate情况下,我们把两个矩阵在某个维度叠加起来,这要求在这个连接的维度上可以不同,但是在其他维度上必须相等。叠加后,某个维度会增加,是两个矩阵上的某个维度相加。比如,我们在Channel这个维度上连接两个矩阵,那么新的矩阵维度是B2 ∗ ( C2 + C1 )∗ H2 ∗ W2
例如26 * 26 * 256和26 * 26 * 512相加,结果是26 * 26 * 768
边栏推荐
- 2022 ByteDance daily practice experience (Tiktok)
- Scala basic tutorial -- 12 -- Reading and writing data
- 其他InterSystems %Net工具
- DeFi生态NFT流动性挖矿系统开发搭建
- 千万不要只学 Oracle、MySQL!
- The kth largest element in the array
- Shell programming core technology "four"
- 神经网络物联网应用技术就业前景【欢迎补充】
- [发布] 一个测试 WebService 和数据库连接的工具 - DBTest v1.0
- 数组中的第K个最大元素
猜你喜欢

Scala basic tutorial -- 17 -- Collection

“只跑一趟”,小区装维任务主动推荐探索

神经网络物联网应用技术学什么

升级智能开关,“零火版”、“单火”接线方式差异有多大?

Don't just learn Oracle and MySQL!

The latest progress of Intel Integrated Optoelectronics Research promotes the progress of CO packaging optics and optical interconnection technology

2022 ByteDance daily practice experience (Tiktok)

自由小兵儿

读写关闭的channel是啥后果?

2022CoCa: Contrastive Captioners are Image-Text Fountion Models
随机推荐
Stream流
Using SSH
读写关闭的channel是啥后果?
From automation to digital twins, what can Tupo do?
Basic tutorial of scala -- 16 -- generics
联想首次详解绿色智城数字孪生平台 破解城市双碳升级难点
Scala basic tutorial -- 13 -- advanced function
神经网络物联网平台搭建(物联网平台搭建实战教程)
IBM WebSphere MQ retrieving messages
《看完就懂系列》字符串截取方法substr() 、 slice() 和 substring()之间的区别和用法
千万不要只学 Oracle、MySQL!
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
Using FTP
Scala basic tutorial -- 12 -- Reading and writing data
The CDC of sqlserver can read the data for the first time, but it can't read the data after adding, deleting and modifying. What's the reason
删除字符串中出现次数最少的字符【JS,Map排序,正则】
Scala basic tutorial -- 14 -- implicit conversion
Send and receive IBM WebSphere MQ messages
国元期货是正规平台吗?在国元期货开户安全吗?
Build your own website (15)