Reputation: 13
I'm trying to construct a simple one-hot converter. It takes a batch of data vectors as input, and for each data vector, converts it to a one-hot vector. The one-hots have 1s at the original data vectors' argmaxes. (e.g. [[2.3, -4.1, 0.4], [-0.1, -3.1, 2.1]] -> [[1.0, 0.0, 0.0], [0.0, 0.0, 1.0]])
I'm doing this with 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))
The first three printouts happen correctly. The last printout triggers this error:
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()
I don't understand why output_shape
should have 10 elements or why this error is happening... Please help!
Upvotes: 1
Views: 1285
Reputation: 126154
The issue seems to arise from the fact that your sparse_indices
matrix is a 2 x 10
matrix, whereas it expects a num_elems x num_dims
(i.e. 10 x 2
) matrix. You should change the code that computes this matrix as follows:
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])
You might also find the recently added tf.one_hot()
op useful.
Upvotes: 2