| 449 | // functions. |
| 450 | template <typename Dtype> |
| 451 | inline Dtype Layer<Dtype>::Forward(const vector<Blob<Dtype>*>& bottom, |
| 452 | const vector<Blob<Dtype>*>& top) { |
| 453 | // Lock during forward to ensure sequential forward |
| 454 | Lock(); |
| 455 | Dtype loss = 0; |
| 456 | Reshape(bottom, top); |
| 457 | switch (Caffe::mode()) { |
| 458 | case Caffe::CPU: |
| 459 | Forward_cpu(bottom, top); |
| 460 | for (int top_id = 0; top_id < top.size(); ++top_id) { |
| 461 | if (!this->loss(top_id)) { continue; } |
| 462 | const int count = top[top_id]->count(); |
| 463 | const Dtype* data = top[top_id]->cpu_data(); |
| 464 | const Dtype* loss_weights = top[top_id]->cpu_diff(); |
| 465 | loss += caffe_cpu_dot(count, data, loss_weights); |
| 466 | } |
| 467 | break; |
| 468 | case Caffe::GPU: |
| 469 | Forward_gpu(bottom, top); |
| 470 | #ifndef CPU_ONLY |
| 471 | for (int top_id = 0; top_id < top.size(); ++top_id) { |
| 472 | if (!this->loss(top_id)) { continue; } |
| 473 | const int count = top[top_id]->count(); |
| 474 | const Dtype* data = top[top_id]->gpu_data(); |
| 475 | const Dtype* loss_weights = top[top_id]->gpu_diff(); |
| 476 | Dtype blob_loss = 0; |
| 477 | caffe_gpu_dot(count, data, loss_weights, &blob_loss); |
| 478 | loss += blob_loss; |
| 479 | } |
| 480 | #endif |
| 481 | break; |
| 482 | default: |
| 483 | LOG(FATAL) << "Unknown caffe mode."; |
| 484 | } |
| 485 | Unlock(); |
| 486 | return loss; |
| 487 | } |
| 488 | |
| 489 | template <typename Dtype> |
| 490 | inline void Layer<Dtype>::Backward(const vector<Blob<Dtype>*>& top, |