1

我已经检查了如何在 TensorFlow 中打印张量对象的值?. 然而,skflow 似乎并没有开箱即用。例如尝试这样:

with tf.Session():
    word_vectors = skflow.ops.categorical_variable(X_test[0], n_classes=n_words,embedding_size=EMBEDDING_SIZE, name='words')
    word_vectors.eval()

也试过

sess = tf.InteractiveSession()

在调用 word_vectors.eval() 之前。但这一切都会导致崩溃:

    Traceback (most recent call last):
      File "/Users/mypc/Documents/scripts/scikitflow/small_rnn_test/test_load.py", line 32, in <module>
    word_vectors.eval()
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/ops.py", line 405, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/ops.py", line 2728, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "/Library/Python/2.7/site-packages/tensorflow/python/client/session.py", line 345, in run
    results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
  File "/Library/Python/2.7/site-packages/tensorflow/python/client/session.py", line 419, in _do_run
    e.code)
tensorflow.python.framework.errors.FailedPreconditionError: Attempting to use uninitialized value words/words_embeddings
     [[Node: words/embedding_lookup/embedding_lookup = Gather[Tindices=DT_INT64, Tparams=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](words/words_embeddings, words/embedding_lookup/Reshape)]]
Caused by op u'words/embedding_lookup/embedding_lookup', defined at:
  File "/Users/mypc/Documents/scripts/scikitflow/small_rnn_test/test_load.py", line 31, in <module>
    word_vectors = skflow.ops.categorical_variable(X_test[0], n_classes=n_words,embedding_size=EMBEDDING_SIZE, name='words')
  File "/Library/Python/2.7/site-packages/skflow/ops/embeddings_ops.py", line 77, in categorical_variable
    return embedding_lookup(embeddings, tensor_in)
  File "/Library/Python/2.7/site-packages/skflow/ops/embeddings_ops.py", line 50, in embedding_lookup
    embeds_flat = tf.nn.embedding_lookup(params, ids_flat, name)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/embedding_ops.py", line 46, in embedding_lookup
    return array_ops.gather(params[0], ids, name=name)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 302, in gather
    name=name)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 633, in apply_op
    op_def=op_def)
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/ops.py", line 1710, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/ops.py", line 988, in __init__
    self._traceback = _extract_stack()

有人知道查看 skflow 创建的张量内容的相对简单的方法吗?

4

2 回答 2

0

目前尚不清楚您抛出了什么错误,但我收到错误“尝试使用未初始化的值单词/words_embeddings”。初始化变量修复了错误。

所以工作代码是:

word_vectors = skflow.ops.categorical_variable([1,2], n_classes=3,embedding_size=2, name='words')
with tf.Session() as sess:
  tf.initialize_all_variables().run()
  word_vectors.eval()

我还将图形节点移到了word_vectors会话上下文之外,尽管这不是修复错误所必需的。

于 2016-02-14T04:15:23.260 回答
0

categorical_variable不会为您初始化所有变量。它不像初始化会话、变量、图形等的估计器。此时,如果您使用tf.initialize_all_variables().run(),您将能够获得这些值。另外,请注意,您可以通过估计器的bias_weights_属性访问权重和偏差。

于 2016-02-17T02:38:16.977 回答