template <typename Dtype> |
| inline Dtype Layer<Dtype>::Forward(const vector<Blob<Dtype>*>& bottom, |
| const vector<Blob<Dtype>*>& top) { |
| Dtype loss = 0; |
| Reshape(bottom, top); |
| switch (Caffe::mode()) { |
| case Caffe::CPU: |
| Forward_cpu(bottom, top); |
| for (int top_id = 0; top_id < top.size(); ++top_id) { |
| if (!this->loss(top_id)) { continue; } |
| const int count = top[top_id]->count(); |
| const Dtype* data = top[top_id]->cpu_data(); |
| const Dtype* loss_weights = top[top_id]->cpu_diff(); |
| loss += caffe_cpu_dot(count, data, loss_weights); |
| } |
| break; |
| case Caffe::GPU: |
| Forward_gpu(bottom, top); |
| #ifndef CPU_ONLY |
| for (int top_id = 0; top_id < top.size(); ++top_id) { |
| if (!this->loss(top_id)) { continue; } |
| const int count = top[top_id]->count(); |
| const Dtype* data = top[top_id]->gpu_data(); |
| const Dtype* loss_weights = top[top_id]->gpu_diff(); |
| Dtype blob_loss = 0; |
| caffe_gpu_dot(count, data, loss_weights, &blob_loss); |
| loss += blob_loss; |
| } |
| #endif |
| break; |
| default: |
| LOG(FATAL) << "Unknown caffe mode."; |
| } |
| return loss; |
| } |