diff --git a/paddle/operators/ctc_edit_distance_op.cc b/paddle/operators/ctc_edit_distance_op.cc index 7b45ccc72e9345767899ca873f788aa401505841..fae5cfc117959fc2229ba71c5a55dffb854abc03 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 d0494b4b1b1a4f65635e27d5325e7f232e6aca53..a52960f1ef762c934d3f5060ae2265c4cc7a88f0 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 a6d9dfdf067d0135a793206d0fc10bd1f98a49ca..8a6b0b5390e4b379ef35e4c05e5b1283e7cf67fe 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()