tensor

LSTM with Attention

坚强是说给别人听的谎言 提交于 2019-12-04 15:43:31
问题 I am trying to add attention mechanism to stacked LSTMs implementation https://github.com/salesforce/awd-lstm-lm All examples online use encoder-decoder architecture, which I do not want to use (do I have to for the attention mechanism?). Basically, I have used https://webcache.googleusercontent.com/search?q=cache:81Q7u36DRPIJ:https://github.com/zhedongzheng/finch/blob/master/nlp-models/pytorch/rnn_attn_text_clf.py+&cd=2&hl=en&ct=clnk&gl=uk def __init__(self, rnn_type, ntoken, ninp, nhid,

Pytorch: Why is the memory occupied by the `tensor` variable so small?

拟墨画扇 提交于 2019-12-04 12:57:30
In Pytorch 1.0.0, I found that a tensor variable occupies very small memory. I wonder how it stores so much data. Here's the code. a = np.random.randn(1, 1, 128, 256) b = torch.tensor(a, device=torch.device('cpu')) a_size = sys.getsizeof(a) b_size = sys.getsizeof(b) a_size is 262288. b_size is 72. The answer is in two parts. From the documentation of sys.getsizeof , firstly All built-in objects will return correct results, but this does not have to hold true for third-party extensions as it is implementation specific. so it could be that for tensors __sizeof__ is undefined or defined

PyTorch/DeepLearning 杂记

纵然是瞬间 提交于 2019-12-04 07:03:53
1. softmax 运算: \(\hat{y_1}, \hat{y_2}, \hat{y_3}=softmax(o_1,o_2,o_3)\) 其中: \(\hat{y_1}=\dfrac{exp(o_1)}{\sum_{i=1}^{3}exp(o_i)}, \hat{y_2}=\dfrac{exp(o_2)}{\sum_{i=1}^{3}exp(o_i)}, \hat{y_3}=\dfrac{exp(o_3)}{\sum_{i=1}^{3}exp(o_i)}\) Pytorch中对多维`Tensor`沿某一维度求和的操作: """ tensor矩阵X: 1 2 3 4 5 6 """ X = torch.tensor([[1,2,3],[4,5,6]]) X.sum(dim=0, keepdim=True) #dim=0, 对同一列元素求和 X.sum(dim=1, keepdim=True) #dim=1, 对同一行元素求和 #keepdim=True表示求和后仍保留这两个维度 2.交叉熵损失函数: 在softmax完成多分类的情况下,我们得到的输出通常是所有类别的概率分布,我们的训练目标是使正确类别的概率最大化,只要目标类别的概率大于其他类别的概率就可以了。如果使用平方损失函数,计算出来的损失过于严格(把其他类别的概率也算了进去),所以这里使用更加适合 衡量两个概率分布差异

pytorch的tensor,Image,numpy和opencv四种格式的相互转换

孤街浪徒 提交于 2019-12-04 06:49:05
转载 https://blog.csdn.net/qq_36955294/article/details/82888443 https://blog.csdn.net/weixin_42662358/article/details/90448566 PIL:使用python自带图像处理库读取出来的图片格式 numpy:使用python-opencv库读取出来的图片格式 tensor:pytorch中训练时所采取的向量格式(当然也可以说图片) 来源: https://www.cnblogs.com/happytaiyang/p/11844415.html

PyTorch之—循环层,RNN,LSTM

爱⌒轻易说出口 提交于 2019-12-04 06:03:46
文章目录 一、RNN 循环神经网络 参数详解 二、LSTM 长短期记忆网络 参数详解 三、词嵌入 Embedding 小案例 使用RNN 训练模型 使用LSTM对文本进行词性标注 基于LSTM的词性标注模型 一、RNN 循环神经网络 参数详解 class torch.nn.RNN( args, * kwargs) 将一个多层的 Elman RNN,激活函数为 tanh 或者 ReLU ,用于输入序列。 对输入序列中每个元素,RNN每层的计算公式为 h t = tanh ⁡ ( w i h x t + b i h + w h h h t − 1 + b h h ) h_t=\tanh(w_{ih} x_t+b_{ih}+w_{hh} h_{t-1}+b_{hh}) h t ​ = tanh ( w i h ​ x t ​ + b i h ​ + w h h ​ h t − 1 ​ + b h h ​ ) h t h_t h t ​ 是时刻 t t t 的隐状态。 x t x_t x t ​ 是上一层时刻 t t t 的隐状态,或者是第一层在时刻 t t t 的输入。如果 nonlinearity=‘relu’, 那么将使用 relu 代替 tanh 作为激活函数。 RNN模型(公式)参数: weight_ih_l[k] – 第k层的 input-hidden 权重, 可学习,形状是

pytorch自定义初始化权重

风格不统一 提交于 2019-12-04 06:02:11
在常见的pytorch代码中,我们见到的初始化方式都是调用init类对每层所有参数进行初始化。但是,有时我们有些特殊需求,比如用某一层的权重取优化其它层,或者手动指定某些权重的初始值。 核心思想就是构造和该层权重同一尺寸的矩阵去对该层权重赋值。但是,值得注意的是,pytorch中各层权重的数据类型是nn.Parameter,而不是Tensor或者Variable。 import torch import torch.nn as nn import torch.optim as optim import numpy as np # 第一一个卷积层,我们可以看到它的权值是随机初始化的 w=torch.nn.Conv2d(2,2,3,padding=1) print(w.weight) # 第一种方法 print("1.使用另一个Conv层的权值") q=torch.nn.Conv2d(2,2,3,padding=1) # 假设q代表一个训练好的卷积层 print(q.weight) # 可以看到q的权重和w是不同的 w.weight=q.weight # 把一个Conv层的权重赋值给另一个Conv层 print(w.weight) # 第二种方法 print("2.使用来自Tensor的权值") ones=torch.Tensor(np.ones([2,2,3,3])) #

Pytorch:参数初始化 笔记

北城余情 提交于 2019-12-04 06:01:39
一、参数初始化概述 在设计好神经网络结构之后,权重初始化方式会很大程度上影响模型的训练过程和最终效果。 权重初始化方式包括ImageNet预训练参数,kaiming_uniform方式以及多种权重初始化方式。这篇笔记主要记录一下Pytorch中内置的各种权重初始化方式的原理与使用。 神经网络中需要进行参数初始化操作的有Linear,Conv,BN等。 二、Pytorch中的参数初始化方法 2.1 不进行初始化操作,系统的默认初始化方法(来源于pytorch0.4源码) Conv{1,2,3}d 都是继承于_ConvNd,其中对于参数的默认初始化方法如下: def reset_parameters(self): n = self.in_channels for k in self.kernel_size: n *= k stdv = 1. / math.sqrt(n) self.weight.data.uniform_(-stdv, stdv) if self.bias is not None: self.bias.data.uniform_(-stdv, stdv) Linear def reset_parameters(self): stdv = 1. / math.sqrt(self.weight.size(1)) self.weight.data.uniform_(-stdv

Pytorch-Tensor()与tensor()

本小妞迷上赌 提交于 2019-12-04 04:30:23
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/tfcy694/article/details/85338745 本文列举的框架源码基于PyTorch1.0,交互语句在0.4.1上测试通过 import torch 1 在PyTorch中,Tensor和tensor都能用于生成新的张量: >>> a=torch.Tensor([1,2]) >>> a tensor([1., 2.]) >>> a=torch.tensor([1,2]) >>> a tensor([1, 2]) 123456 但是这二者的用法有什么区别呢?我没有找到合适的中文资料,英文的资料如 https://discuss.pytorch.org/t/what-is-the-difference-between-tensor-and-tensor-is-tensor-going-to-be-deprecated-in-the-future/17134/8 也已经过时了,那就自己动手丰衣足食吧。 首先,我们需要明确一下,torch.Tensor()是python类,更明确地说,是默认张量类型torch.FloatTensor()的别名,torch.Tensor([1,2])会调用Tensor类的构造函数_

Flatten a dataset in TensorFlow

为君一笑 提交于 2019-12-04 04:10:10
问题 I am trying to convert a dataset in TensorFlow to have several single-valued tensors. The dataset currently looks like this: [12 43 64 34 45 2 13 54] [34 65 34 67 87 12 23 43] [23 53 23 1 5] ... After the transformation it should look like this: [12] [43] [64] [34] [45] [2] [13] [54] [34] [65] [34] [67] [87] [12] ... My initial idea was using flat_map on the data set and then converting each tensor to a list of tensors using reshape and unstack : output_labels = self.dataset.flat_map(convert

How can you re-use a variable scope in tensorflow without a new scope being created by default?

六眼飞鱼酱① 提交于 2019-12-04 01:07:14
问题 I have created a variable scope in one part of my graph, and later in another part of the graph I want to add OPs to an existing scope. That equates to this distilled example: import tensorflow as tf with tf.variable_scope('myscope'): tf.Variable(1.0, name='var1') with tf.variable_scope('myscope', reuse=True): tf.Variable(2.0, name='var2') print([n.name for n in tf.get_default_graph().as_graph_def().node]) Which yields: ['myscope/var1/initial_value', 'myscope/var1', 'myscope/var1/Assign',