2017-01-13 9 views
2

テンソルフローで2次元のプレースホルダを定義しようとしていますが、そのサイズは事前にわかりません。したがって、別のプレースホルダを定義しますが、まったく動作しないようです。ここでは最小の例である:テンソルフローの形状としてプレースホルダを使用する

import tensorflow as tf 

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

エラーメッセージ:

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' 

それから私は、形状をパックしてみましたので、私はこれがあります。

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

は動作しません。次のいずれか

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' 

答えて

2

使用しない場合は、Noneを使用してください。ある次元の長さを事前に知っている。

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

あなたはこのプレースホルダに形状の適切な配列(BATCH_SIZE、5)、それはダイナミックな形状が正しく設定されますを養う、予想通りすなわち

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

array([10, 5], dtype=int32) 

を返します。

関連する問題