交叉熵损失函数的简单介绍的链接
下面我们就介绍一下caffe里面实现交叉熵的流程:
首先:下面这个式子就是交叉熵的损失表达式
Dtype loss = 0;
for (int i = 0; i < count; ++i) {
loss -= input_data[i] * (target[i] - (input_data[i] >= 0)) -
log(1 + exp(input_data[i] - 2 * input_data[i] * (input_data[i] >= 0)));
}
top[0]->mutable_cpu_data()[0] = loss / num;
caffe loss理解
介绍完理论和注意的内容,接下来就贴代码和一些注释:
SigmoidCrossEntropyLossLayer的定义
template <typename Dtype>
class SigmoidCrossEntropyLossLayer : public LossLayer {
public:
explicit SigmoidCrossEntropyLossLayer(const LayerParameter& param)
: LossLayer(param),
sigmoid_layer_(new SigmoidLayer(param)),
sigmoid_output_(new Blob()) {}
virtual void LayerSetUp(const vector *>& bottom,
const vector *>& top);
virtual void Reshape(const vector *>& bottom,
const vector *>& top);
virtual inline const char* type() const { return "SigmoidCrossEntropyLoss"; }
protected:
virtual void Forward_cpu(const vector *>& bottom,
const vector *>& top);
virtual void Backward_cpu(const vector *>& top,
const vector<bool>& propagate_down, const vector *>& bottom);
virtual void Backward_gpu(const vector *>& top,
const vector<bool>& propagate_down, const vector *>& bottom);
/// The internal SigmoidLayer used to map predictions to probabilities.
shared_ptr > sigmoid_layer_;//用于生成生成sigmoid结果
/// sigmoid_output stores the output of the SigmoidLayer.
shared_ptr > sigmoid_output_;//指向sigmoid的输出
/// bottom vector holder to call the underlying SigmoidLayer::Forward
vector *> sigmoid_bottom_vec_;//sigmoid的输入
/// top vector holder to call the underlying SigmoidLayer::Forward
vector *> sigmoid_top_vec_; //sigmoid的输出
};
SigmoidCrossEntropyLossLayer类的成员函数实现
template <typename Dtype>
void SigmoidCrossEntropyLossLayer::LayerSetUp(
const vector *>& bottom, const vector *>& top) {
LossLayer::LayerSetUp(bottom, top);
sigmoid_bottom_vec_.clear();
sigmoid_bottom_vec_.push_back(bottom[0]);
sigmoid_top_vec_.clear();
sigmoid_top_vec_.push_back(sigmoid_output_.get());
sigmoid_layer_->SetUp(sigmoid_bottom_vec_, sigmoid_top_vec_);
}
template <typename Dtype>
void SigmoidCrossEntropyLossLayer::Reshape(
const vector *>& bottom, const vector *>& top) {
LossLayer::Reshape(bottom, top);
CHECK_EQ(bottom[0]->count(), bottom[1]->count()) <<
"SIGMOID_CROSS_ENTROPY_LOSS layer inputs must have the same count.";
sigmoid_layer_->Reshape(sigmoid_bottom_vec_, sigmoid_top_vec_);
}
template <typename Dtype>
void SigmoidCrossEntropyLossLayer::Forward_cpu(
const vector *>& bottom, const vector *>& top) {
// The forward pass computes the sigmoid outputs.
sigmoid_bottom_vec_[0] = bottom[0];
sigmoid_layer_->Forward(sigmoid_bottom_vec_, sigmoid_top_vec_);
// Compute the loss (negative log likelihood)
const int count = bottom[0]->count();
const int num = bottom[0]->num();
// Stable version of loss computation from input data
const Dtype* input_data = bottom[0]->cpu_data();
const Dtype* target = bottom[1]->cpu_data();
Dtype loss = 0;
for (int i = 0; i < count; ++i) {
loss -= input_data[i] * (target[i] - (input_data[i] >= 0)) -
log(1 + exp(input_data[i] - 2 * input_data[i] * (input_data[i] >= 0)));
}
top[0]->mutable_cpu_data()[0] = loss / num;
}
template <typename Dtype>
void SigmoidCrossEntropyLossLayer::Backward_cpu(
const vector *>& top, const vector<bool>& propagate_down,
const vector *>& bottom) {
if (propagate_down[1]) {
LOG(FATAL) << this->type()
<< " Layer cannot backpropagate to label inputs.";
}
if (propagate_down[0]) {
// First, compute the diff
const int count = bottom[0]->count();
const int num = bottom[0]->num();
const Dtype* sigmoid_output_data = sigmoid_output_->cpu_data();
const Dtype* target = bottom[1]->cpu_data();
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
caffe_sub(count, sigmoid_output_data, target, bottom_diff);
// Scale down gradient
const Dtype loss_weight = top[0]->cpu_diff()[0];
caffe_scal(count, loss_weight / num, bottom_diff);
}
}