From b7a4e3d72c58a6ca39b6434888c8144558d0bdbb Mon Sep 17 00:00:00 2001 From: Yibing Liu Date: Thu, 2 Nov 2017 12:12:31 +0800 Subject: [PATCH] rename some variables in ctc_edit_distance_op --- paddle/operators/ctc_edit_distance_op.cc | 4 ++-- paddle/operators/ctc_edit_distance_op.h | 21 +++++++++---------- .../framework/tests/test_ctc_edit_distance.py | 8 ++----- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/paddle/operators/ctc_edit_distance_op.cc b/paddle/operators/ctc_edit_distance_op.cc index 7b45ccc72..fae5cfc11 100644 --- a/paddle/operators/ctc_edit_distance_op.cc +++ b/paddle/operators/ctc_edit_distance_op.cc @@ -38,11 +38,11 @@ class CTCEditDistanceOpMaker : public framework::OpProtoAndCheckerMaker { "(2-D tensor with shape [M x 1]) The indices for " "hypothesis string"); AddInput("X2", - "(2-D tensor with shape [batch_size x 1]) The indices " + "(2-D tensor with shape [N x 1]) The indices " "for reference string."); AddAttr("normalized", "(bool, default false) Indicated whether " - "normalize. the Output(Out) by the length of reference " + "normalize the Output(Out) by the length of reference " "string (X2).") .SetDefault(false); AddOutput("Out", diff --git a/paddle/operators/ctc_edit_distance_op.h b/paddle/operators/ctc_edit_distance_op.h index d0494b4b1..a52960f1e 100644 --- a/paddle/operators/ctc_edit_distance_op.h +++ b/paddle/operators/ctc_edit_distance_op.h @@ -47,20 +47,19 @@ class CTCEditDistanceKernel : public framework::OpKernel { auto dist = dist_t.data(); auto x1 = x1_t->data(); auto x2 = x2_t->data(); - for (int i = 0; i < m + 1; ++i) { - dist[i * (n + 1)] = i; // dist[i][0] = i; + for (size_t i = 0; i < m + 1; ++i) { + dist[i * (n + 1)] = i; } - for (int j = 0; j < n + 1; ++j) { - dist[j] = j; // dist[0][j] = j; + for (size_t j = 0; j < n + 1; ++j) { + dist[j] = j; } - for (int i = 1; i < m + 1; ++i) { - for (int j = 1; j < n + 1; ++j) { + for (size_t i = 1; i < m + 1; ++i) { + for (size_t j = 1; j < n + 1; ++j) { int cost = x1[i - 1] == x2[j - 1] ? 0 : 1; - int deletions = dist[(i - 1) * (n + 1) + j] + 1; - int insertions = dist[i * (n + 1) + (j - 1)] + 1; - int substitutions = dist[(i - 1) * (n + 1) + (j - 1)] + cost; - dist[i * (n + 1) + j] = - std::min(deletions, std::min(insertions, substitutions)); + int dels = dist[(i - 1) * (n + 1) + j] + 1; + int ins = dist[i * (n + 1) + (j - 1)] + 1; + int subs = dist[(i - 1) * (n + 1) + (j - 1)] + cost; + dist[i * (n + 1) + j] = std::min(dels, std::min(ins, subs)); } } distance = dist[m * (n + 1) + n]; diff --git a/python/paddle/v2/framework/tests/test_ctc_edit_distance.py b/python/paddle/v2/framework/tests/test_ctc_edit_distance.py index a6d9dfdf0..8a6b0b539 100644 --- a/python/paddle/v2/framework/tests/test_ctc_edit_distance.py +++ b/python/paddle/v2/framework/tests/test_ctc_edit_distance.py @@ -6,9 +6,9 @@ from op_test import OpTest def Levenshtein(hyp, ref): """ Compute the Levenshtein distance between two strings. - :param hyp: + :param hyp: hypothesis string in index :type hyp: list - :param ref: + :param ref: reference string in index :type ref: list """ m = len(hyp) @@ -44,7 +44,6 @@ class TestCTCEditDistanceOp(OpTest): distance = Levenshtein(hyp=x1, ref=x2) if normalized is True: distance = distance / len(x2) - print "distance = ", distance self.attrs = {'normalized': normalized} self.inputs = {'X1': x1, 'X2': x2} self.outputs = {'Out': distance} @@ -54,7 +53,4 @@ class TestCTCEditDistanceOp(OpTest): if __name__ == '__main__': - #x1 = ['c', 'a', 'f', 'e'] - #x2 = ['c', 'o', 'f', 'f', 'e', 'e'] - #print Levenshtein(x1, x2) unittest.main() -- GitLab