| 67 | } |
| 68 | |
| 69 | const std::pair<Tensor, vector<Tensor>> Activation::Backward( |
| 70 | int flag, const Tensor& grad) { |
| 71 | vector<Tensor> param_grad; |
| 72 | CHECK(!buf_.empty()); |
| 73 | // inout means either input or output, but only one is valid for an |
| 74 | // activation. |
| 75 | Tensor input_grad, inout = buf_.top(); |
| 76 | buf_.pop(); |
| 77 | if (mode_ == "sigmoid") |
| 78 | input_grad = grad * inout * (inout * (-1.f) + 1.f); |
| 79 | else if (mode_ == "tanh") |
| 80 | input_grad = grad * (inout * inout * (-1.f) + 1.f); |
| 81 | else if (mode_ == "relu") |
| 82 | input_grad = grad * (inout > 0.f) + (inout <= 0.f) * neg_slope_; |
| 83 | else LOG(FATAL) << "Unkown activation: " << mode_; |
| 84 | return std::make_pair(input_grad, param_grad); |
| 85 | } |
| 86 | |
| 87 | } // namespace singa |