fluid 使用nce定制custom_dist 负采样概率报错
Created by: PengheLiu
paddle: 1.3.1 on mac cpu version 直接运行的这个例子。 http://paddlepaddle.org/documentation/docs/zh/1.3/api_cn/layers_cn.html#permalink-110-nce
import paddle as paddle
import paddle.fluid as fluid
from paddle.fluid import layers
import numpy as np
window_size = 5
words = []
for i in xrange(window_size):
words.append(layers.data(
name='word_{0}'.format(i), shape=[1], dtype='int64'))
dict_size = 10000
label_word = int(window_size / 2) + 1
embs = []
for i in xrange(window_size):
if i == label_word:
continue
emb = layers.embedding(input=words[i], size=[dict_size, 32],
param_attr='emb.w', is_sparse=True)
embs.append(emb)
embs = layers.concat(input=embs, axis=1)
#使用custom distribution
dist = fluid.layers.assign(input=np.array([0.05,0.5,0.1,0.3,0.05]).astype("float32"))
loss = layers.nce(input=embs, label=words[label_word],
num_total_classes=5, param_attr='nce.w',
bias_attr='nce.b',
num_neg_samples=3,
sampler="custom_dist",
custom_dist=dist)