基本数据类型和tensor

 import torch
import numpy as np #array 和 tensor的转换
array = np.array([1.1,,])
tensorArray = torch.from_numpy(array) #array对象变为tensor对象
array1 = tensorArray.numpy()#tensor对象变为array对象
print(array,'\t', tensorArray, '\t', array1 ) #torch拥有和numpy一样的处理数据的能力
print(torch.sin(tensorArray))
print(np.ones([,]))#两行五列
print(np.ones())#一行两个数字
a = torch.randn(, )#两行三列的正态分布
print(a)
print(a.size(),a.size(),a.shape[])#,, 0代表行,1代表对应的列数
print(a.shape)#torch.Size([,])
print(a.type())#torch.FloatTensor
isinstance(a, torch.DoubleTensor)#false
isinstance(a, torch.FloatTensor)#true
a1 = a.cuda()
print(isinstance(a1,torch.FloatTensor))#false
print(isinstance(a1,torch.cuda.FloatTensor))#true,#torch里面的数据不同于torch.cuda里面的数据 #torch的tensor对象
tensor1 = torch.tensor()
print(tensor1)#tensor()
tensor2 = torch.tensor(1.2)
print(tensor2)#tensor(1.2000)
print(tensor2.shape)#torch.Size([])
print(len(tensor2.shape))#,当tensor只是一个数字的时候,他的维度是0,所以他的size是[],shape为0
tensor3 = torch.tensor([1.1])#一维的列表,所以输出维度是1
print(tensor3,tensor3.shape)#tensor([1.1000]) torch.Size([])
tensor4 = torch.FloatTensor()#注意此时1代表随机返回一个FloatTensor对象
print(tensor4)#tensor([1.1000])
tensor5 = torch.FloatTensor()#考虑一下tensor和FloatTensor的差别
print(tensor5)#tensor([0.0000e+00, 0.0000e+00, 6.8645e+36])

切片

 import torch
import numpy as np #tensor和随机数
a = torch.rand(, , , )
print(a, a.shape)#随机生成一个2***28的四维矩阵(可以看成声明一个四维矩阵),torch.Size([, , , ])
#四维适合做CNN ,三维适合RNN,二维适合batch
print(a.numel())#,计算元素个数
print(a.dim())#
print(torch.tensor().dim())#,
print(torch.empty())#一维数字0,tensor([.])
print(torch.Tensor(,).type())#默认是torch.FloatTensor
print(torch.IntTensor(,))#*
print(torch.tensor([,]).type())#torch.LongTensor
print(torch.tensor([1.2,]).type())#torch.FloatTensor
print(torch.rand(,))#取值范围为0到1之间
print(torch.rand_like(torch.rand(,)))#rand_like直接继承了参数的行和列,生成3*3的0到1之间的随机矩阵
print(torch.randint(,,(,)))#取值在1到10之间(左闭右开)大小为3*3的矩阵
print(torch.randn(,))#*3矩阵,服从均值为0,方差为1的正态分布
print(torch.normal(mean=torch.full([],),std = torch.arange(,,-0.1)))#均值为0方差递减的10*1一维矩阵
print(torch.full([,],))#*3全为7的二维矩阵
print(torch.full([],))#数字7维度0
print([],)#一维1*1矩阵元素为7
print(torch.logspace(,,steps=))#log(^)到log(^)中间取10个数 #切片
a = torch.rand(,,,)
print(a[].shape)#torch.Size([,,])
print(a[,].shape)#torch.Size([, ])
print(a[,,,])#tensor(0.6186)
print(a[:].shape)#torch.Size([, , , ])
print(a[:,:,:,:].shape)#torch.Size([, , , ])
print(a[:,-:,:,:].shape)#torch.Size([, , , ]) print(a[:,:,::,::].shape)#torch.Size([, , , ])
print(a[:,:,::,::].shape)#torch.Size([, , , ])
print(a.index_select(,torch.arange()).shape)#torch.Size([, , , ]) x = torch.randn(,)
mask = x.ge(0.5)#比0.5大的标记为true
print(mask)
torch.masked_select(x,mask)#把为true的选择出来
torch.masked_select(x,mask).shape src =torch.tensor([[,,],[,,]])
taa = torch.take(src, torch.tensor([,,]))#展平后按照位置选数据
print(taa)

维度变换

 import torch
import numpy as np
#维度变化
#View reshape
a = torch.rand(,,,)#四张图片,通道数是1,长宽是28*
print(a.shape)#torch.Size([, , , ])
print(a.view(,**).shape)#torch.Size([, ]),把后三维展成一行
print(a.view(*,).shape)#torch.Size([, ])变成112行28列的二维数据
print(a.view(*,,).shape)#torch.Size([, , ])要理解对应的图片的物理意义
b = a.view(,)
print(b.view(,,,).shape)#torch.Size([, , , ]),b变成的数据不是a(一定要注意)
#print(a.view(,))#尺寸不一致会报错 #unsqueeze,增加维度,但不会影响数据的变化
#数据的范围是[-a.dim()-,a.dim()+)
print()#下面例子是[-,)
print(a.unsqueeze().shape)#torch.Size([, , , , ])
print(a.unsqueeze(-).shape)#torch.Size([, , , , ])
print(a.unsqueeze().shape)#torch.Size([, , , , ])
print(a.unsqueeze(-).shape)#torch.Size([, , , , ])
print(a.unsqueeze(-).shape)#torch.Size([, , , , ])
#print(a.unsqueeze().shape)
a = torch.tensor([1.2,2.3])#a的shape是[]
print(a.unsqueeze(-))#tensor([[1.2000],
#[2.3000]])变成2行一列
print(a.unsqueeze())#tensor([[1.2000, 2.3000]])#shape变成[,],即一行二列
b = torch.rand()
f = torch.rand(,,,)
b = b.unsqueeze().unsqueeze().unsqueeze()#torch.Size([, , , ])
print(b.shape) #维度减少
print()
print(b.shape)#torch.Size([, , , ])
print(b.squeeze().shape)#torch.Size([]),所有为1的被挤压
print(b.squeeze(-).shape)#torch.Size([, , ])
print(b.squeeze().shape)#torch.Size([, , ])
print(b.squeeze().shape)#torch.Size([, , , ]),因为不等于1 所以没有被挤压
print(b.squeeze(-).shape)#torch.Size([, , ]) #expand扩展数据,进行数据拷贝,但不会主动复制数据,只会在需要的时候复制,推荐使用
print()
print(b.shape)#torch.Size([, , , ])
print(b.expand(,,,).shape)#torch.Size([, , , ]),只能对维度是1 的进行扩展
print(b.expand(-,,-,-).shape)#torch.Size([, , , ]),其他维度为-,这样可以进行原维度不是一的进行扩展同样大小的维度
print(b.expand(-,,-,-).shape)#torch.Size([, , , -]) -4是无意义的 #repeat表示在原来维度上拷贝多少次,而不是扩展到多少,这个方法申请了新的空间,对空间使用加大
print()
print(b.shape)#torch.Size([, , , ])
print(b.repeat(,,,).shape)#torch.Size([, , , ]),第二维表示拷贝愿来的32倍
print(b.repeat(,,,).shape)#torch.Size([, , , ])
print(b.repeat(,,,).shape)#torch.Size([, , , ]) #transpose实现指定维度之间的交换
a = torch.rand(,,,)
print(a.shape)#torch.Size([, , , ])
a1 = a.transpose(,).contiguous().view(,**).view(,,,).transpose(,)
print(a1.shape)#torch.Size([, , , ])
print(torch.all(torch.eq(a,a1)))#tensor(True) #premute实现指定维度位置交换到指定位置
print(a.permute(,,,).shape)#torch.Size([, , , ])

最新文章

  1. 未解决的问题,登录163邮箱http://mail.163.com/,用xpath的方式定位密码输入框的时候,总是报找不到该元素
  2. 纯JS实现中国行政区域上下联动选择地址
  3. android wifi ANR问题分析总结
  4. Hibernate-二级缓存 sessionFactory
  5. Azure Blob Storage从入门到精通
  6. NOIP2016 D1T1 玩具迷題(toy)
  7. 上机实践 - - 一个例子了解C/C++中指针与数组的区别
  8. php in_array比较原理和类型比较问题
  9. 工具类_java 数字转化为汉字大写
  10. POJ1062 昂贵的聘礼 【DFS】
  11. NanoApe Loves Sequence Ⅱ(尺取法)
  12. Java架构师系统培训高并发分布式电商实战activemq,netty,nginx,redis dubbo shiro jvm虚拟机视频教程下载
  13. php获取指定目录下的所有文件列表
  14. Docker & ASP.NET Core (2):定制Docker镜像
  15. listener介绍
  16. spring+shiro共享session完整小例子
  17. PowerDesigner最基础的使用方法入门学习(二)
  18. [jQ]jQuery显式操作Checkbox,并用数组存储关联值的方案
  19. Spring IOC(二)beanName 别名管理
  20. NoSQL -- Redis使用

热门文章

  1. 【JUC】JDK1.8源码分析之ConcurrentHashMap
  2. Metasploit 如何使用Exploits(漏洞)
  3. sklearn 调用逻辑回归函数训练数据时出现 “unknown label type:unknown”
  4. 第一周课堂笔记1th
  5. 精选15个国外CSS框架
  6. CTR预估的常用方法
  7. range和arange
  8. Iview+Vue CDN NetMvC 简单demo
  9. PSCC2019常用基础操作
  10. PAT甲级——A1107 Social Clusters