基本数据类型和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. Codeforces 1176A Divide it!

    题目链接:http://codeforces.com/problemset/problem/1176/A 思路:贪心,对第二个操作进行俩次等于将n变成n/3,第三个操作同理,我们将n不断除以2,再除以 ...

  2. HDFS API 操作实例(二) 目录操作

    1. 递归读取文件名 1.1 递归实现读取文件名(scala + listFiles) /** * 实现:listFiles方法 * 迭代列出文件夹下的文件,只能列出文件 * 通过fs的listFil ...

  3. RHEL7中网卡绑定team和bond的区别

    red hat 官方给出的team和bond特性对比 A Comparison of Features in Bonding and Team Feature Bonding Team broadca ...

  4. USACO2008 Cow Cars /// oj23323

    题目大意: N (1 ≤ N ≤ 50,000)头牛被编号为1-N,牛i可以在M(1 ≤ M ≤ N)条不同的高速路上以Si (1 ≤ Si ≤ 1,000,000) km/h的速度飞驰 为了避免相撞 ...

  5. C 终端输入 字符123 输出 10进制123

    #include <stdio.h> #define N 20 int main(int argc, const char *argv[]) { char a[N] = {'\0'}; i ...

  6. sikuli+eclipse对于安卓app自动化测试的应用(第一次写博客,有些语言还不太专业,望海涵)

    Sikuli是什么? 下面是来自于官网的介绍:Sikuli is a visual technology to automate and test graphical user interfaces ...

  7. es5 JSON对象

    1. JSON.stringify(obj/arr) js对象(数组)转换为json对象(数组) 2. JSON.parse(json) json对象(数组)转换为js对象(数组) <!DOCT ...

  8. day34 异常处理、断言、socket之ftp协议

    Python之路,Day20 = 异常处理.断言.socket之ftp协议 参考博客:http://www.cnblogs.com/metianzing/articles/7148191.html 异 ...

  9. 0929CSP-S模拟测试赛后总结

    70分31名滚粗. 赛后发现赛时得到的分数全都是暴力分…… T2打的三分跑都没跑……边界设错了……赛后稍微调了调多了15分…… 据说有15分的暴力分,那么另外15分就是只有一种选择的情况了…… (如果 ...

  10. 夏令营501-511NOIP训练16——数字转换

    传送门:QAQQAQ 题意:如果一个数x的约数和(不包括它本身,下同)比它本身小,那么x可以变成它的约数和:如果对于某个y>x且y的约数和为x,那么x也可以变成y.例如,4可以变为3,1可以变为 ...