vl_nnsoftmax源码解析

function Y = vl_nnsoftmax(X,dzdY)
%VL_NNSOFTMAX CNN softmax.
%   Y = VL_NNSOFTMAX(X) applies the softmax operator the data X. X
%   has dimension H x W x D x N, packing N arrays of W x H
%   D-dimensional vectors.
%
%   D can be thought of as the number of possible classes and the
%   function computes the softmax along the D dimension. Often W=H=1,
%   but this is not a requirement, as the operator is applied
%   convolutionally at all spatial locations.
%
%   DZDX = VL_NNSOFTMAX(X, DZDY) computes the derivative of the block
%   projected onto DZDY. DZDX and DZDY have the same dimensions as
%   X and Y respectively.

% Copyright (C) 2014 Andrea Vedaldi.
% All rights reserved.
%
% This file is part of the VLFeat library and is made available under
% the terms of the BSD license (see the COPYING file).

E = exp(bsxfun(@minus, X, max(X,[],3))) ;
L = sum(E,3) ;
Y = bsxfun(@rdivide, E, L) ;

if nargin <= 1, return ; end

% backward
Y = Y .* bsxfun(@minus, dzdY, sum(dzdY .* Y, 3)) ;

vl_nnsoftmax函数的主要作用就是将最后一层全连接层的输出score转化为(0,1)之间的概率值,形成概率分布。
其中在前向传播阶段,即为

相应源码为:

E = exp(bsxfun(@minus, X, max(X,[],3))) ;
L = sum(E,3) ;
Y = bsxfun(@rdivide, E, L) ;

if nargin <= 1, return ; end

在反向传播阶段,即计算softmax的偏导,如下:


相应的源码为:

% backward
Y = Y .* bsxfun(@minus, dzdY, sum(dzdY .* Y, 3)) ;
其中,dzdY初始化值为1,即Y=Y.*(1-Y) 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值