Dropout caffe源码
GPU和CPU实现的不一样,这里贴的是CPU中的drop out
直接看caffe里面的源码吧:(产生满足伯努利分布的随机数mask,train的时候,data除以p,......
scale_ = 1. / (1. - threshold_);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <br> template < typename Dtype> void DropoutLayer<Dtype>::Forward_cpu( const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { const Dtype* bottom_data = bottom[0]->cpu_data(); Dtype* top_data = top[0]->mutable_cpu_data(); unsigned int * mask = rand_vec_.mutable_cpu_data(); const int count = bottom[0]->count(); if ( this ->phase_ == TRAIN) { // Create random numbers caffe_rng_bernoulli(count, 1. - threshold_, mask); for ( int i = 0; i < count; ++i) { top_data[i] = bottom_data[i] * mask[i] * scale_; } } else { caffe_copy(bottom[0]->count(), bottom_data, top_data); } } template < typename Dtype> void DropoutLayer<Dtype>::Backward_cpu( const vector<Blob<Dtype>*>& top, const vector< bool >& propagate_down, const vector<Blob<Dtype>*>& bottom) { if (propagate_down[0]) { const Dtype* top_diff = top[0]->cpu_diff(); Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); if ( this ->phase_ == TRAIN) { const unsigned int * mask = rand_vec_.cpu_data(); const int count = bottom[0]->count(); for ( int i = 0; i < count; ++i) { bottom_diff[i] = top_diff[i] * mask[i] * scale_; } } else { caffe_copy(top[0]->count(), top_diff, bottom_diff); } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步