From af8cb820a767b5e6498e7e38479db762621050f2 Mon Sep 17 00:00:00 2001 From: Yang Yu Date: Mon, 22 Jan 2018 15:32:57 +0800 Subject: [PATCH] Fix bug of nce_op * also div num_samples when return cost of nce_op --- paddle/operators/nce_op.h | 3 ++- python/paddle/v2/fluid/layers/nn.py | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/paddle/operators/nce_op.h b/paddle/operators/nce_op.h index e6b496f78..86fa13a64 100644 --- a/paddle/operators/nce_op.h +++ b/paddle/operators/nce_op.h @@ -197,7 +197,8 @@ class NCEGradKernel : public framework::OpKernel { // get d_x auto d_x = context.Output(framework::GradVarName("Input")); if (d_x != nullptr) { - d_x->mutable_data(context.GetPlace()); + auto* d_x_data = d_x->mutable_data(context.GetPlace()); + std::fill(d_x_data, d_x_data + d_x->numel(), 0.0); auto d_x_matrix = EigenMatrix::From(*d_x); auto w_matrix = EigenMatrix::From(*(context.Input("Weight"))); for (int64_t i = 0; i < sample_labels->numel(); ++i) { diff --git a/python/paddle/v2/fluid/layers/nn.py b/python/paddle/v2/fluid/layers/nn.py index 668f7788d..9684c4de0 100644 --- a/python/paddle/v2/fluid/layers/nn.py +++ b/python/paddle/v2/fluid/layers/nn.py @@ -2001,9 +2001,15 @@ def nce(input, sample_logits = helper.create_tmp_variable(dtype=input.dtype) sample_labels = helper.create_tmp_variable(dtype=label.dtype) - attrs = {'num_total_classes': int(num_total_classes)} - if num_neg_samples is not None: - attrs['num_neg_samples'] = int(num_neg_samples) + if num_neg_samples is None: + num_neg_samples = 10 + else: + num_neg_samples = int(num_neg_samples) + + attrs = { + 'num_total_classes': int(num_total_classes), + 'num_neg_samples': num_neg_samples + } helper.append_op( type='nce', @@ -2020,4 +2026,4 @@ def nce(input, 'SampleLabels': sample_labels }, attrs=attrs) - return cost + return cost / (num_neg_samples + 1) -- GitLab