" output_shape的元素数量不正确"

时间:2016-04-14 10:39:04

标签: tensorflow

我试图构建一个简单的单热转换器。它需要一批数据向量作为输入,并且对于每个数据向量,将其转换为单热矢量。单热点在原始数据向量上有1秒。 argmaxes。 (例如[[2.3,-4.1,0.4],[ - 0.1,-3.1,2.1]] - > [[1.0,0.0,0.0],[0.0,0.0,1.0]])

我使用tf.sparse_to_dense()执行此操作。

import random
import tensorflow as tf

batch_size = 10
data_size = 3
data = []
for i in range(batch_size):
    data.append([])
    for j in range(data_size):
        data[i].append(random.random())
with tf.Graph().as_default(), tf.Session() as sess:
    indices = tf.reshape(tf.range(0, limit=batch_size, delta=1), [1, -1])
    hot_ids = tf.reshape(tf.cast(tf.argmax(data, 1), tf.int32), [1, -1])
    sparse_indices = tf.concat(0, [indices, hot_ids])
    output_shape = tf.pack([batch_size, data_size])
    result = tf.sparse_to_dense(sparse_indices, output_shape, 1.0, 0.0)
    tf.initialize_all_variables().run()
    print(data)
    print(sparse_indices.eval(session=sess))
    print(output_shape.eval(session=sess))
    print(result.eval(session=sess))

前三次打印输出正确。最后一次打印输出会触发此错误:

W tensorflow/core/common_runtime/executor.cc:1102] 0x7fb0e5903560 Compute status: Invalid argument: output_shape has incorrect number of elements: 2 should be: 10
     [[Node: SparseToDense = SparseToDense[T=DT_FLOAT, Tindices=DT_INT32, validate_indices=true, _device="/job:localhost/replica:0/task:0/cpu:0"](concat, pack, SparseToDense/sparse_values, SparseToDense/default_value)]]
Traceback (most recent call last):
  File "one-hot_simple", line 21, in <module>
    print(result.eval(session=sess))
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 465, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3097, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 315, in run
    return self._run(None, fetches, feed_dict)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 511, in _run
    feed_dict_string)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 564, in _do_run
    target_list)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 586, in _do_call
    e.code)
tensorflow.python.framework.errors.InvalidArgumentError: output_shape has incorrect number of elements: 2 should be: 10
     [[Node: SparseToDense = SparseToDense[T=DT_FLOAT, Tindices=DT_INT32, validate_indices=true, _device="/job:localhost/replica:0/task:0/cpu:0"](concat, pack, SparseToDense/sparse_values, SparseToDense/default_value)]]
Caused by op u'SparseToDense', defined at:
  File "one-hot_simple", line 16, in <module>
    result = tf.sparse_to_dense(sparse_indices, output_shape, 1.0, 0.0)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/sparse_ops.py", line 358, in sparse_to_dense
    name=name)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_sparse_ops.py", line 322, in _sparse_to_dense
    validate_indices=validate_indices, name=name)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 655, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2040, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1087, in __init__
    self._traceback = _extract_stack()

我不明白为什么output_shape应该有10个元素或为什么会发生这个错误...请帮助!

1 个答案:

答案 0 :(得分:1)

问题似乎是由于您的sparse_indices矩阵是2 x 10矩阵,而它需要num_elems x num_dims(即10 x 2)矩阵。您应该更改计算此矩阵的代码,如下所示:

indices = tf.reshape(tf.range(0, limit=batch_size, delta=1), [-1, 1])
hot_ids = tf.reshape(tf.cast(tf.argmax(data, 1), tf.int32), [-1, 1])
sparse_indices = tf.concat(1, [indices, hot_ids])

您可能还会发现最近添加的tf.one_hot()操作有用。