首页 > 解决方案 > TensorFlow:没有打印输出查找嵌入

问题描述

我一直在遵循https://ireneli.eu/2017/01/17/tensorflow-07-word-embeddings-2-loading-pre-trained-vectors/上的说明。

为什么我没有得到运行以下命令的输出(在最后一个包含 sess.run() 的单元格中):

from __future__ import absolute_import, division, print_function

import os
import matplotlib.pyplot as plt

import tensorflow as tf

[新单元格]

import numpy as np
filename = 'glove.6B.50d.txt'
def loadGloVe(filename):

[有关其余定义,请参见上面的链接]

[见上面的链接初始化嵌入]

[新单元格]

sent_toks = None
with open("documents.json") as f:
    training = js.load(f)
    all_parags = [compute this value]

sent_toks = nltk.sent_tokenize(all_parags)

[新单元格 - 见上面的链接]

import tensorflow.contrib.learn as le

# init vocab processor
[etc.]
x = np.array(list(vocab_processor.transform(sent_toks)))

[新单元格]

g = tf.Graph()

with g.as_default():
    W = tf.Variable(tf.constant(0.0, shape=[vocab_size, embedding_dim]),
            trainable=False, name="W")
    embedding_placeholder = tf.placeholder(tf.float32, [vocab_size, embedding_dim])
    embedding_init = W.assign(embedding_placeholder)
    foo = tf.nn.embedding_lookup(W, x[0])

    init_op = tf.initialize_all_variables()
    print_output = tf.Print(foo, [foo])

    with tf.Session(graph = g) as sess:
        sess.run(init_op)
        sess.run(print_output)

顺便一提,

print(x[0])

[    0  2827     5  6097    19     0     0    18    13  1427     1 59126
  4135   111     1  7770 43737     2   622     0     2     6 19517 31152
  1245 44144     5     6 20308    10  2891   509   707  6385     4     1
  6307  3649     2    41   970 19123  2656     0     0     0     0     0
     0     0]

标签: tensorflow

解决方案


注释中所述

注意:此操作打印到标准错误。它目前与 jupyter notebook 不兼容(打印到笔记本服务器的输出,而不是到笔记本中)。

你检查过笔记本服务器的输出(控制台)吗?


推荐阅读