2016-04-14 22 views
1

Ich versuche einen einfachen One-Hot-Konverter zu konstruieren. Es nimmt eine Charge von Datenvektoren als Eingabe und wandelt sie für jeden Datenvektor in einen Ein-Heiß-Vektor um. Die One-hots haben 1s bei den Argma- len der ursprünglichen Datenvektoren. (zB [[2.3, -4.1, 0.4], [-0.1, -3.1, 2.1]] -> [[1.0, 0.0, 0.0], [0.0, 0.0, 1.0]])"output_shape hat eine falsche Anzahl von Elementen"

Ich mache dies mit 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)) 

Die ersten drei Ausdrucke passieren richtig. Der letzte Ausdruck löst diesen Fehler:

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() 

Ich verstehe nicht, warum output_shape sollten 10 Elemente haben oder warum dieser Fehler passiert ... Bitte um Hilfe!

Antwort

1

Das Problem scheint aus der Tatsache entstehen, dass Ihre sparse_indices Matrix eine 2 x 10 Matrix ist, während es eine num_elems x num_dims (das heißt 10 x 2) Matrix erwartet. Sie sollten den Code ändern, die diese Matrix berechnet sich wie folgt:

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]) 

Sie auch die kürzlich tf.one_hot() op nützlich hinzugefügt finden könnte.

+0

Arbeitete, danke! – user6203369

Verwandte Themen