Skip to content

Commit

Permalink
PReLU always accumulate grad
Browse files Browse the repository at this point in the history
Conflicts:
	src/caffe/layers/prelu_layer.cu
  • Loading branch information
tnarihi committed Mar 12, 2015
1 parent a01dfbf commit 4d3fbd5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/caffe/layers/prelu_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ 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);
// 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
5 changes: 3 additions & 2 deletions src/caffe/layers/prelu_layer.cu
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void PReLULayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
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);
// 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 +106,8 @@ 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_set(this->blobs_[0]->count(), Dtype(dsum), slope_diff);
caffe_gpu_add_scalar(this->blobs_[0]->count(), Dtype(dsum), slope_diff);

This comment has been minimized.

Copy link
@hli2020

hli2020 Jun 1, 2015

@tnarihi Hi, why to change this if channel_shared? (new to caffe, thanks)

This comment has been minimized.

Copy link
@tnarihi

tnarihi Jun 1, 2015

Author Owner

@hli2020 Unlike unshared case, the gradient is accumulated to a scalar temporary variable dsum for a computational batch. To accumulate a gradient to slope_diff, I use caffe_gpu_add_scalar here.

}
}
// Propagate to bottom
Expand Down

0 comments on commit 4d3fbd5

Please sign in to comment.