2017-07-24 29 views
1

使用Word2vec與Tensorflow在this tutorial file由Tensorflow以下行被找到(線45)加載word2vec「擴展」:在Windows

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

我使用Windows 10,並且如也指出在this SO question.so -files適用於Linux。

什麼是在Windows上加載的等效擴展?

另外,我不明白爲什麼在安裝時包含在Tensorflow中,但Word2Vec必須在本地構建。在文檔Installing TensorFlow on Windows中,沒有提到必須構建這些擴展。

這是一箇舊的做法,現在已經改變,一切都隨安裝一起發運?如果是這樣,那麼這個變化如何適用於示例中的word2vec模塊?

回答

2

是的,它已經改變! 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回購更好地維護。

+1

很好的回覆,謝謝你幫我清理一下。 –