tensor

关于pytorch官方文档tensor/two_layer_net_numpy.py的学习

匿名 (未验证) 提交于 2019-12-02 22:56:40
warm_up 热身训练 代码是使用numpy进行的模型训练,目的是通过这样一个我们熟悉的矩阵计算过程模拟神经网络的运算,实质上的过程基本一致,就是中间的实现不同 import numpy N, D_in, H, D_out = 64, 1000, 100, 10 分别代表,N:每次训练的样本个数 D_in:输入 D_out:输出 H:隐藏层 x = np.random.randn(N, D_in) y = np.random.randn(N, D_out) w1 = np.random.randn(D_in, H) w2 = np.random.randn(H, D_out) 构建四个随机生成的长和宽分别是括号里面的数字的矩阵 x:输入 y:输出 w1:输入和隐藏层的 learning_rate = 1e-6 学习率0.好多零6 训练过程 通俗的来说,我们保存的模型就是一个矩阵,后面的预测值只是根据预测数据和矩阵进行计算得到的结果,比如分类问题就是计算每一个种类的概率,选择最大的概率作为预测结果 感兴趣的话,可以把最开始的参数调小,并输出每一步的结果,这样可以有一个更直观的了解 文章来源: 关于pytorch官方文档tensor/two_layer_net_numpy.py的学习

pytorch1.1.0-python3.6-CUDA9.0-ToTensor ToPILImage

匿名 (未验证) 提交于 2019-12-02 22:51:30
参考: PIL.Image和np.ndarray图片与Tensor之间的转换 https://blog.csdn.net/tsq292978891/article/details/78767326 Pytorch之深入torchvision.transforms.ToTensor与ToPILImage https://blog.csdn.net/qq_37385726/article/details/81811466 PyTorch载入图片后ToTensor解读(含PIL和OpenCV读取图片对比) https://www.cnblogs.com/ocean1100/p/9494640.html torchvision.transforms.ToTensor 对于一个图片img,调用ToTensor转化成张量的形式,发生的不是将图片的RGB三维信道矩阵变成tensor 图片在内存中以bytes的形式存储,转化过程的步骤是: 对张量进行reshape 对张量进行transpose 将当前张量的每个元素除以255 输出张量 torchvision.transforms.ToPILImage 对于一个Tensor的转化过程是: 将张量的每个元素乘上255 将张量的数据类型有FloatTensor转化成Uint8 将张量转化成numpy的ndarray类型

数据统计

匿名 (未验证) 提交于 2019-12-02 22:11:45
Ŀ¼ Outline Vector norm Eukl. Norm L1 Norm reduce_min/max/mean argmax/argmin tf.equal Accuracy tf.unique Outline tf.norm tf.reduce_min/max/mean tf.argmax/argmin tf.equal tf.unique Vector norm Eukl. Norm \[ ||x||_2=|\sum_{k}x_k^2|^{\frac{1}{2}} \] Max.norm \[ ||x||_{\infty}=max_k|x_k| \] L1-Norm \[ ||x||_1=\sum_{k}|x_k| \] Here talks about Vector Norm Eukl. Norm import tensorflow as tf a = tf . ones ([ 2 , 2 ]) a <tf.Tensor : id = 11, shape = (2, 2), dtype = float32, numpy = array([[1., 1.], [1., 1.]], dtype = float32) > tf . norm ( a ) <tf.Tensor : id = 7, shape = (), dtype = float32, numpy = 2

tf工程化部署相关

拜拜、爱过 提交于 2019-12-02 19:09:32
1、TensorFlow 模型保存/载入的两种方法 https://blog.csdn.net/thriving_fcl/article/details/71423039 【讲解清晰,2种方法都有缺陷,最好的方法是 saved_model 模块保存/载入模型 】 a. 使用saver.save() 保存, saver.restore()方法载入: 这种方法不方便的在于,在使用模型的时候,必须把模型的结构重新定义一遍,然后载入对应名字的变量的值。但是很多时候我们都更希望能够读取一个文件然后就直接使用模型,而不是还要把模型重新定义一遍。所以就需要使用另一种方法。 b. tf.train.import_meta_graph: 这个方法可以从文件中将保存的graph的所有节点加载到当前的default graph中,并返回一个saver。也就是说,我们在保存的时候,除了将变量的值保存下来,其实还有将对应graph中的各种节点保存下来,所以模型的结构也同样被保存下来了。 比如我们想要保存计算最后预测结果的 y ,则应该在训练阶段将它添加到collection中。 相比第一种,不需要重新定义模型,但是为了从graph中找到输入输出的tensor,还是得用graph.get_tensor_by_name()来获取,也就是还需要知道在定义模型阶段所赋予这些tensor的名字

How to Switch from Keras Tensortype to numpy array for a custom layer?

戏子无情 提交于 2019-12-02 16:45:59
问题 So I have a custom layer, that does not have any weights. In a fist step, I tried to implement the functions manipulating the input tensors in Kers. But I did not succeed because of many reasons. My second approach was to implement the functions with numpy operations, since the custom layer I am implementing does not have any weights, from my understanding, I would say, that I could use numpy operarations, as I don't need backpropagation, since there are no weights, right? And then, I would

How does reduce_sum() work in tensorflow?

て烟熏妆下的殇ゞ 提交于 2019-12-02 16:43:51
I am learning tensorflow, I picked up the following code from the tensorflow website. According to my understanding, axis=0 is for rows and axis=1 is for columns. How are they getting output mentioned in comments? I have mentioned output according to my thinking against ##. import tensorflow as tf x = tf.constant([[1, 1, 1], [1, 1, 1]]) tf.reduce_sum(x, 0) # [2, 2, 2] ## [3, 3] tf.reduce_sum(x, 1) # [3, 3] ##[2, 2, 2] tf.reduce_sum(x, [0, 1]) # 6 ## Didn't understood at all. x has a shape of (2, 3) (two rows and three columns): 1 1 1 1 1 1 By doing tf.reduce_sum(x, 0) the tensor is reduced

How to get the dimensions of a tensor (in TensorFlow) at graph construction time?

半腔热情 提交于 2019-12-02 16:36:45
I am trying an Op that is not behaving as expected. graph = tf.Graph() with graph.as_default(): train_dataset = tf.placeholder(tf.int32, shape=[128, 2]) embeddings = tf.Variable( tf.random_uniform([50000, 64], -1.0, 1.0)) embed = tf.nn.embedding_lookup(embeddings, train_dataset) embed = tf.reduce_sum(embed, reduction_indices=0) So I need to know the dimensions of the Tensor embed . I know that it can be done at the run time but it's too much work for such a simple operation. What's the easier way to do it? I see most people confused about tf.shape(tensor) and tensor.get_shape() Let's make it

Pytorch学习----认识张量

人盡茶涼 提交于 2019-12-02 15:03:54
一、Tensor概念 张量是一个多维数组,它是标量、向量、矩阵的高维拓展。张量是三维及以上的数组。 标量:是一个常数,为0维张量 向量:是一行或者一列数组成,为1维张量 矩阵:包含行和列两个维度。是2维张量。 torch.Tensor包含的属性: dtype:张量的数据类型,如torch.FloatTensor shape:张量的形状,如(64, 3, 224, 224) device:张量所在的设备,GPU/CPU data:被包装的Tensor grad:data的梯度 grad_fn:创建Tensor的Function,是自动求导的关键 requires_grad:指示是否需要梯度 is_leaf:指示是否是叶子结点 二、Tensor创建:直接创建 1. torch.tensor(data, dtype=None, device=None) 功能 :从data创建tensor 参数 : data:数据,可以是list,numpy dtype:数据类型,默认与data一致 device:所在设备,cpu或gpu requires_grad:是否需要梯度 pin_memory:是否存于锁页内存 import torch import numpy as np arr = np.ones((3, 3)) print("arr的数据类型:", arr.dtype) t = torch

Tensorflow从零到精通

十年热恋 提交于 2019-12-02 12:57:54
Tensorflow是一种计算图模型,即用图的形式来表示运算过程的一种模型。Tensorflow程序一般分为图的构建和图的执行两个阶段。图的构建阶段也称为图的定义阶段,该过程会在图模型中定义所需的运算,每次运算的的结果以及原始的输入数据都可称为一个节点(operation ,缩写为op)。我们通过以下程序来说明图的构建过程: 程序2-1: 程序2-1定义了图的构建过程,“import tensorflow as tf”,是在python中导入tensorflow模块,并另起名为“tf”;接着定义了两个常量op,m1和m2,均为1*2的矩阵;最后将m1和m2的值作为输入创建一个矩阵加法op,并输出最后的结果result。 我们分析最终的输出结果可知,其并没有输出矩阵相加的结果,而是输出了一个包含三个属性的Tensor(Tensor的概念我们会在下一节中详细讲解,这里就不再赘述)。 以上过程便是图模型的构建阶段:只在图中定义所需要的运算,而没有去执行运算。我们可以用图2-1来表示: 第二个阶段为图的执行阶段,也就是在会话(session)中执行图模型中定义好的运算。 我们通过程序2-2来解释图的执行阶段: 程序2-2: 此外,我们还可以利用CPU或GPU等计算资源分布式执行图的运算过程。一般我们无需显示的指定计算资源,Tensorflow可以自动地进行识别,如果检测到我们的GPU环境

doParallel performance on a tensor in R

一曲冷凌霜 提交于 2019-12-02 11:11:13
I need to perform some operations on a tensor and I would like make this parallel. Consider the following example: # first part without doParallel N = 8192 M = 128 F = 64 ma <- function(x,n=5){filter(x,rep(1/n,n), sides=2)} m <- array(rexp(N*M*F), dim=c(N,M,F)) new_m <- array(0, dim=c(N,M,F)) system.time ( for(i in 1:N) { for(j in 1:F) { ma_r <- ma(m[i,,j],2) ma_r <- c(ma_r[-length(ma_r)], ma_r[(length(ma_r)-1)]) new_m[i,,j] <- ma_r } } ) This takes around 38 seconds in my laptop. The following is with doParallel: # second part with doParallel library(doParallel) no_cores <- detectCores() - 1