提交 3afb9dc8 编写于 作者: C caoying03

use double in unittest.

上级 bce4f7d6
......@@ -195,8 +195,6 @@ class LinearChainCRFOp : public framework::OperatorWithKernel {
// is the sequence number in a mini-batch. The dimension set here should be
// resized to its correct size in the function Compute.
ctx->SetOutputDim("LogLikelihood", {emission_dims[0], 1});
ctx->ShareLoD("Emission", /*->*/ "EmissionExps");
}
protected:
......@@ -402,7 +400,7 @@ class LinearChainCRFGradOp : public framework::OperatorWithKernel {
// operator is determined by its input "EmissionExps".
framework::DataType IndicateDataType(
const framework::ExecutionContext& ctx) const override {
return framework::ToDataType(ctx.Input<LoDTensor>("EmissionExps")->type());
return framework::ToDataType(ctx.Input<LoDTensor>("LogLikelihood")->type());
}
};
......@@ -562,7 +560,9 @@ REGISTER_OP(linear_chain_crf, ops::LinearChainCRFOp, ops::LinearChainCRFOpMaker,
linear_chain_crf_grad, ops::LinearChainCRFGradOp);
REGISTER_OP_CPU_KERNEL(
linear_chain_crf,
ops::LinearChainCRFOpKernel<paddle::platform::CPUPlace, float>);
ops::LinearChainCRFOpKernel<paddle::platform::CPUPlace, float>,
ops::LinearChainCRFOpKernel<paddle::platform::CPUPlace, double>);
REGISTER_OP_CPU_KERNEL(
linear_chain_crf_grad,
ops::LinearChainCRFGradOpKernel<paddle::platform::CPUPlace, float>);
ops::LinearChainCRFGradOpKernel<paddle::platform::CPUPlace, float>,
ops::LinearChainCRFGradOpKernel<paddle::platform::CPUPlace, double>);
......@@ -32,7 +32,7 @@ class LinearChainCrfForward(object):
# alpha is a memo table in dynamic programming to caculate
# nomalization factor.
self.alpha = np.zeros(
(seq_start_positions[-1], self.tag_num), dtype="float32")
(seq_start_positions[-1], self.tag_num), dtype="float64")
self.log_likelihood = np.zeros((self.seq_num, 1))
def _l1_norm(self, x):
......@@ -92,12 +92,12 @@ class TestLinearChainCrfOp(OpTest):
for i in range(SEQ_NUM):
lod[-1].append(lod[-1][-1] + random.randint(1, MAX_SEQ_LEN))
emission = np.random.uniform(-1, 1,
[lod[-1][-1], TAG_NUM]).astype("float32")
[lod[-1][-1], TAG_NUM]).astype("float64")
emission_row_max = np.amax(emission, axis=1, keepdims=True)
emission_exps = np.exp(emission - emission_row_max)
transition = np.random.uniform(-0.5, 0.5,
[TAG_NUM + 2, TAG_NUM]).astype("float32")
[TAG_NUM + 2, TAG_NUM]).astype("float64")
transition_exps = np.exp(transition)
labels = np.random.randint(
......@@ -128,17 +128,11 @@ class TestLinearChainCrfOp(OpTest):
self.check_output()
def test_check_grad(self):
self.check_grad(
["Emission", "Transition"],
"LogLikelihood",
max_relative_error=0.05)
self.check_grad(["Emission", "Transition"], "LogLikelihood")
def test_check_grad_ignore_transition(self):
self.check_grad(
["Emission"],
"LogLikelihood",
max_relative_error=0.05,
no_grad_set=set("Transition"))
["Emission"], "LogLikelihood", no_grad_set=set("Transition"))
if __name__ == "__main__":
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册