|
|
|
@ -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__":
|
|
|
|
|