在Tensorflow的本教程文件中,找到以下行(第45行)来加载word2vec“扩展名”:
word2vec = tf.load_op_library(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'word2vec_ops.so'))
我正在使用Windows 10,并且正如该SO问题所指出的,.so
-files适用于Linux。
在Windows上加载的等效扩展名是什么?
另外,我不明白为什么安装时Tensorflow中会包含很多其他内容,但Word2Vec必须在本地构建。在文档《在Windows上安装TensorFlow》中,没有提到必须构建这些扩展。
这是一种古老的做法,现在已经改变了,安装过程中附带了所有内容吗?如果是这样,该更改如何应用于word2vec
示例中的模块?
是的,它已经改变了!Tensorflow现在包括一个辅助函数,tf.nn.embedding_lookup
使嵌入数据变得非常容易。
你可以做这样的事情使用这个,即
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)
完整的代码在这里。
总的来说,我会过分依赖tensorflow/models
回购协议。部分内容已过时。主tensorflow/tensorflow
仓库可以更好地维护。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句