Softmaxlayer and Softmaxwithlosslayer Code interpretation
Wang Xiao
Let's take a look at the definition of Softmaxwithloss in the Prototext file:
1 Layer {2Name"Loss"3Type"Softmaxwithloss"4Bottom"Fc8"5Bottom"label"6Top"Loss"7}
And look at the Softmaxwithlosslayer. cpp file:
1#include <algorithm>2#include <cfloat>3#include <vector>4 5#include"caffe/layers/softmax_loss_layer.hpp"6#include"caffe/util/math_functions.hpp"7 8 namespaceCaffe {9 TenTemplate <typename dtype> One void Softmaxwithlosslayer<dtype>::layersetup( A Constvector<blob<dtype>*>& Bottom,Constvector<blob<dtype>*>&top) { -Losslayer<dtype>:: Layersetup (bottom, top); -Layerparameter Softmax_param ( This-LAYER_PARAM_); theSoftmax_param.set_type ("Softmax"); -Softmax_layer_ = layerregistry<dtype>:: Createlayer (Softmax_param); - softmax_bottom_vec_.clear (); - softmax_bottom_vec_. push_back (bottom[0]); //bottom[0] deposited into softmax_bottom_vec_; + softmax_top_vec_.clear (); - softmax_top_vec_. Push_back (&prob_); //prob_ deposited into softmax_top_vec_;
+Softmax_layer_->SetUp (softmax_bottom_vec_, softmax_top_vec_);
A
at Has_ignore_label_=//Draw the parameter from layer
- This-Layer_param_.loss_param (). Has_ignore_label ();
- if(HAS_IGNORE_LABEL_) {
-Ignore_label_ = This-Layer_param_.loss_param (). Ignore_label ();
- }
- if(! This->layer_param_.loss_param (). Has_normalization () &&
in This-Layer_param_.loss_param (). Has_normalize ()) {
-Normalization_ = This->layer_param_.loss_param (). Normalize ()?
to Lossparameter_normalizationmode_valid:
+ lossparameter_normalizationmode_batch_size;
-}Else {
theNormalization_ = This-Layer_param_.loss_param (). normalization ();
* }
$}
The next step is to reshape the input data:
1Template <typename dtype>2 voidSoftmaxwithlosslayer<dtype>:: Reshape (3 Constvector<blob<dtype>*>& Bottom,Constvector<blob<dtype>*>&top) {4Losslayer<dtype>:: Reshape (bottom, top);5Softmax_layer_->reshape (softmax_bottom_vec_, softmax_top_vec_);6Softmax_axis_ =7bottom[0]->canonicalaxisindex ( This-Layer_param_.softmax_param (). axis ());8Outer_num_ = bottom[0]->count (0, softmax_axis_);9Inner_num_ = bottom[0]->count (Softmax_axis_ +1);TenCheck_eq (Outer_num_ * inner_num_, bottom[1]->count ()) One<<"Number of labels must match number of predictions;" A<<"e.g., if softmax axis = = 1 and Prediction shape is (N, C, H, W)," -<<"Label count (number of labels) must be n*h*w," -<<"with integer values in {0, 1, ..., C-1}."; the if(Top.size () >=2) { - //Softmax Output -top[1]->reshapelike (*bottom[0]); - } +}
Softmaxlayer and Softmaxwithlosslayer Code interpretation