| 39 | |
| 40 | template <typename Dtype> |
| 41 | void LSTMUnitLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, |
| 42 | const vector<Blob<Dtype>*>& top) { |
| 43 | const int num = bottom[0]->shape(1); |
| 44 | const int x_dim = hidden_dim_ * 4; |
| 45 | const Dtype* C_prev = bottom[0]->cpu_data(); |
| 46 | const Dtype* X = bottom[1]->cpu_data(); |
| 47 | const Dtype* cont = bottom[2]->cpu_data(); |
| 48 | Dtype* C = top[0]->mutable_cpu_data(); |
| 49 | Dtype* H = top[1]->mutable_cpu_data(); |
| 50 | for (int n = 0; n < num; ++n) { |
| 51 | for (int d = 0; d < hidden_dim_; ++d) { |
| 52 | const Dtype i = sigmoid(X[d]); |
| 53 | const Dtype f = (*cont == 0) ? 0 : |
| 54 | (*cont * sigmoid(X[1 * hidden_dim_ + d])); |
| 55 | const Dtype o = sigmoid(X[2 * hidden_dim_ + d]); |
| 56 | const Dtype g = tanh(X[3 * hidden_dim_ + d]); |
| 57 | const Dtype c_prev = C_prev[d]; |
| 58 | const Dtype c = f * c_prev + i * g; |
| 59 | C[d] = c; |
| 60 | const Dtype tanh_c = tanh(c); |
| 61 | H[d] = o * tanh_c; |
| 62 | } |
| 63 | C_prev += hidden_dim_; |
| 64 | X += x_dim; |
| 65 | C += hidden_dim_; |
| 66 | H += hidden_dim_; |
| 67 | ++cont; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | template <typename Dtype> |
| 72 | void LSTMUnitLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, |
nothing calls this directly
no test coverage detected