tensor

[pytorch] PyTorch Hook

谁都会走 提交于 2019-11-28 21:05:43
PyTorch Hook ¶ 为什么要引入hook? -> hook可以做什么? 都有哪些hook? 如何使用hook? 1. 为什么引入hook? ¶ 参考: Pytorch中autograd以及hook函数详解 在pytorch中的自动求梯度机制(Autograd mechanics)中,如果将tensor的requires_grad设为True, 那么涉及到它的一系列运算将在反向传播中自动求梯度。 In [0]: x = torch.randn(5, 5) # requires_grad=False by default y = torch.randn(5, 5) # requires_grad=False by default z = torch.randn((5, 5), requires_grad=True) a = x + y b = a + z print(a.requires_grad, b.requires_grad) False True 但是自动求导的机制有个我们需要注意的地方: 在自动求导机制中只保存叶子节点,也就是中间变量在计算完成梯度后会自动释放以节省空间. 所以下面代码我们在计算过程中只得到了z对x的梯度,而y和z的梯度都在梯度计算后被自动释放了,所以显示为None. In [0]: x = torch.tensor([1,2],dtype

Deepctr框架代码阅读

旧时模样 提交于 2019-11-28 17:50:46
DeepCtr是一个简易的CTR模型框架,集成了深度学习流行的所有模型,适合学推荐系统模型的人参考。 我在参加比赛中用到了这个框架,但是效果一般,为了搞清楚原因从算法和框架两方面入手。在读代码的过程中遇到一些不理解的问题,所以记录在这里。 dense_embedding和sparse_embedding的区别? 处理dense, 是将dense接全连接层变成embedding size的一个list,然后和sparse一样的操作 concat_fun 这里是concat什么?fm的输入为什么需要concat? 原来是list,每一行是一个tensor,concat之后是tensor,每一行是tensor tf.keras.layers.Flatten()(fm_input) 原先的embedding输入是[d,f,k],deep embedding是[d,f*k] 这里的实现和我的实现不一样: 我的linear+interact+deep接入全连接层,将所有的特征接入全连接层, 但是根据根据论文和多家的博客来看,我之前理解的是错误的,正确的应该是 fm logit+deep logit,最后接全连接层。 同时AFM等多个模型都是这么处理的。 运行模型,每次结果不一样: 这个属于正常现象,尤其是数据不够充分的情况下,

why do we “pack” the sequences in pytorch?

强颜欢笑 提交于 2019-11-28 16:39:15
I was trying to replicate How to use packing for variable-length sequence inputs for rnn but I guess I first need to understand why we need to "pack" the sequence. I understand why we need to "pad" them but why is "packing" ( through pack_padded_sequence ) necessary? Any high-level explanation would be appreciated! Umang Gupta I have stumbled upon this problem too and below is what I figured out. When training RNN (LSTM or GRU or vanilla-RNN), it is difficult to batch the variable length sequences. For ex: if length of sequences in a size 8 batch is [4,6,8,5,4,3,7,8], you will pad all the

What is a batch in TensorFlow?

坚强是说给别人听的谎言 提交于 2019-11-28 16:33:23
The introductory documentation, which I am reading ( TOC here ) introduces the term here without having defined it. [1] https://www.tensorflow.org/get_started/ [2] https://www.tensorflow.org/tutorials/mnist/tf/ Let's say you want to do digit recognition (MNIST) and you have defined your architecture of the network (CNNs). Now, you can start feeding the images from the training data one by one to the network, get the prediction (till this step it's called as doing inference ), compute the loss, compute the gradient, and then update the parameters of your network (i.e. weights and biases ) and

Tensorflow define · Dyting's Blog

故事扮演 提交于 2019-11-28 16:19:35
Tensorflow 概念 Tensor Tensor是TensorFlow中主要的数据结构,是一个多维数组。例如可以讲一小组图像集表示成一个四维的浮点数数组,这四个维度分别是[batch,height,width,channels]. 创建tensor有两种方式,一是直接用tensorflow自带的函数创建,二是用Python的numpy库创建。 第一种如下: 12 import tensorflow as tftf.zeros([row_dim,col_dim]) 第二种方式: 123 import numpy as npx_data=np.array([[1,2,3],[4,5,6]])tf.convert_to_tensor(x_data,dtype=tf.float32) graph图 一个完整的TF代码主要分成2个部分,定义(构建)和执行。通常在构建阶段创建一个图表示和训练神经网络。 如下图所示: Tensor在一个或者多个由节点和边组成的图中流动,边代表tensors,节点代表对tensors的操作。 如图,节点a接收了一个1-D tensor,该tensor从节点a流出后,分别流向节点b和c。。 一旦开始一个任务,一个默认的图已经创建好了,可以通过调用tf.get_default_graph)()来访问。在默认的图里面添加操作,如下例子所示: 12345

How does the “view” method work in PyTorch?

旧街凉风 提交于 2019-11-28 13:19:06
问题 I am confused about the method view() in the following code snippet. class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(3, 6, 5) self.pool = nn.MaxPool2d(2,2) self.conv2 = nn.Conv2d(6, 16, 5) self.fc1 = nn.Linear(16*5*5, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(x))) x = x.view(-1, 16*5*5) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x =

Accuracy metric of a subsection of categories in Keras

北城余情 提交于 2019-11-28 10:51:11
问题 I've got a 3-class classification problem. Let's define them as classes 0,1 and 2. In my case, class 0 is not important - that is, whatever gets classified as class 0 is irrelevant. What's relevant, however, is accuracy, precision, recall, and error rate only for classes 1 and 2. I would like to define an accuracy metric that only looks at a subsection of the data that relates to 1 and 2 and gives me a measure of that as the model is training. I am not asking for code for accuracy or f1 or

TensorFlow: Max of a tensor along an axis

僤鯓⒐⒋嵵緔 提交于 2019-11-28 08:57:05
My question is in two connected parts: How do I calculate the max along a certain axis of a tensor? For example, if I have x = tf.constant([[1,220,55],[4,3,-1]]) I want something like x_max = tf.max(x, axis=1) print sess.run(x_max) output: [220,4] I know there is a tf.argmax and a tf.maximum , but neither give the maximum value along an axis of a single tensor. For now I have a workaround: x_max = tf.slice(x, begin=[0,0], size=[-1,1]) for a in range(1,2): x_max = tf.maximum(x_max , tf.slice(x, begin=[0,a], size=[-1,1])) But it looks less than optimal. Is there a better way to do this? Given

pytorch 入门指南

醉酒当歌 提交于 2019-11-28 08:16:21
两类深度学习框架的优缺点 动态图(PyTorch) 计算图的进行与代码的运行时同时进行的。 静态图(Tensorflow <2.0) 自建命名体系 自建时序控制 难以介入 使用深度学习框架的优点 GPU 加速 (cuda) 自动求导 常用网络层的API PyTorch 的特点 支持 GPU 动态神经网络 Python 优先 命令式体验 轻松扩展 1.Pytorch简介 Pytorch是Facebook 的 AI 研究团队发布了一个基于 Python的科学计算包,旨在服务两类场合: 替代numpy发挥GPU潜能(在线环境暂时不支持GPU) 一个提供了高度灵活性和效率的深度学习实验性平台 2.Pytorch特点及优势 2.1 Pytorch特点 PyTorch 提供了运行在 GPU/CPU 之上、基础的张量操作库; 可以内置的神经网络库; 提供模型训练功能; 支持共享内存的多进程并发(multiprocessing )库等; 2.2 Pytorch特点 处于机器学习第一大语言 Python 的生态圈之中,使得开发者能使用广大的 Python 库和软件;如 NumPy、SciPy 和 Cython(为了速度把 Python 编译成 C 语言); (最大优势)改进现有的神经网络,提供了更快速的方法——不需要从头重新构建整个网络,这是由于 PyTorch 采用了动态计算图(dynamic

Pytorch——自动求导机制

别来无恙 提交于 2019-11-28 08:16:12
文章目录 Tensor Autograd Autograd过程解析 结论 引用 Tensor Tensor(张量)是Pytorch中的基础计算单位,和numpy中的ndarray一样都是表示一个多维矩阵,不同的地方在于:Tensor既可以在CPU上运算也可以在GPU上进行运算,而ndarray只能在CPU上进行计算。 Tensor有三个重要的属性: data :保存张量的值; grad :保存该张量的梯度; grad_fn :指向一个用于反向传播计算输入梯度的Function对象; 在创建Tensor默认是不使用梯度的,如果需要进行梯度计算需要设置属性: requires_grad = True Autograd Autograd是Pytorch的核心模块,该模块实现了深度学习算法的反向传播(BP)求导过程。在Pytorch中所有作用在Tensor上的操作,Autograd都能为其提供自动微分求导的操作。 在创建Tensor时,同设置属性 requires_grad 为 True 声明该Tensor需要计算梯度。 用户手动创建的Tensor的 grand_fn 属性默认是 None 。 在张量进行操作之后 grad_fn 就回被赋值为一个新的函数,该函数指向创建了该Tensor的Function对象。 Tensor 和 Function 共同组成一个非循环图,通过 grad_fn