提交 6f658bb7 编写于 作者: D dangqingqing

Clean code and update doc.

上级 1d7c03e7
...@@ -126,11 +126,11 @@ class LSTMOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -126,11 +126,11 @@ class LSTMOpMaker : public framework::OpProtoAndCheckerMaker {
" - Bias = {b_c, b_i, b_f, b_o, W_ic, W_fc, W_oc}.") " - Bias = {b_c, b_i, b_f, b_o, W_ic, W_fc, W_oc}.")
.AsDispensable(); .AsDispensable();
AddOutput("Hidden", AddOutput("Hidden",
"(LoDTensor) the hidden state lod tensor of LSTM operator. " "(LoDTensor) the hidden state of LSTM operator. "
"The shape and lod is the same with the `Input`."); "The shape is (T x D), and lod is the same with the `Input`.");
AddOutput("Cell", AddOutput("Cell",
"(LoDTensor) the cell state lod tensor of LSTM operator. " "(LoDTensor) the cell state of LSTM operator. "
"The shape and lod is the same with the `Input`."); "The shape is (T x D), and lod is the same with the `Input`.");
AddOutput("BatchGate", AddOutput("BatchGate",
"(LoDTensor) This LoDTensor contains input gate, forget gate " "(LoDTensor) This LoDTensor contains input gate, forget gate "
"and output gate after the nonlinear computation. This " "and output gate after the nonlinear computation. This "
...@@ -141,7 +141,7 @@ class LSTMOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -141,7 +141,7 @@ class LSTMOpMaker : public framework::OpProtoAndCheckerMaker {
"in the raw input.") "in the raw input.")
.AsIntermediate(); .AsIntermediate();
AddOutput("BatchCellPreAct", AddOutput("BatchCellPreAct",
"(LoDTensor) This LoDTensor is get in the forward and used " "(LoDTensor) This LoDTensor is got in the forward and used "
"in the backward.") "in the backward.")
.AsIntermediate(); .AsIntermediate();
AddAttr<bool>("usePeepholes", AddAttr<bool>("usePeepholes",
......
...@@ -155,7 +155,6 @@ class LSTMGradKernel : public framework::OpKernel<T> { ...@@ -155,7 +155,6 @@ class LSTMGradKernel : public framework::OpKernel<T> {
auto* batch_cell_pre_act = ctx.Input<LoDTensor>("BatchCellPreAct"); auto* batch_cell_pre_act = ctx.Input<LoDTensor>("BatchCellPreAct");
auto* hidden_g = ctx.Input<LoDTensor>(framework::GradVarName("Hidden")); auto* hidden_g = ctx.Input<LoDTensor>(framework::GradVarName("Hidden"));
// auto* cell_g = ctx.Input<LoDTensor>(framework::GradVarName("Cell"));
auto* in_g = ctx.Output<LoDTensor>(framework::GradVarName("Input")); auto* in_g = ctx.Output<LoDTensor>(framework::GradVarName("Input"));
auto* weight_g = ctx.Output<Tensor>(framework::GradVarName("Weight")); auto* weight_g = ctx.Output<Tensor>(framework::GradVarName("Weight"));
...@@ -251,7 +250,7 @@ class LSTMGradKernel : public framework::OpKernel<T> { ...@@ -251,7 +250,7 @@ class LSTMGradKernel : public framework::OpKernel<T> {
lstm_grad.gateGrad = gate_g.data<T>(); lstm_grad.gateGrad = gate_g.data<T>();
lstm_grad.outputGrad = out_g.data<T>(); lstm_grad.outputGrad = out_g.data<T>();
if (n != 0) { if (n) {
int bstart_pre = static_cast<int>(batch_starts[n - 1]); int bstart_pre = static_cast<int>(batch_starts[n - 1]);
Tensor cell_pre = batch_cell.Slice(bstart_pre, bstart); Tensor cell_pre = batch_cell.Slice(bstart_pre, bstart);
Tensor cell_pre_g = batch_cell_g.Slice(bstart_pre, bstart); Tensor cell_pre_g = batch_cell_g.Slice(bstart_pre, bstart);
...@@ -292,17 +291,6 @@ class LSTMGradKernel : public framework::OpKernel<T> { ...@@ -292,17 +291,6 @@ class LSTMGradKernel : public framework::OpKernel<T> {
} }
if (bias && bias_g) { if (bias && bias_g) {
/* backward bias */ /* backward bias */
// Following Eigen computation failed for double type on GPU device.
// bias_g->mutable_data<T>(ctx.GetPlace());
// Tensor bias_mat;
// bias_mat.ShareDataWith(*bias_g);
// bias_mat.Resize({1, 4 * frame_size});
// auto bias_g_e = EigenVector<T>::Flatten(bias_mat);
// auto gate_g_e = EigenMatrix<T>::From(batch_gate_g);
// Eigen::array<int, 1> dims{{0}};
// bias_g_e.device(ctx.GetEigenDevice<Place>()) = gate_g_e.sum(dims);
int m = static_cast<int>(batch_gate_g.dims()[0]); int m = static_cast<int>(batch_gate_g.dims()[0]);
int n = static_cast<int>(batch_gate_g.dims()[1]); int n = static_cast<int>(batch_gate_g.dims()[1]);
......
...@@ -157,15 +157,17 @@ class TestLstmOp(OpTest): ...@@ -157,15 +157,17 @@ class TestLstmOp(OpTest):
} }
def test_check_output(self): def test_check_output(self):
self.check_output() self.check_output(atol=1e-8)
#TODO(qingqing) add more unit testing case #TODO(qingqing) add more unit testing case
def test_check_grad(self): def test_check_grad(self):
# TODO(qingqing) remove folowing two lines after the check_grad is refined. # TODO(qingqing) remove folowing lines after the check_grad is refined.
self.outputs['BatchGate'] = None N = len(self.lod[0]) - 1
self.outputs['BatchCellPreAct'] = None self.outputs['BatchGate'] = np.zeros((N, 4 * self.D)).astype('float64')
self.outputs['BatchCellPreAct'] = np.zeros(
(N, self.D)).astype('float64')
self.check_grad( self.check_grad(
['Input', 'Weight', 'Bias'], ['Hidden'], max_relative_error=0.02) ['Input', 'Weight', 'Bias'], ['Hidden'], max_relative_error=5e-4)
class TestLstmOpHasNoInitial(TestLstmOp): class TestLstmOpHasNoInitial(TestLstmOp):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册