2016-04-14 12 views
1

Basit bir tek-sıcak dönüştürücü oluşturmaya çalışıyorum. Veri vektörlerinin bir girişini girdi olarak alır ve her bir veri vektörü onu tek-sıcak bir vektöre dönüştürür. Tek-hots orijinal veri vektörlerinin argmaxes 1s var. (örneğin [[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 öğesinin yanlış sayıda öğesi var"

yapıyorum Bu tf.sparse_to_dense() ile.

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

İlk üç çıktı doğru olarak gerçekleşir. output_shape 10 öğe olmalı veya bu hata neden oluyor ... Lütfen yardım neden

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

anlamıyorum: Geçen çıktı bu hatayı tetikler! Bir num_elems x num_dims (yani 10 x 2) matrisi beklediğini ise

cevap

1

sorunu, senin sparse_indices matris 2 x 10 matris olmasından kaynaklandığı görülmektedir. Ayrıca, son tf.one_hot() op yararlı katma bulabileceğiniz

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

: Bunu şöyle matrisi hesaplar kodunu değiştirmek gerekir.

+0

Çalıştı, teşekkürler! – user6203369

İlgili konular