From e3223ad7044f7c32af3069c6beefb9b61937315d Mon Sep 17 00:00:00 2001 From: Feiyu Chan Date: Tue, 7 Jul 2020 10:05:44 +0800 Subject: [PATCH] fix nce sample code for python3, test=develop (#25103) fix nce sample code for python3 compatability. --- python/paddle/fluid/layers/loss.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/python/paddle/fluid/layers/loss.py b/python/paddle/fluid/layers/loss.py index 805c8f81688..4217a98798e 100644 --- a/python/paddle/fluid/layers/loss.py +++ b/python/paddle/fluid/layers/loss.py @@ -725,7 +725,7 @@ def nce(input, window_size = 5 words = [] - for i in xrange(window_size): + for i in range(window_size): words.append(fluid.data( name='word_{0}'.format(i), shape=[-1, 1], dtype='int64')) @@ -733,7 +733,7 @@ def nce(input, label_word = int(window_size / 2) + 1 embs = [] - for i in xrange(window_size): + for i in range(window_size): if i == label_word: continue @@ -746,14 +746,14 @@ def nce(input, num_total_classes=dict_size, param_attr='nce.w_0', bias_attr='nce.b_0') - #or use custom distribution - dist = np.array([0.05,0.5,0.1,0.3,0.05]) - loss = fluid.layers.nce(input=embs, label=words[label_word], - num_total_classes=5, param_attr='nce.w_1', - bias_attr='nce.b_1', - num_neg_samples=3, - sampler="custom_dist", - custom_dist=dist) + #or use custom distribution + dist = np.array([0.05,0.5,0.1,0.3,0.05]) + loss = fluid.layers.nce(input=embs, label=words[label_word], + num_total_classes=5, param_attr='nce.w_1', + bias_attr='nce.b_1', + num_neg_samples=3, + sampler="custom_dist", + custom_dist=dist) """ helper = LayerHelper('nce', **locals()) check_variable_and_dtype(input, 'input', ['float32', 'float64'], 'nce') -- GitLab