Skip to content

Commit

Permalink
PReLU accumulates grad
Browse files Browse the repository at this point in the history
  • Loading branch information
tnarihi authored and shelhamer committed Jun 1, 2015
1 parent e22cd54 commit a02a8ba
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/caffe/layers/prelu_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ void PReLULayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
// keep top_diff unchanged.
if (this->param_propagate_down_[0]) {
Dtype* slope_diff = this->blobs_[0]->mutable_cpu_diff();
caffe_set(this->blobs_[0]->count(), Dtype(0), slope_diff);
for (int i = 0; i < count; ++i) {
int c = (i / dim) % channels / div_factor;
slope_diff[c] += top_diff[i] * bottom_data[i] * (bottom_data[i] <= 0);
Expand Down
6 changes: 2 additions & 4 deletions src/caffe/layers/prelu_layer.cu
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,12 @@ void PReLULayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
bottom_data = bottom_memory_.gpu_data();
}

// Propagte to param
// Propagate to param
// Since to write bottom diff will affect top diff if top and bottom blobs
// are identical (in-place computaion), we first compute param backward to
// keep top_diff unchanged.
if (this->param_propagate_down_[0]) {
Dtype* slope_diff = this->blobs_[0]->mutable_gpu_diff();
// slope_diff is set as 0, then accumulated over batches
caffe_gpu_set<Dtype>(this->blobs_[0]->count(), Dtype(0), slope_diff);
int cdim = channels * dim;
Dtype dsum = 0.;
for (int n = 0; n < bottom[0]->num(); ++n) {
Expand All @@ -106,7 +104,7 @@ void PReLULayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
}
}
if (channel_shared_) {
caffe_gpu_set(this->blobs_[0]->count(), Dtype(dsum), slope_diff);
caffe_gpu_add_scalar(this->blobs_[0]->count(), Dtype(dsum), slope_diff);
}
}
// Propagate to bottom
Expand Down

0 comments on commit a02a8ba

Please sign in to comment.