文件路径:caffe-master_github/include/caffe/

Backward函数:

template <typename Dtype>
inline void Layer<Dtype>::Backward(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down,
    const vector<Blob<Dtype>*>& bottom) {
  switch (Caffe::mode()) {
  case Caffe::CPU:
/*根据blob top 的error 梯度(diff)计算bottom 的 error 梯度。 propagate_down 是长度 和bottom 相同的vector ,用于控制是否需要对对应的bottom 元素传播梯度。具体layer具体定义。*/
    Backward_cpu(top, propagate_down, bottom);
    break;
  case Caffe::GPU:
    Backward_gpu(top, propagate_down, bottom);
    break;
  default:
    LOG(FATAL) << "Unknown caffe mode.";
  }
}

SetLossWeights函数:

 inline void SetLossWeights(const vector<Blob<Dtype>*>& top) {
    const int num_loss_weights = layer_param_.loss_weight_size();
    if (num_loss_weights) {
      CHECK_EQ(top.size(), num_loss_weights) << "loss_weight must be "
          "unspecified or specified once per top blob.";
      for (int top_id = 0; top_id < top.size(); ++top_id) {
        const Dtype loss_weight = layer_param_.loss_weight(top_id);
        if (loss_weight == Dtype(0)) { continue; }
        this->set_loss(top_id, loss_weight);
        const int count = top[top_id]->count();
        Dtype* loss_multiplier = top[top_id]->mutable_cpu_diff();
        caffe_set(count, loss_weight, loss_multiplier); //将loss_multiplier设为loss_weight
      }
    }
  }

loss_weight分析:
loss_weight一般只对带有loss的层有意义。一个网络可以有多个loss层,而且每个loss层可以计算多个loss(每个loss放在单独的top blob中,这种情况在caffe中没出现过,caffe的loss层一般只有一个loss,放在top[0]对应的blob里)。整个网络的损失就是这些loss的和,loss_weight指的就是求和过程中每个loss的权重,可以在定义layer的时候加上loss_weight这个参数来指定,设置的时候该层loss_weight的个数需要与top的个数相同,否则就不设置。默认情况下,普通层没有loss_weight, loss层的loss_weight为1。
对于caffe中的loss层,计算出的loss放在top[0]指向的blob里,该blob里的diff存放的就是loss对应的loss_weight,传递过程在Layer的SetUp里通过调用SetLossWeights(top)完成

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐