caffe中softmax源码阅读
(1) softmax函数
(1)
其中,zj 是softmax层的bottom输入, f(zj)是softmax层的top输出,C为该层的channel数。
(2) softmax_layer.cpp中的Reshape函数:
template <typename Dtype>
void SoftmaxLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, //bottom blob为softmax层的输入,top blob为该层输出。
const vector<Blob<Dtype>*>& top) {
softmax_axis_ = //softmax_axis_为1
bottom[]->CanonicalAxisIndex(this->layer_param_.softmax_param().axis());
top[]->ReshapeLike(*bottom[]); //使用bttom[0]的shape和值去初始化top[0],后面所有的操作基于top[0]
//bottom[0]的shape为[N, C, H, W], bottom[0]->shape(softmax_axis_)的值为C
vector<int> mult_dims(, bottom[]->shape(softmax_axis_));
//Blob<Dtype> sum_multipiler、Blob<Dtype> scale_、int outer_num_、int inner_num_变量定义在softmax_layer.hpp中
//初始化sum_multiplier, mult_dims的值为C
sum_multiplier_.Reshape(mult_dims);
Dtype* multiplier_data = sum_multiplier_.mutable_cpu_data();
//设置sum_multiplier的所有元素值为1
caffe_set(sum_multiplier_.count(), Dtype(), multiplier_data);
//blob的shape为[N, C, H, W], 形象点说就是blob->shape[0] = N, blob->shape[1] = C
//blob的count为N*C*H*W,形象点说就是blob->count() = N*C*H*W
//blob->count(0, 2)中的(0, 2)是左闭右开区间,返回的是N*C
//所以就有outer_num_ = bottom[0]->count(0, softmax_axis_) = N
// inner_num_ = bottom[0]->count(softmax_axis_) = H*W
outer_num_ = bottom[]->count(, softmax_axis_);
inner_num_ = bottom[]->count(softmax_axis_ + );
//下面两行scale_dims的shape为[N, 1, H, W]
vector<int> scale_dims = bottom[]->shape();
scale_dims[softmax_axis_] = ;
//scale_ blob的shape为[N, 1, H, W]
scale_.Reshape(scale_dims);
}
(3) softmax_layer.cpp中的Forward_cpu函数:
template <typename Dtype>
void SoftmaxLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[]->cpu_data();
Dtype* top_data = top[]->mutable_cpu_data();
Dtype* scale_data = scale_.mutable_cpu_data();
int channels = bottom[]->shape(softmax_axis_); //channels = C
int dim = bottom[]->count() / outer_num_; //dim = N*C*H*W / N = C*H*W
caffe_copy(bottom[]->count(), bottom_data, top_data); //将bottom_data的blob数据复制给top_data的blob.
// We need to subtract the max to avoid numerical issues, compute the exp,
// and then normalize.
//求channel最大值,存放在scale_ blob中。
for (int i = ; i < outer_num_; ++i) {
// initialize scale_data to the first plane
caffe_copy(inner_num_, bottom_data + i * dim, scale_data);
for (int j = ; j < channels; j++) {
for (int k = ; k < inner_num_; k++) {
scale_data[k] = std::max(scale_data[k],
bottom_data[i * dim + j * inner_num_ + k]);
}
}
// subtraction
//bottom blob数据减去对应channel的最大值
caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, channels, inner_num_,
, -., sum_multiplier_.cpu_data(), scale_data, ., top_data);
// exponentiation
//对每个样本的每个channel数据取e.
caffe_exp<Dtype>(dim, top_data, top_data);
// sum after exp
// 下面的代码实现的是公式(1)
caffe_cpu_gemv<Dtype>(CblasTrans, channels, inner_num_, .,
top_data, sum_multiplier_.cpu_data(), ., scale_data);
// division
for (int j = ; j < channels; j++) {
caffe_div(inner_num_, top_data, scale_data, top_data);
//指针指向下一个数据
top_data += inner_num_;
}
}
}
该函数分为下面几个步骤:
<1> 求每个样本channel的最大值;
<2> softmax的每个输入减去其所在channel的最大值,即caffe_cpu_gemm函数的功能,该函数的原型为:
void caffe_cpu_gemm<float>(const CBLAS_TRANSPOSE TransA, const CBLAS_TRANSPOSE TransB, const int M, const int N, const int K, const float alpha, const float *A, const float *B, const float beta, float *C){
int lda = (TransA == CblasNoTrans) ? K : M;
int ldb = (TransB == CblasNoTrans) ? N : K;
cblas_sgemm(CblasRowMajor, TransA, TransB, M, N, K, alpha, A, lda, B, ldb, beta, C, N);
}
cblas_sgemm函数作用为实现矩阵间的乘法,原型为:
//该函数实现的运算为:C = alpha*A*B + beta*C
//cblasTrans/cblasNoTrans表示对输入矩阵是否转置
//M为矩阵A,C的行数,若转置,则表示转置后的行数
//N为矩阵B、C的列数,若转置,则表示转置后的列数
//K为矩阵A的列数,或B的行数,若转置,则为转置后的列数和行数
//alpha, beta为系数
//A'cols为矩阵A的列数,与是否转置无关
//B'cols为矩阵B的列数,与是否转置无关
1 cblas_sgemm(cblasRowMajor, cblasNoTrans cblasNoTrans, M, N, K, alpha, A, A'cols, B, B'cols, beta, C, C'cols)
形象点说,caffe_cpu_gemm实现的功能为:

<3> 对top blob中的每个数据取e.
<4> 对每个样本的channel求和,与caffe_cpu_gemm不同的是,caffe_cpu_gemv实现的是矩阵与向量的乘法,具体的相乘过程和上面<2>中一样;
<5> 对每个样本而言,其channel的每个值除以该channel的和,也就是caffe_div完成的功能。
(4) softmax_layer.cpp中的Backward_cpu函数:
因为该函数在实际中没有用到,所以没有作过多阅读。
template <typename Dtype>
void SoftmaxLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) {
const Dtype* top_diff = top[]->cpu_diff();
const Dtype* top_data = top[]->cpu_data();
Dtype* bottom_diff = bottom[]->mutable_cpu_diff();
Dtype* scale_data = scale_.mutable_cpu_data();
int channels = top[]->shape(softmax_axis_);
int dim = top[]->count() / outer_num_;
caffe_copy(top[]->count(), top_diff, bottom_diff);
for (int i = ; i < outer_num_; ++i) {
// compute dot(top_diff, top_data) and subtract them from the bottom diff
for (int k = ; k < inner_num_; ++k) {
scale_data[k] = caffe_cpu_strided_dot<Dtype>(channels,
bottom_diff + i * dim + k, inner_num_,
top_data + i * dim + k, inner_num_);
}
// subtraction
caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, channels, inner_num_, ,
-., sum_multiplier_.cpu_data(), scale_data, ., bottom_diff + i * dim);
}
// elementwise multiplication
caffe_mul(top[]->count(), bottom_diff, top_data, bottom_diff);
}
caffe中softmax源码阅读的更多相关文章
- go 中 select 源码阅读
深入了解下 go 中的 select 前言 1.栗子一 2.栗子二 3.栗子三 看下源码实现 1.不存在 case 2.select 中仅存在一个 case 3.select 中存在两个 case,其 ...
- vue中$watch源码阅读笔记
项目中使用了vue,一直在比较computed和$watch的使用场景,今天周末抽时间看了下vue中$watch的源码部分,也查阅了一些别人的文章,暂时把自己的笔记记录于此,供以后查阅: 实现一个简单 ...
- 【tensorflow使用笔记三】:tensorflow tutorial中的源码阅读
https://blog.csdn.net/victoriaw/article/details/61195620#t0 input_data 没用的另一种解决方法:tensorflow1.8版本及以上 ...
- caffe中batch norm源码阅读
1. batch norm 输入batch norm层的数据为[N, C, H, W], 该层计算得到均值为C个,方差为C个,输出数据为[N, C, H, W]. <1> 形象点说,均值的 ...
- Caffe源码阅读(1) 全连接层
Caffe源码阅读(1) 全连接层 发表于 2014-09-15 | 今天看全连接层的实现.主要看的是https://github.com/BVLC/caffe/blob/master/src ...
- caffe-windows中classification.cpp的源码阅读
caffe-windows中classification.cpp的源码阅读 命令格式: usage: classification string(模型描述文件net.prototxt) string( ...
- 源码阅读笔记 - 1 MSVC2015中的std::sort
大约寒假开始的时候我就已经把std::sort的源码阅读完毕并理解其中的做法了,到了寒假结尾,姑且把它写出来 这是我的第一篇源码阅读笔记,以后会发更多的,包括算法和库实现,源码会按照我自己的代码风格格 ...
- 源码阅读经验谈-slim,darknet,labelimg,caffe(1)
本文首先谈自己的源码阅读体验,然后给几个案例解读,选的例子都是比较简单.重在说明我琢磨的点线面源码阅读方法.我不是专业架构师,是从一个深度学习算法工程师的角度来谈的,不专业的地方请大家轻拍. 经常看别 ...
- SpringMVC源码阅读:Controller中参数解析
1.前言 SpringMVC是目前J2EE平台的主流Web框架,不熟悉的园友可以看SpringMVC源码阅读入门,它交代了SpringMVC的基础知识和源码阅读的技巧 本文将通过源码(基于Spring ...
随机推荐
- 小斌之SpringCloud 开发某某交易所
最近一个月都在弄某某交易所,让之前学的东西能够用上,在这里分享一下收货的东西吧 #### 简介 系统是进行了二次开发,用的是ZTuo开源框架第一个版本,节省了很多时间(坑也挺多,哈哈哈),文章结尾贴源 ...
- python+selenium一:对浏览器的常规操作
# 1.打开Firefox浏览器 from selenium import webdriverdriver = webdriver.Firefox()driver.get("https:// ...
- Day 26 网络基础 3
tcpdump抓包 tcpdump -i eth0 port 80 -nn -S -i 指定网卡 port 指定端口号 http 80:ftp 21 :ssh 22:telnet 23:smtp 25 ...
- [C++] C++中的常用库
转载自:C++常用库 C++ 资源大全 关于 C++ 框架.库和资源的一些汇总列表,内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包 ...
- 第六届蓝桥杯java b组第五题
九数组分数 1,2,3…9 这九个数字组成一个分数,其值恰好为1/3,如何组法? 下面的程序实现了该功能,请填写划线部分缺失的代码. public class A { public static vo ...
- 一个有意思的js块作用域问题
1.问题 首先把问题放出来,昨天看了一个掘友发的一个问题,然后跟我同事一起研究了一下,没找出来是为什么,然后我回来一直在想为什么,然后各种找资料研究,从各个方面找为什么,比如js上下文,作用域,js垃 ...
- java基础面试集结
1.hashMap实现原理及相关问题 :https://blog.csdn.net/h1130189083/article/details/78303865
- Dockerfile 构建镜像
一.使用dockerfile构建镜像 基本结构: a.设置基础镜像 当前镜像继承于的基础镜像 FROM centos:latest b.设置维护者信息 没有固定格式 c.设置需要添加到容器中的文件 ...
- ELK 学习笔记之 elasticsearch 基本查询
elasticsearch 基本查询: 基本查询: term查询: terms查询: from和size查询: match查询: match_all查询: match_phrase查询: multi_ ...
- Drill 学习笔记之 与Mongo性能测试比较
性能测试: 竟然用了30秒.... 如果使用mongo 客户端只有1-2秒 感觉Drill是把数据拉过来再进行sql优化,并没有使用Mongo的API,效率低下让人失望...