| 50 | |
| 51 | template<typename Dtype> |
| 52 | void BatchReindexLayer<Dtype>::Backward_cpu( |
| 53 | const vector<Blob<Dtype>*>& top, const vector<bool>& propagate_down, |
| 54 | const vector<Blob<Dtype>*>& bottom) { |
| 55 | CHECK(!propagate_down[1]) << "Cannot backprop to index."; |
| 56 | if (!propagate_down[0]) { |
| 57 | return; |
| 58 | } |
| 59 | int inner_dim = bottom[0]->count() / bottom[0]->shape(0); |
| 60 | Dtype* bot_diff = bottom[0]->mutable_cpu_diff(); |
| 61 | const Dtype* permut = bottom[1]->cpu_data(); |
| 62 | const Dtype* top_diff = top[0]->cpu_diff(); |
| 63 | caffe_set(bottom[0]->count(), Dtype(0), bot_diff); |
| 64 | for (int index = 0; index < top[0]->count(); ++index) { |
| 65 | int n = index / (inner_dim); |
| 66 | int in_n = static_cast<int>(permut[n]); |
| 67 | bot_diff[in_n * (inner_dim) + index % (inner_dim)] += top_diff[index]; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #ifdef CPU_ONLY |
| 72 | STUB_GPU(BatchReindexLayer); |