【问题标题】:Using Word2vec with Tensorflow on Windows在 Windows 上使用 Word2vec 和 TensorFlow
【发布时间】:2017-07-24 12:51:02
【问题描述】:

在 Tensorflow 的 this tutorial file 中找到以下行(第 45 行)以加载 word2vec“扩展”:

word2vec = tf.load_op_library(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'word2vec_ops.so'))

我使用的是 Windows 10,正如this SO question 中指出的那样,.so-文件适用于 Linux。

在 Windows 上加载的等效扩展是什么?

另外,我不明白为什么安装时 Tensorflow 中还包含这么多其他内容,但 Word2Vec 必须在本地构建。在文档 Installing TensorFlow on Windows 中,没有提到必须构建这些扩展。

这是一种旧的做法,现在已经改变并且所有东西都随安装一起提供?如果是这样,该更改如何应用于示例中的 word2vec 模块?

【问题讨论】:

    标签: python windows tensorflow


    【解决方案1】:

    是的,它已经改变了! Tensorflow 现在包含一个辅助函数 tf.nn.embedding_lookup,它可以让嵌入数据变得非常容易。

    你可以通过像this这样的方式来使用它,即

    embeddings = tf.Variable(
        tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
    
    nce_weights = tf.Variable(
      tf.truncated_normal([vocabulary_size, embedding_size],
                          stddev=1.0 / math.sqrt(embedding_size)))
    nce_biases = tf.Variable(tf.zeros([vocabulary_size]))
    
    # Placeholders for inputs
    train_inputs = tf.placeholder(tf.int32, shape=[batch_size])
    train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])
    
    embed = tf.nn.embedding_lookup(embeddings, train_inputs)
    
    # Compute the NCE loss, using a sample of the negative labels each time.
    loss = tf.reduce_mean(
      tf.nn.nce_loss(weights=nce_weights,
                     biases=nce_biases,
                     labels=train_labels,
                     inputs=embed,
                     num_sampled=num_sampled,
                     num_classes=vocabulary_size))
    # We use the SGD optimizer.
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0).minimize(loss)
    
    for inputs, labels in generate_batch(...):
      feed_dict = {train_inputs: inputs, train_labels: labels}
      _, cur_loss = session.run([optimizer, loss], feed_dict=feed_dict)
    

    完整代码为here

    一般来说,我会犹豫过度依赖 tensorflow/models 回购。它的一部分已经过时了。主要的tensorflow/tensorflow repo 维护得更好。

    【讨论】:

      猜你喜欢
      • 2017-10-02
      • 2018-08-04
      • 2016-05-13
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 2017-10-18
      • 2016-06-12
      • 1970-01-01
      相关资源
      最近更新 更多