2017-01-13 7 views
2

Ich versuche, einen zweidimensionalen Platzhalter im Tensorflow zu definieren, aber ich weiß nicht, die Größe davon im Voraus. Daher definiere ich einen anderen Platzhalter, aber es scheint, dass es überhaupt nicht funktioniert. Hier ist das Minimum Beispiel:Verwenden von Platzhalter als Form in Tensorflow

import tensorflow as tf 

batchSize = tf.placeholder(tf.int32) 
input = tf.placeholder(tf.int32, [batchSize, 5]) 

Fehlermeldung:

Traceback (most recent call last): 
    File "C:/Users/v-zhaom/OneDrive/testconv/test_placeholder.py", line 5, in <module> 
    input = tf.placeholder(tf.int32, [batchSize, 5]) 
    File "C:\Python35\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1579, in placeholder 
    shape = tensor_shape.as_shape(shape) 
    File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 821, in as_shape 
    return TensorShape(shape) 
    File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 457, in __init__ 
    self._dims = [as_dimension(d) for d in dims_iter] 
    File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 457, in <listcomp> 
    self._dims = [as_dimension(d) for d in dims_iter] 
    File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 378, in as_dimension 
    return Dimension(value) 
    File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 33, in __init__ 
    self._value = int(value) 
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor' 

Dann habe ich versucht, die Form zu packen, so habe ich dies:

input = tf.placeholder(tf.int32, tf.pack([batchSize, 5])) 

funktioniert auch nicht:

Traceback (most recent call last): 
    File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 451, in __init__ 
    dims_iter = iter(dims) 
    File "C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 510, in __iter__ 
    raise TypeError("'Tensor' object is not iterable.") 
TypeError: 'Tensor' object is not iterable. 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:/Users/v-zhaom/OneDrive/testconv/test_placeholder.py", line 5, in <module> 
    input = tf.placeholder(tf.int32, tf.pack([batchSize, 5])) 
    File "C:\Python35\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1579, in placeholder 
    shape = tensor_shape.as_shape(shape) 
    File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 821, in as_shape 
    return TensorShape(shape) 
    File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 454, in __init__ 
    self._dims = [as_dimension(dims)] 
    File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 378, in as_dimension 
    return Dimension(value) 
    File "C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_shape.py", line 33, in __init__ 
    self._value = int(value) 
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor' 

Antwort

2

Verwenden Sie None wenn nicht kennen die Länge in einer Dimension im Voraus, z.B.

input = tf.placeholder(tf.int32, [None, 5]) 

Wenn Sie diesen Platzhalter füttern eine richtige Anordnung von Form (batch_size, 5), dann ist es dynamische Form korrekt eingestellt werden, das heißt

sess.run(tf.shape(input), feed_dict={input: np.zeros(dtype=np.int32, shape=(10, 5))}) 

wird

Rückkehr
array([10, 5], dtype=int32) 

wie erwartet

Verwandte Themen