tensor

pytorch: Tensor 常用操作

大兔子大兔子 提交于 2019-12-05 21:16:49
pytorch: Tensor 常用操作 torch.tensor是一个包含多个同类数据类型数据的多维矩阵。 常用参数 dtype : tessor的数据类型,总共有八种数据类型。其中默认的类型是 torch.FloatTensor ,而且这种类型的别名也可以写作 torch.Tensor 。 Data type dtype CPU tensor GPU tensor 32-bit floating point torch.float32 or torch.float torch.FloatTensor torch.cuda.FloatTensor 64-bit floating point torch.float64 or torch.double torch.DoubleTensor torch.cuda.DoubleTensor 16-bit floating point torch.float16 or torch.half torch.HalfTensor torch.cuda.HalfTensor 8-bit integer (unsigned) torch.uint8 torch.ByteTensor torch.cuda.ByteTensor 8-bit integer (signed) torch.int8 torch.CharTensor torch.cuda

Pytorch(一)入门:Tensor基础

元气小坏坏 提交于 2019-12-05 21:16:03
torch.Tensor 基础 Tensor就是pytorch中存储数据的主要格式,跟numpy类似 这里,我们先介绍一些最基本的操作和常用的功能 在numpy中,我们是通过shape来获取数组的形状,而在我们的tensor当中,我们使用size来得到形状。 有时候,我们需要对数组形状进行改变,我们可以采用 .view() 的方式 可以注意到test5通过test4变形的时候,是从test4的第一行从左至右,然后第二行从左至右…这样形成3x2的矩阵的。 torch.Tensor 支持大量的数学操作符 + , - , * , / 都是可以用的。 当然也可以用Tensor内置的 add() 等, 这里需要提一下的就是 add 和 add_ 的区别 使用add函数会生成一个新的Tensor变量, add_ 函数会直接再当前Tensor变量上进行操作 所以,对于函数名末尾带有"_" 的函数都是会对Tensor变量本身进行操作的 tensor对矩阵同样支持 跟numpy一样,再Tensor中,也存在Broadcasting 当二元操作符左右两边Tensor形状不一样的时候,系统会尝试将其复制到一个共同的形状。例如a的第0维是3, b的第0维是1,那么 a + b这个操作会将b沿着第0维复制3遍。 Tensor和Numpy的相互转换 一些其他的常用的操作 torch.unsqueeze()

pytorch学习笔记1:张量

流过昼夜 提交于 2019-12-05 21:15:16
目录 Tensor与numpy相互转换: torch.ones(),torch.add(),torch.zeros(),torch.squeeze() Tensor与numpy相互转换: import torch x=torch.Tensor(2,3)#生成一个4*5的Tensor张量 将Tensor转换为numpy数组 y=x.numpy() 将numpy数组转换为Tensor import numpy as np z=torch.from_numpy(x) torch.ones(),torch.add(),torch.zeros(),torch.squeeze() torch.ones() torch.ones(*sizes, out=None) → Tensor 返回一个全为1 的张量,形状由可变参数 sizes 定义。 参数: sizes (int...) – 整数序列,定义了输出形状 out (Tensor, optional) – 结果张量 例子: >>> torch.ones(2, 3) 1 1 1 1 1 1 [torch.FloatTensor of size 2x3] >>> torch.ones(5) 1 1 1 1 1 [torch.FloatTensor of size 5] torch.add() torch.add(input, value, out

Pytorch Tensor的索引与切片

你离开我真会死。 提交于 2019-12-05 21:11:03
1. Pytorch风格的索引 根据Tensor的shape,从前往后索引,依次在每个维度上做索引。 示例代码: import torch a = torch.rand(4, 3, 28, 28) print(a[0].shape) #取到第一个维度 print(a[0, 0].shape) # 取到二个维度 print(a[1, 2, 2, 4]) # 具体到某个元素 上述代码创建了一个shape=[4, 3, 28, 28]的Tensor,我们可以理解为4张图片,每张图片有3个通道,每个通道是28x28的图像数据。a代表这个Tensor,a后面跟着的列表[]表示对Tensor进行索引,a的维度dim = 4,决定了[]中的元素个数不能超过4个,[]中的值表示对应维度上的哪一个元素,比如 a[0]表示取第一个维度上的第一个元素,可以理解为第一张图片,a[1]表示取第一个维度上的第二个元素,可以理解为第二张图片。a[0, 0]表示取第一个维度上第一个元素的与第二个维度上的第一个元素,也就是第一张图片第一个通道的元素。a[1, 2, 2, 4]表示取第第一个维度上的第二个元素与第二个维度上的第三个元素与第三个维度上的第三个元素与第四个维度上的第5个元素,也就是第二张图片第三个通道第三行第四列的像素值是一个标量值。 输出结果: torch.Size([3, 28, 28]) torch

【PyTorch学习笔记】3:创建Tensor的多种方法

帅比萌擦擦* 提交于 2019-12-05 21:09:42
创建Tensor的多种方法 从numpy创建 import torch import numpy as np a = np . array ( [ 2 , 3.3 ] ) a = torch . from_numpy ( a ) # torch.DoubleTensor 从list创建 a = torch . FloatTensor ( [ 2 , 3.3 ] ) # 尽量少用这种方式,容易和给shape的情况看混淆 b = torch . tensor ( [ 2 , 3.3 ] ) # 现有list时尽量用这种方式 注意小写的 tensor 只接受现有的数据;而大写的 Tensor 相当于就是 FloatTensor ,既可以接收现有的数据,也可以接受shape来创建指定形状的Tensor。 仅指定维度的初始化 前面都提供了里面元素的具体值,也可以按照shape来创建,而先不为其提供具体值。 # 生成2行3列的数据 a = torch . empty ( 2 , 3 ) b = torch . FloatTensor ( 2 , 3 ) c = torch . IntTensor ( 2 , 3 ) 修改tensor的默认类型 使用 torch.tensor 传入浮点数元素,或者使用 torch.Tensor 仅指定维度时,生成的默认是FloatTensor

pytorch学习笔记1: Tensor

耗尽温柔 提交于 2019-12-05 21:07:10
pytorch学习笔记1: Tensor 参考网址 pytorch学习笔记1: Tensor 1、Tensors 建立5*3的矩阵,未初始化 建立随机初始化矩阵 建立零初始化矩阵,数据类型是Long 建立一个tensor数据来源于data 在原有tnesor的基础上形成新的tensor,会继承原有tensor的shapee和dtype等属性,当然我么也可以修改这些属性 获取tensor的size torch.size是一个元组,支持所有元组(tuple)的操作 2、对Tensor的操作 实现加法的四种方式 所有原地替换 你可以使用标准的numpy操作 使用torch.view 改变tensor的形状 tensor转化为numpy的数字,使用item Torch Tensor 和numpy的相互转换 将numpy array转化为pytorch Tensor CUDA Tensors 1、Tensors Tensors are similar to NumPy’s ndaeeays,不同的是可以在GPU上使用和加速计算。 导入包 from __future__ import print_function import torch 建立5*3的矩阵,未初始化 x = torch. empty ( 5 , 3 ) print (x) out tensor( [[ 1.4395e-36,

Loop over a tensor and apply function to each element

折月煮酒 提交于 2019-12-05 18:37:47
I want to loop over a tensor which contains a list of Int , and apply a function to each of the elements. In the function every element will get the value from a dict of python. I have tried the easy way with tf.map_fn , which will work on add function, such as the following code: import tensorflow as tf def trans_1(x): return x+10 a = tf.constant([1, 2, 3]) b = tf.map_fn(trans_1, a) with tf.Session() as sess: res = sess.run(b) print(str(res)) # output: [11 12 13] But the following code throw the KeyError: tf.Tensor'map_8/while/TensorArrayReadV3:0' shape=() dtype=int32 exception: import

tensor 中mul_,add_解读

余生长醉 提交于 2019-12-05 16:32:59
pytorch 中文网文档链接 https://ptorch.com/docs/1/Tensor 每一个张量tensor都有一个相应的torch.Storage保存其数据,张量类提供了一个多维的,横向视图的存储,并定义了数字操作。 img = ToTensor(img) img = img.mul_(2).add_(-1) torch.FloatTensor.abs_() 会在原地计算绝对值并返回修改的张量, 而 tensor.FloatTensor.abs() 将会在新张量中计算结果。 mul_( value ) mul() 的直接运算形式,即直接执行并且返回修改后的张量 add_(value) add() 的直接运算形式,即直接执行并且返回修改后的张量   来源: https://www.cnblogs.com/shuangcao/p/11933414.html

torch笔记合集

杀马特。学长 韩版系。学妹 提交于 2019-12-05 15:34:57
Torch笔记 import torch import numpy as np import torch.nn as nn a_np = np.random.rand(10,100) numpy知识回顾 a_np.dtype # 数据类型 a_np.ndim #维度个数 a_np.shape # 形状 整数元祖 a_np.dtype=np.int32 # 修改数据类型 获取tensor基本信息 tensor_a = torch.from_numpy(a_np) tensor_a.type() # 获取tensor类型 tensor_a.size() # 获取维度特征 并返回数组 tensor_a.size(0) #获取第一个维度信息 tensor_a.dim() # 获取维度个数 tensor_a.device # 返回设备类型 tensor数据类型转换 tensor_b = tensor_a.float() tensor_b = tensor_a.to(torch.float) 设备类型转换 tensor_b = tensor_b.cuda() tensor_b = tensor_b.cpu() tensor_b = tensor_a.to(torch.device(0)) tensor 转换到 numpy数据 tensor_b = tensor_b.numpy() # 注意

Multidimensional sparse array (3-way tensor) in R

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 12:38:45
问题 Using the Matrix package I can create a two-dimensional sparse matrix. Can someone suggest a package that would allow me to create a multidimensional (specifically a 3-dimensional) sparse matrix (array, or technically a three-way tensor) in R? 回答1: The slam package has a simple_sparse_array class: http://finzi.psych.upenn.edu/R/library/slam/html/array.html , although it only has support for indexing and coercion (if you wanted to do tensor operations, or elementwise arithmetic, without