softmax

用numpy实现CNN卷积神经网络

我是研究僧i 提交于 2019-12-02 03:47:24
为了加深对卷积神经网络底层原理的理解,本文通过使用numpy来搭建一个基础的包含卷积层、池化层、全连接层和Softmax层的卷积神经网络,并选择relu作为我们的激活函数,选择多分类交叉熵损失函数,最后使用了mnist数据集进行了训练和测试。 关于卷积网络的详细原理和实现可参考下列文章: 刘建平Pinard:卷积网络前向反向传播算法 卷积层的反向传播 手把手带你 Numpy实现CNN 1、卷积层 卷积层的前向传播输出由卷积核和特征图作卷积运算得到,反向传播时需要计算kernel和bias的梯度以及delta的反向传播误差,kernel的梯度由原特征图和delta作卷积得到,bias每个通道的梯度由对delta每个通道直接求和得到,delta的反向传播误差由delta和旋转180度的卷积核作卷积运算得到。其中卷积运算在实现时先将特征图的对应部分和卷积核展开成了向量的形式,再作向量乘法运算,这样可以通过并行运算加快速度,实现代码如下: def img2col(x, ksize, stride): wx, hx, cx = x.shape # [width,height,channel] feature_w = (wx - ksize) // stride + 1 # 返回的特征图尺寸 image_col = np.zeros((feature_w*feature_w, ksize

Pytorch - Pick best probability after softmax layer

强颜欢笑 提交于 2019-12-01 23:56:39
问题 I have a logistic regression model using Pytorch 0.4.0, where my input is high-dimensional and my output must be a scalar - 0 , 1 or 2 . I'm using a linear layer combined with a softmax layer to return a n x 3 tensor, where each column represents the probability of the input falling in one of the three classes ( 0 , 1 or 2 ). However, I must return a n x 1 tensor, so I need to somehow pick the highest probability for each input and create a tensor indicating which class had the highest

caffe中softmax源码阅读

偶尔善良 提交于 2019-12-01 15:24:51
(1) softmax函数              (1) 其中,z j 是softmax层的bottom输入, f(z j )是softmax层的top输出,C为该层的channel数。 (2) softmax_layer.cpp中的Reshape函数: 1 template <typename Dtype> 2 void SoftmaxLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, //bottom blob为softmax层的输入,top blob为该层输出。 3 const vector<Blob<Dtype>*>& top) { 4 softmax_axis_ = //softmax_axis_为1 5 bottom[0]->CanonicalAxisIndex(this->layer_param_.softmax_param().axis()); 6 top[0]->ReshapeLike(*bottom[0]); //使用bttom[0]的shape和值去初始化top[0],后面所有的操作基于top[0]   //bottom[0]的shape为[N, C, H, W], bottom[0]->shape(softmax_axis_)的值为C 7 vector<int> mult_dims(1,

Custom Keras Layer Troubles

左心房为你撑大大i 提交于 2019-12-01 12:53:28
问题 Hate to ask a question like this on machine learning but googling has yielded nothing useful - I've just found 2 github threads where people on super old versions of tensorflow got the same error message , but not for the same reason im getting it. Basically; I'm implementing this facial point paper for work; and it uses spatial softargmax (just a layer that takes in a stack of images a lot like this - and it returns the most "intense part" of the image (so just the x,y coordinates of the

Gradient calculation for softmax version of triplet loss

我的未来我决定 提交于 2019-12-01 08:51:49
问题 I have been trying to implement the softmax version of the triplet loss in Caffe described in Hoffer and Ailon, Deep Metric Learning Using Triplet Network, ICLR 2015. I have tried this but I am finding it hard to calculate the gradient as the L2 in exponent is not squared. Can someone please help me here? 回答1: Implementing the L2 norm using existing layers of caffe can save you all the hustle. Here's one way to compute ||x1-x2||_2 in caffe for "bottom"s x1 and x2 (assuming x1 and x2 are B -by

tensorflow的MNIST教程

我的梦境 提交于 2019-12-01 07:13:55
(ps:根据自己的理解,提炼了一下官方文档的内容,错误的地方希望大佬们多多指正。。。。。) 0x01:数据集的获取和表示 数据集的获取,可以通过代码自动下载。这里的数据就是各种手写数字图片和图片对应的标签(告诉我们这个数字是几,比如下面的是5,0,4,1)。 下载下来的数据集被分成两部分:60000行的训练数据集( mnist.train )和10000行的测试数据集( mnist.test ),而每一个数据集都有两部分组成:一张包含手写数字的 图片(xs) 和一个对应的 标签(ys) 。训练数据集和测试数据集都包含xs和ys,比如训练数据集的图片是 mnist.train.images ,训练数据集的标签是 mnist.train.labels 。根据图片像素点把图片展开为向量,再进一步操作,识别图片上的数值。那这60000个训练数据集是怎么表示的呢?在MNIST训练数据集中, mnist.train.images 是一个形状为 [60000, 784] 的张量(至于什么是张量,小伙伴们可以手都搜一下,加深一下印象),第一个维度数字用来索引图片,第二个维度数字用来索引每张图片中的像素点。在此张量里的每一个元素,都表示某张图片里的某个像素的强度值,值介于0和1之间。 0x02:代码运行 代码分为两部分,一个是用于下载数据的 input_data.py , 另一个是主程序 mnist

如何正确的理解激活函数?

淺唱寂寞╮ 提交于 2019-12-01 01:56:22
神经网络中,会得到w0,w1,...wn,这些是各个特征的权重,如果输出output = w0x0+w1x1+...+wnxn 则训练得到的是特征的线性组合,如果只是线性组合,我们完全可以去掉所有隐藏层。 事实上,有很多情况下,特征与输出之间的关系是非线性的,所以我们需要一个通用的,可以逼近所有(线性与非线性)关系的网络 引入非线性激活函数的原因就在这里。 深度学习中,激活函数通常指能够实现非线性映射的函数 常见激活函数: softmax函数 使用方法: 1、不同层的激活函数可以是不一样的。 2、sigmoid函数一般只用在2分类的输出层,且结果与softmax激活函数输出相同,我一般习惯统一使用softmax 3、ReLU (修正线性单元 Rectified Linear Unit) 最常用 y = max(0,x) 4\ 来源: https://www.cnblogs.com/yjybupt/p/11647033.html

word2vec预训练词向量

守給你的承諾、 提交于 2019-11-30 23:06:41
NLP中的Word2Vec讲解   word2vec是Google开源的一款用于词向量计算 的工具,可以很好的度量词与词之间的相似性;   word2vec建模是指用 CBoW模型 或 Skip-gram模型 来计算不同 词语的向量(word vector)    CBoW是给定上下文来预测输入词、Skip-gram给定输入词预测上下文,但最终都会得到词向量矩阵W 上图为词向量的部分可视化结构 Statistical Language Model (统计语言模型)    在深入word2vec之前,首先回顾下nlp中的一个基本问题: 如何计算一段文本序列在某种语言下出现的概率?(Ngram) 统计语言模型给出了这一类问题的一个基本解决框架。对于一段文本序列: 它的概率可表示为: 即将序列的联合概率转化为一系列条件概率的乘积。问题变成了如何去预测这些给定previous words下的条件概率:                          由于其巨大的参数空间,这样一个原始的模型在实际中并没有什么用。我们更多的是采用其简化版本——Ngram模型:   常见的如bigram模型(N=2)和tirgram模型(N=3)。事实上,由于模型复杂度和预测精度的限制,我们很少会考虑N>3的模型。我们可以用最大似然法去求解Ngram模型的参数——等价于去统计每个Ngram的条件词频

Does the Inception Model have two softmax outputs?

馋奶兔 提交于 2019-11-30 21:01:27
The Inception v3 model is shown in this image: The image is from this blog-post: https://research.googleblog.com/2016/03/train-your-own-image-classifier-with.html It seems that there are two Softmax classification outputs. Why is that? Which one is used in the TensorFlow example as the output tensor with the name 'softmax:0' in this file? https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/imagenet/classify_image.py The academic paper for the Inception v3 model doesn't seem to have this image of the Inception model: http://arxiv.org/pdf/1512.00567v3.pdf I'm trying to

【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法

混江龙づ霸主 提交于 2019-11-30 18:27:49
【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法 【TensorFlow】tf.nn.softmax_cross_entropy_with_logits的用法 from:https://blog.csdn.net/mao_xiao_feng/article/details/53382790 在计算loss的时候,最常见的一句话就是tf.nn.softmax_cross_entropy_with_logits,那么它到底是怎么做的呢? 首先明确一点,loss是代价值,也就是我们要最小化的值 tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None) 除去name参数用以指定该操作的name,与方法有关的一共两个参数 : 第一个参数logits:就是神经网络最后一层的输出,如果有batch的话,它的大小就是[batchsize,num_classes],单样本的话,大小就是 num_classes 第二个参数labels:实际的标签,大小同上 具体的执行流程大概分为两步: 第一步是先对网络最后一层的输出做一个softmax,这一步通常是求取输出属于某一类的概率,对于单样本而言,输出就是一个 num_classes 大小的向量([Y1,Y2,Y3...