diff --git a/paddle/operators/nce_op.h b/paddle/operators/nce_op.h index e6b496f7896dcb412be8ff096fdccb2f0b682369..86fa13a649ce7fdcaad64e2609ceea2fb4d7e072 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 668f7788db97b9e629d8588a19690933416f428f..9684c4de0e5f76260c7eeab3b4de8f162e4ba337 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)