tensor

Pytorch调研笔记

随声附和 提交于 2020-01-21 16:07:51
Pytorch 训练调研 首先我们简单说明一下,这么多深度学习框架中,为什么选择PyTorrch呢? 因为PyTorch是当前难得的简洁优雅且高效快速的框架。在笔者眼里,PyTorch达到目前深度学习框架的最高水平。当前开源的框架中,没有哪一个框架能够在灵活性、易用性、速度这三个方面有两个能同时超过PyTorch。下面是许多研究人员选择PyTorch的原因。 1、简洁:PyTorch的设计追求最少的封装,尽量避免重复造轮子。不像TensorFlow中充斥着session、graph、operation、name_scope、variable、tensor、layer等全新的概念,PyTorch的设计遵循tensor→variable(autograd)→nn.Module 三个由低到高的抽象层次,分别代表高维数组(张量)、自动求导(变量)和神经网络(层/模块),而且这三个抽象之间联系紧密,可以同时进行修改和操作。 简洁的设计带来的另外一个好处就是代码易于理解。PyTorch的源码只有TensorFlow的十分之一左右,更少的抽象、更直观的设计使得PyTorch的源码十分易于阅读。 2、速度:PyTorch的灵活性不以速度为代价,在许多评测中,PyTorch的速度表现胜过TensorFlow和Keras等框架 。框架的运行速度和程序员的编码水平有极大关系,但同样的算法

pytorch tutorials

感情迁移 提交于 2020-01-20 03:12:15
pytorch 1.4.0版本的tutorials提供了三个大块的教程: deep learning with pytorch: a 60 minute blitz writing custom datasets, dataloaders and transforms visualizing models, data and training with tensorboard 根据个人所需将其分为五个篇章,作为pytorch的入门( 假设你已经有了一定的tensorflow和深度学习基础 ): 第一篇章 tensor tensor是pytorch核心的数据结构,和numpy中的ndarray的区别是,它可以在GPU上实现加速。 tensor需要了解的就是两个部分的内容:(为表述方便,用 t 表示某个具体的tensor) tensor的属性 t.shape/t.size() 创建tensor 创建tensor有很多种方法,和tensorflow中的tensor差不多 构造方法: tensor() 特殊张量: ones()/ones_like() 随机函数: randn() tensor运算 tensor的计算(包括算术计算、代数运算、维度计算),太多了,和tensorflow几乎没什么两样。 t.view() :改变tensor的shape并返回 t.item()

tf用法

拥有回忆 提交于 2020-01-19 21:20:33
------------恢复内容开始------------ tensorflow基础函数合辑 import tensorflow as tf *tf.constant* 函数定义:tf.constant(data,shape=[]) 参数说明:data--输入数据,shape--reshape后的形状。使用的reshape中的最低维度优先填充原则。 reshape可参考: http://chenjingjiu.cn/index.php/2019/07/04/numpy-reshape/ *tf.tile* tf.tile:用来对张量(Tensor)进行扩展的, 其特点是对当前张量内的数据进行一定规则的复制。最终的输出张量维度不变。 函数定义: tf.tile( input, multiples, name=None ) 示例: >>>tf.tile([1,2],[2]) [1,2,1,2] *tf.nn.conv2d* 函数定义: tf.nn.conv2d( input, filter, strides, padding, use_cudnn_on_gpu=True, data_format='NHWC', dilations=[1, 1, 1, 1], name=None ) Returns: A Tensor. 参数说明: input: 指需要做卷积的输入图像(tensor)

PyTorch图像分类

a 夏天 提交于 2020-01-19 07:11:12
目录 一、torch和torchvision 1、torchvision.datasets 2、torchvision.models 3、torchvision.transforms 4、torchvision.utils 二、MNIST手写数字识别 1、获取MNIST训练集和测试集 2、数据装载 3、数据预览 4、构建卷积神经网络模型 5、对模型进行训练和参数优化 6、对训练模型进行保存和加载 7、MNIST手写数字识别完整代码 三、CIFAR10图像分类 1、CIFAR10数据集介绍 2、CIFAR10图像分类实现 3、在GPU上跑神经网络 一、torch和torchvision PyTorch 中有两个核心的包,分别是 torch 和 torchvision 。 torch.nn 包提供了很多与实现神经网络中的具体功能相关的类,torch.optim包中提供了非常多的可实现参数自动优化的类,torch.autograd实现自动梯度的功能等。 torchvision 包含了目前流行的数据集,模型结构和常用的图片转换工具,它的主要功能是实现数据的处理、导入和预览等,所以如果需要对计算机视觉的相关问题进行处理,就可以借用在torchvision包中提供的大量的类来完成相应的工作。 1、torchvision.datasets torchvision.datasets

tensor维度变换

你说的曾经没有我的故事 提交于 2020-01-18 08:37:36
维度变换是tensorflow中的重要模块之一,前面mnist实战模块我们使用了图片数据的压平操作,它就是维度变换的应用之一。 在详解维度变换的方法之前,这里先介绍一下View(视图)的概念。所谓View,简单的可以理解成我们对一个tensor不同维度关系的认识。举个例子,一个[ b,28,28,1 ]的tensor(可以理解为mnist数据集的一组图片),对于这样一组图片,我们可以有一下几种理解方式: (1)按照物理设备储存结构,即一整行的方式(28*28)储存,这一行有连续的784个数据,这种理解方式可以用[ b,28*28 ]表示 (2)按照图片原有结构储存,即保留图片的行列关系,以28行28列的数据理解,这种方式可以用[ b,28,28 ]表示 (3)将图片分块(比如上下两部分),这种理解方式与第二种类似,只是将一张图变为两张,这种方式可以用[ b,2,14*28 ]表示 (4)增加channel通道,这种理解方式也与第二种类似,只是这种对rgb三色图区别更明显,可以用[ b,28 28,1 ]表示 通过维度的等价变换,就可以实现思维上View的转换 维度变换的方式: 方式1:tf.reshape (可通过破坏维度之间的关系改变tensor的维度,但不会改变原有数据的存储顺序) a = tf.random.normal([4,28,28,3]) print(a.shape)

PyTorch基础

蓝咒 提交于 2020-01-18 07:03:20
目录 一、PyTorch中的Tensor张量 1、Tensor张量 2、Tensor数据类型 3、Tensor常用函数 二、基于PyTorch搭建简易神经网络模型 1、简易神经网络模型 2、Pytorch自动梯度 3、使用自动梯度和自定义函数搭建简易神经网络模型 三、torch.nn和torch.optim 1、使用torch.nn搭建神经网络模型 2、使用torch.optim优化模型 一、PyTorch中的Tensor张量 1、Tensor张量 Tensor在PyTorch中负责存储基本数据,PyTorch针对Tensor也提供了相对丰富的函数和方法,所以PyTorch中的Tensor与NumPy的数组具有极高的相似性,同时 Tensor可以使用 GPU 进行计算。 2、Tensor数据类型 (1)torch.FloatTensor # 此变量用于生成数据类型为浮点型的Tensor,传递给torch.FloatTensor的参数可以是一个列表,也可以是一个维度值; import torch a=torch.FloatTensor(2,3) #生成数据类型为浮点型的Tensor print(a) b=torch.FloatTensor([2,3,4,5]) print(b) tensor([[0., 0., 0.], [0., 0., 0.]]) tensor([2., 3.,

pytorch --- tensor.permute()和torch.transpose()

我们两清 提交于 2020-01-18 03:24:29
tensor.permute(dim1, dim2, dim3, …) permute可以对任意高维矩阵进行转置.但只有 tensor.permute() 这个调用方式 x = torch . rand ( 2 , 3 , 4 ) print ( "x.shape:" , x . shape ) x = x . permute ( 2 , 1 , 0 ) print ( "x.shape:" , x . shape ) 输出: x.shape: torch.Size([2, 3, 4]) x.shape: torch.Size([4, 3, 2]) [Finished in 1.0s] 例2: t . rand ( 2 , 3 , 4 , 5 ) . permute ( 3 , 2 , 0 , 1 ) . shape Out [ 669 ] : torch . Size ( [ 5 , 4 , 2 , 3 ] ) 总结 传入permute方法的参数是维度, 未进行变换前的dim是[0, 1, 2]的方式, 转换后表示将第0维度和第2维度调换 torch.transpose(tensor, dim1, dim2) transpose只能操作2D矩阵的转置(就是每次transpose只能在两个维度之间转换,其他维度保持不变)。有两种调用方式

Row by row processing on tensor in Tensorflow

天涯浪子 提交于 2020-01-16 09:01:28
问题 I am testing the code to process row by row of a tensor. The tensor may have a row with the last 4 elements are 0 or with non-zero values. If the row has 0 for the last 4 elements [1.0,2.0,2.3,3.4,0,0,0,0] The last four are removed and shape is changed to 5 elements in the row. The first element represents the row index. It becomes like [0.0,1.0,2.0,2.3,3.4]. If the row has all 8 elements with non-zero values, then split into two rows and put row index in the first place. Then [3.0,4.0,1.0,2

Pytorch的to(device)用法

折月煮酒 提交于 2020-01-16 03:28:55
如下所示: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model.to(device) 这两行代码放在读取数据之前。 mytensor = my_tensor.to(device) 这行代码的意思是将所有最开始读取数据时的tensor变量copy一份到device所指定的GPU上去,之后的运算都在GPU上进行。 这句话需要写的次数等于需要保存GPU上的tensor变量的个数;一般情况下这些tensor变量都是最开始读数据时的tensor变量,后面衍生的变量自然也都在GPU上 如果是多个GPU 在代码中的使用方法为: device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model = Model() if torch.cuda.device_count() > 1: model = nn.DataParallel(model,device_ids=[0,1,2]) model.to(device) Tensor总结 (1)Tensor 和 Numpy都是矩阵,区别是前者可以在GPU上运行,后者只能在CPU上; (2)Tensor和Numpy互相转化很方便,类型也比较兼容 (3

How to reshape a tensor in Eigen3?

女生的网名这么多〃 提交于 2020-01-15 11:06:10
问题 I get some Eigen::TensorMap from the outputs vector from a tensorflow session in C++. I want to do some operations to the Eigen::TensorMap (reshape and concat etc.). However, my codes cannot be compiled due to some weird error. I tried to reproduce it in pure Eigen3 code. #include <unsupported/Eigen/CXX11/Tensor> using Eigen::Tensor; using Eigen::TensorMap; using Eigen::TensorRef; using std::vector; int main() { int storage[128]; TensorMap<Tensor<int, 4>> t_4d(storage, 2, 4, 2, 8); vector