我尝试在tensorflow中定义一个二维占位符,但是,我事先并不知道它的大小。因此我定义了另一个占位符,但它似乎根本不起作用。这是最小的例子:
<script type="text/javascript">
$(document).ready(function(){
$("#barScan").click(function(){
cordova.plugins.barcodeScanner.scan(
function (result) {
alert("We got a barcode\n" +
"Result: " + result.text + "\n" +
"Format: " + result.format + "\n" +
"Cancelled: " + result.cancelled);
},
function (error) {
alert("Scanning failed: " + error);
}
);
});
});
</script>
错误讯息:
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]))
答案 0 :(得分:7)
如果您事先不知道某个维度的长度,请使用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)
按预期