caffe源码分析:layer.hpp分析
生活随笔
收集整理的這篇文章主要介紹了
caffe源码分析:layer.hpp分析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文件路徑: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)完成
總結
以上是生活随笔為你收集整理的caffe源码分析:layer.hpp分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: string类的erase函数属于stl
- 下一篇: 10038 mysql,关于MySql