| 61 | |
| 62 | template <typename Dtype> |
| 63 | void SoftmaxLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, |
| 64 | const vector<bool>& propagate_down, |
| 65 | const vector<Blob<Dtype>*>& bottom) { |
| 66 | const Dtype* top_diff = top[0]->cpu_diff(); |
| 67 | const Dtype* top_data = top[0]->cpu_data(); |
| 68 | Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); |
| 69 | Dtype* scale_data = scale_.mutable_cpu_data(); |
| 70 | int channels = top[0]->shape(softmax_axis_); |
| 71 | int dim = top[0]->count() / outer_num_; |
| 72 | caffe_copy(top[0]->count(), top_diff, bottom_diff); |
| 73 | for (int i = 0; i < outer_num_; ++i) { |
| 74 | // compute dot(top_diff, top_data) and subtract them from the bottom diff |
| 75 | for (int k = 0; k < inner_num_; ++k) { |
| 76 | scale_data[k] = caffe_cpu_strided_dot<Dtype>(channels, |
| 77 | bottom_diff + i * dim + k, inner_num_, |
| 78 | top_data + i * dim + k, inner_num_); |
| 79 | } |
| 80 | // subtraction |
| 81 | caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, channels, inner_num_, 1, |
| 82 | -1., sum_multiplier_.cpu_data(), scale_data, 1., bottom_diff + i * dim); |
| 83 | } |
| 84 | // elementwise multiplication |
| 85 | caffe_mul(top[0]->count(), bottom_diff, top_data, bottom_diff); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | #ifdef CPU_ONLY |
nothing calls this directly
no test coverage detected