MCPcopy Create free account
hub / github.com/BVLC/caffe / Backward_cpu

Method Backward_cpu

src/caffe/layers/lstm_unit_layer.cpp:72–121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

70
71template <typename Dtype>
72void LSTMUnitLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
73 const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
74 CHECK(!propagate_down[2]) << "Cannot backpropagate to sequence indicators.";
75 if (!propagate_down[0] && !propagate_down[1]) { return; }
76
77 const int num = bottom[0]->shape(1);
78 const int x_dim = hidden_dim_ * 4;
79 const Dtype* C_prev = bottom[0]->cpu_data();
80 const Dtype* X = bottom[1]->cpu_data();
81 const Dtype* cont = bottom[2]->cpu_data();
82 const Dtype* C = top[0]->cpu_data();
83 const Dtype* H = top[1]->cpu_data();
84 const Dtype* C_diff = top[0]->cpu_diff();
85 const Dtype* H_diff = top[1]->cpu_diff();
86 Dtype* C_prev_diff = bottom[0]->mutable_cpu_diff();
87 Dtype* X_diff = bottom[1]->mutable_cpu_diff();
88 for (int n = 0; n < num; ++n) {
89 for (int d = 0; d < hidden_dim_; ++d) {
90 const Dtype i = sigmoid(X[d]);
91 const Dtype f = (*cont == 0) ? 0 :
92 (*cont * sigmoid(X[1 * hidden_dim_ + d]));
93 const Dtype o = sigmoid(X[2 * hidden_dim_ + d]);
94 const Dtype g = tanh(X[3 * hidden_dim_ + d]);
95 const Dtype c_prev = C_prev[d];
96 const Dtype c = C[d];
97 const Dtype tanh_c = tanh(c);
98 Dtype* c_prev_diff = C_prev_diff + d;
99 Dtype* i_diff = X_diff + d;
100 Dtype* f_diff = X_diff + 1 * hidden_dim_ + d;
101 Dtype* o_diff = X_diff + 2 * hidden_dim_ + d;
102 Dtype* g_diff = X_diff + 3 * hidden_dim_ + d;
103 const Dtype c_term_diff =
104 C_diff[d] + H_diff[d] * o * (1 - tanh_c * tanh_c);
105 *c_prev_diff = c_term_diff * f;
106 *i_diff = c_term_diff * g * i * (1 - i);
107 *f_diff = c_term_diff * c_prev * f * (1 - f);
108 *o_diff = H_diff[d] * tanh_c * o * (1 - o);
109 *g_diff = c_term_diff * i * (1 - g * g);
110 }
111 C_prev += hidden_dim_;
112 X += x_dim;
113 C += hidden_dim_;
114 H += hidden_dim_;
115 C_diff += hidden_dim_;
116 H_diff += hidden_dim_;
117 X_diff += x_dim;
118 C_prev_diff += hidden_dim_;
119 ++cont;
120 }
121}
122
123#ifdef CPU_ONLY
124STUB_GPU(LSTMUnitLayer);

Callers

nothing calls this directly

Calls 6

tanhFunction · 0.85
shapeMethod · 0.80
cpu_diffMethod · 0.80
mutable_cpu_diffMethod · 0.80
sigmoidFunction · 0.70
cpu_dataMethod · 0.45

Tested by

no test coverage detected