【问题标题】:why tf.random.log_uniform_candidate_sampler gives true class?为什么 tf.random.log_uniform_candidate_sampler 提供真正的课程?
【发布时间】:2021-02-16 02:54:15
【问题描述】:

我在看tensorflow的word2vec教程:https://www.tensorflow.org/tutorials/text/word2vec#define_loss_function_and_compile_model

在本教程中,负抽样是使用tf.random.log_uniform_candidate_sampler 进行的。给定上下文类(真实类),目标是从整个词汇表中抽取负类。据我了解,否定类必须与给定的上下文类不同。但是我发现上下文类可能出现在tf.random.log_uniform_candidate_sampler采样的负类中。代码如下:

import tensorflow as tf
SEED = 42 

# encode the words
sentence = "The wide road shimmered in the hot sun"
tokens = list(sentence.lower().split())
vocab, index = {}, 1 # start indexing from 1
vocab['<pad>'] = 0 # add a padding token 
for token in tokens:
  if token not in vocab: 
    vocab[token] = index
    index += 1
vocab_size = len(vocab)
print(vocab)
inverse_vocab = {index: token for token, index in vocab.items()}
print(inverse_vocab)


# make (hot, the) as a context pair
target_word, context_word = 6, 1
print("target: {}, context: {}".format(inverse_vocab[target_word], inverse_vocab[context_word]))


# negative sampling
# Set the number of negative samples per positive context. 
num_ns = 4

context_class = tf.reshape(tf.constant(context_word, dtype="int64"), (1, 1))
negative_sampling_candidates, _, _ = tf.random.log_uniform_candidate_sampler(
    true_classes=context_class, # class that should be sampled as 'positive'
    num_true=1, # each positive skip-gram has 1 positive context class
    num_sampled=num_ns, # number of negative context words to sample
    unique=True, # all the negative samples should be unique
    range_max=vocab_size, # pick index of the samples from [0, vocab_size]
    seed=SEED, # seed for reproducibility
    name="negative_sampling" # name of this operation
)
print("negative samples\' index", negative_sampling_candidates)
print("negetive samples: ", [inverse_vocab[index.numpy()] for index in negative_sampling_candidates])
# "the" will show in negative samples, if not, run it several times.

单词the是单词hot的上下文类,为什么它会出现在采样的负类中?此外,目标词hot 也可以被采样为负类。我是不是误会了什么?

【问题讨论】:

    标签: python tensorflow nlp word2vec sampling


    【解决方案1】:

    你是对的。 TensorFlow 犯了一个错误。请参阅https://github.com/tensorflow/tensorflow/issues/49490 上的错误报告

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-07
      • 2021-03-30
      • 1970-01-01
      • 2011-01-16
      • 2023-03-19
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多