基本数据类型和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([, , , ])

pytorch基础(1)的更多相关文章

  1. [人工智能]Pytorch基础

    PyTorch基础 摘抄自<深度学习之Pytorch>. Tensor(张量) PyTorch里面处理的最基本的操作对象就是Tensor,表示的是一个多维矩阵,比如零维矩阵就是一个点,一维 ...

  2. 【新生学习】第一周:深度学习及pytorch基础

    DEADLINE: 2020-07-25 22:00 写在最前面: 本课程的主要思路还是要求大家大量练习 pytorch 代码,在写代码的过程中掌握深度学习的各类算法,希望大家能够坚持练习,相信经度过 ...

  3. pytorch基础学习(二)

    在神经网络训练时,还涉及到一些tricks,如网络权重的初始化方法,优化器种类(权重更新),图片预处理等,继续填坑. 1. 神经网络初始化(Network Initialization ) 1.1 初 ...

  4. PyTorch基础——词向量(Word Vector)技术

    一.介绍 内容 将接触现代 NLP 技术的基础:词向量技术. 第一个是构建一个简单的 N-Gram 语言模型,它可以根据 N 个历史词汇预测下一个单词,从而得到每一个单词的向量表示. 第二个将接触到现 ...

  5. pytorch 基础内容

    一些基础的操作: import torch as th a=th.rand(3,4) #随机数,维度为3,4的tensor b=th.rand(4)print(a)print(b) a+b tenso ...

  6. Pytorch 基础

    Pytorch 1.0.0 学习笔记: Pytorch 的学习可以参考:Welcome to PyTorch Tutorials Pytorch 是什么? 快速上手 Pytorch! Tensors( ...

  7. pytorch基础教程1

    0.迅速入门:根据上一个博客先安装好,然后终端python进入,import torch ******************************************************* ...

  8. 【pytorch】pytorch基础学习

    目录 1. 前言 # 2. Deep Learning with PyTorch: A 60 Minute Blitz 2.1 base operations 2.2 train a classifi ...

  9. Pytorch基础(6)----参数初始化

    一.使用Numpy初始化:[直接对Tensor操作] 对Sequential模型的参数进行修改: import numpy as np import torch from torch import n ...

  10. pytorch基础学习(一)

    在炼丹师的路上越走越远,开始入手pytorch框架的学习,越炼越熟吧... 1. 张量的创建和操作 创建为初始化矩阵,并初始化 a = torch.empty(, ) #创建一个5*3的未初始化矩阵 ...

随机推荐

  1. 天道神诀---防火墙以及selinux(上篇)

    Linux防火墙 linux6.x 防火墙会影响通信,默认是拒绝所有. [root@redhat6 sysconfig]# chkconfig iptables --listiptables      ...

  2. SpringCloud学习笔记《---04 Feign---》基础篇

  3. Mysql优化-索引

    1. 索引的本质 MySQL官方对索引的定义为:索引是帮助MySQL高效获取数据的数据结构. 数据库查询是数据库的最主要功能之一.我们都希望查询数据的速度尽可能的快,因此 数据库系统的设计者会从查询算 ...

  4. Android开发 retrofit入门讲解

    前言 retrofit基于okhttp封装的网络请求框架,网络请求的工作本质上是 OkHttp 完成,而 retrofit 仅负责网络请求接口的封装.如果你不了解OKhttp建议你还是先了解它在来学习 ...

  5. Binary XML file line #23: Error inflating class android.widget.TextView

    分析一波,报错23行TextView的问题,但是检查了xml没有发现23行又TextView相关代码,就不应该继续纠结xml了,代码是通过R文件拿到xml资源的,你就应该怀疑是R文件的问题,R文件编译 ...

  6. eclipse总结source folder和Deployment Assembly部署

    在src下创建多级目录 然后右键build path-->use as source folder 就可以直接将多级普通文件夹转换成source folder build path下也可以直接n ...

  7. WebAPI介绍

    Web API介绍 API的概念 API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访 ...

  8. win 7 下安装GIT(亲测有效)

    我首先是百度到了这个网站:https://git-scm.com/download/win 当然由于外网访问速度的缓慢 可以直接在百度搜索下载自己对应的版本 这个网站上有下载链接,你可以根据你的系统选 ...

  9. jeecms jeecmsv93建库

    create tablespace jeecms93 datafile 'jeecms93.dbf' size 100M reuse autoextend on next 50M;1. 2.drop ...

  10. Classpath in jar关于java加载第三方jar的集中方法和详细解释。

    转载地址:http://www.iteye.com/topic/332580 大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个ja ...