| 25 | |
| 26 | template <typename Dtype> |
| 27 | void SoftmaxLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, |
| 28 | const vector<Blob<Dtype>*>& top) { |
| 29 | const Dtype* bottom_data = bottom[0]->cpu_data(); |
| 30 | Dtype* top_data = top[0]->mutable_cpu_data(); |
| 31 | Dtype* scale_data = scale_.mutable_cpu_data(); |
| 32 | int channels = bottom[0]->shape(softmax_axis_); |
| 33 | int dim = bottom[0]->count() / outer_num_; |
| 34 | caffe_copy(bottom[0]->count(), bottom_data, top_data); |
| 35 | // We need to subtract the max to avoid numerical issues, compute the exp, |
| 36 | // and then normalize. |
| 37 | for (int i = 0; i < outer_num_; ++i) { |
| 38 | // initialize scale_data to the first plane |
| 39 | caffe_copy(inner_num_, bottom_data + i * dim, scale_data); |
| 40 | for (int j = 0; j < channels; j++) { |
| 41 | for (int k = 0; k < inner_num_; k++) { |
| 42 | scale_data[k] = std::max(scale_data[k], |
| 43 | bottom_data[i * dim + j * inner_num_ + k]); |
| 44 | } |
| 45 | } |
| 46 | // subtraction |
| 47 | caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, channels, inner_num_, |
| 48 | 1, -1., sum_multiplier_.cpu_data(), scale_data, 1., top_data); |
| 49 | // exponentiation |
| 50 | caffe_exp<Dtype>(dim, top_data, top_data); |
| 51 | // sum after exp |
| 52 | caffe_cpu_gemv<Dtype>(CblasTrans, channels, inner_num_, 1., |
| 53 | top_data, sum_multiplier_.cpu_data(), 0., scale_data); |
| 54 | // division |
| 55 | for (int j = 0; j < channels; j++) { |
| 56 | caffe_div(inner_num_, top_data, scale_data, top_data); |
| 57 | top_data += inner_num_; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | template <typename Dtype> |
| 63 | void SoftmaxLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, |
nothing calls this directly
no test coverage detected