From abfd9fdd22c135e3e4048ef1d7fdf05e86a8939f Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Fri, 22 Sep 2017 23:28:28 +0800 Subject: [PATCH] add clip to avoid log zero and nan --- paddle/math/Matrix.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/paddle/math/Matrix.cpp b/paddle/math/Matrix.cpp index 0023b4d0f..588310fbe 100644 --- a/paddle/math/Matrix.cpp +++ b/paddle/math/Matrix.cpp @@ -3637,7 +3637,7 @@ void CpuMatrix::oneHotCrossEntropy(Matrix& output, IVector& label) { for (size_t i = 0; i < numSamples; ++i, out += dim) { CHECK_GE(lbl[i], 0); CHECK_LT((size_t)lbl[i], dim); - cost[i] = -std::log(out[lbl[i]]); + cost[i] = -std::log(std::max(out[lbl[i]], real(FLT_MIN))); } } @@ -3652,7 +3652,7 @@ void CpuMatrix::oneHotCrossEntropyBp(Matrix& output, IVector& label) { real* grad = getData(); int* lbl = label.getData(); for (size_t i = 0; i < numSamples; ++i, out += dim, grad += dim) { - grad[lbl[i]] -= 1 / out[lbl[i]]; + grad[lbl[i]] -= 1 / std::max(out[lbl[i]], real(FLT_MIN)); } } -- GitLab