未验证 提交 e3223ad7 编写于 作者: F Feiyu Chan 提交者: GitHub

fix nce sample code for python3, test=develop (#25103)

fix nce sample code for python3 compatability.
上级 84f9b9ef
...@@ -725,7 +725,7 @@ def nce(input, ...@@ -725,7 +725,7 @@ def nce(input,
window_size = 5 window_size = 5
words = [] words = []
for i in xrange(window_size): for i in range(window_size):
words.append(fluid.data( words.append(fluid.data(
name='word_{0}'.format(i), shape=[-1, 1], dtype='int64')) name='word_{0}'.format(i), shape=[-1, 1], dtype='int64'))
...@@ -733,7 +733,7 @@ def nce(input, ...@@ -733,7 +733,7 @@ def nce(input,
label_word = int(window_size / 2) + 1 label_word = int(window_size / 2) + 1
embs = [] embs = []
for i in xrange(window_size): for i in range(window_size):
if i == label_word: if i == label_word:
continue continue
...@@ -746,14 +746,14 @@ def nce(input, ...@@ -746,14 +746,14 @@ def nce(input,
num_total_classes=dict_size, param_attr='nce.w_0', num_total_classes=dict_size, param_attr='nce.w_0',
bias_attr='nce.b_0') bias_attr='nce.b_0')
#or use custom distribution #or use custom distribution
dist = np.array([0.05,0.5,0.1,0.3,0.05]) dist = np.array([0.05,0.5,0.1,0.3,0.05])
loss = fluid.layers.nce(input=embs, label=words[label_word], loss = fluid.layers.nce(input=embs, label=words[label_word],
num_total_classes=5, param_attr='nce.w_1', num_total_classes=5, param_attr='nce.w_1',
bias_attr='nce.b_1', bias_attr='nce.b_1',
num_neg_samples=3, num_neg_samples=3,
sampler="custom_dist", sampler="custom_dist",
custom_dist=dist) custom_dist=dist)
""" """
helper = LayerHelper('nce', **locals()) helper = LayerHelper('nce', **locals())
check_variable_and_dtype(input, 'input', ['float32', 'float64'], 'nce') check_variable_and_dtype(input, 'input', ['float32', 'float64'], 'nce')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册