如何在使用skflow时查看张量内的值

时间:2016-02-10 03:12:28

标签: tensorflow skflow

我查了How to print the value of a Tensor object in 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创建的Tensors内容的相对简单的方法?

2 个答案:

答案 0 :(得分: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移到了会话上下文之外,虽然没有必要修复错误。

答案 1 :(得分:0)

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