pytorch基础(1)
基本数据类型和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)的更多相关文章
- [人工智能]Pytorch基础
PyTorch基础 摘抄自<深度学习之Pytorch>. Tensor(张量) PyTorch里面处理的最基本的操作对象就是Tensor,表示的是一个多维矩阵,比如零维矩阵就是一个点,一维 ...
- 【新生学习】第一周:深度学习及pytorch基础
DEADLINE: 2020-07-25 22:00 写在最前面: 本课程的主要思路还是要求大家大量练习 pytorch 代码,在写代码的过程中掌握深度学习的各类算法,希望大家能够坚持练习,相信经度过 ...
- pytorch基础学习(二)
在神经网络训练时,还涉及到一些tricks,如网络权重的初始化方法,优化器种类(权重更新),图片预处理等,继续填坑. 1. 神经网络初始化(Network Initialization ) 1.1 初 ...
- PyTorch基础——词向量(Word Vector)技术
一.介绍 内容 将接触现代 NLP 技术的基础:词向量技术. 第一个是构建一个简单的 N-Gram 语言模型,它可以根据 N 个历史词汇预测下一个单词,从而得到每一个单词的向量表示. 第二个将接触到现 ...
- pytorch 基础内容
一些基础的操作: import torch as th a=th.rand(3,4) #随机数,维度为3,4的tensor b=th.rand(4)print(a)print(b) a+b tenso ...
- Pytorch 基础
Pytorch 1.0.0 学习笔记: Pytorch 的学习可以参考:Welcome to PyTorch Tutorials Pytorch 是什么? 快速上手 Pytorch! Tensors( ...
- pytorch基础教程1
0.迅速入门:根据上一个博客先安装好,然后终端python进入,import torch ******************************************************* ...
- 【pytorch】pytorch基础学习
目录 1. 前言 # 2. Deep Learning with PyTorch: A 60 Minute Blitz 2.1 base operations 2.2 train a classifi ...
- Pytorch基础(6)----参数初始化
一.使用Numpy初始化:[直接对Tensor操作] 对Sequential模型的参数进行修改: import numpy as np import torch from torch import n ...
- pytorch基础学习(一)
在炼丹师的路上越走越远,开始入手pytorch框架的学习,越炼越熟吧... 1. 张量的创建和操作 创建为初始化矩阵,并初始化 a = torch.empty(, ) #创建一个5*3的未初始化矩阵 ...
随机推荐
- 剑指offer——16二进制中1的个数
题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 有可能引起死循环解法: 每次判断最右端是不是1[与 & 1即可],是就cnt++,然后右移一位,直到num为0,结束 ...
- 剑指offer——09青蛙跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 题解: 说俗一点,就是找规律: 不不,首先,我们分析一下,青蛙第一 ...
- Python 文件处理一
1.路径下所有文件(不包含子文件) import os dirs = os.listdir(path) 注:dirs 是一个list 2.遍历路径下所有文件 def file_name(file_di ...
- Git创建本地库过程
- arm-linux-strip 的使用
3.2.1 1. 移除所有的符号信息 [arm@localhost gcc]#cp hello hello1 [arm@localhost gcc]#armlinuxstrip strip ...
- PHP算法之字符串转换整数 (atoi)
请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之 ...
- arc098E Range Minimum Queries
题意:给你一个n个数的数组,每次能够选取连续的长度为K的子序列,取出其中任意一个最小元素. 一共操作Q次.问取出的元素中Max-Min最小是多少? 标程: #include<bits/stdc+ ...
- Python 读取本地*.txt文件 替换 内容 并保存
# r 以只读的方式打开文件,文件的描述符放在文件的开头# w 打开一个文件只用于写入,如果该文件已经存在会覆盖,如果不存在则创建新文件 #路径path = r"D:\pytho ...
- PHP curl采集
if (function_exists('curl_init')) { //检查函数是否存在 $url = "http://***.com/"; $ch = curl_init() ...
- input、textarea等输入框输入中文时,拼音在输入框内会触发input事件的问题
监听文本输入框的input事件,在拼写汉字(输入法)但汉字并未实际填充到文本框中(选词)时会触发input事件,如图: 但是在很多情况下,只需要输入到输入框的中文字符. 解决办法: 通过查阅资料得知在 ...