维数未知时如何创建一维范围张量?
How to create a 1-D range tensor when dimension is Unknown?
我有一个 n 维数组。我需要根据尺寸创建一维范围张量。
举个例子:
x = tf.placeholder(tf.float32, shape=[None,4])
r = tf.range(start=0, limit=, delta=x.shape[0],dtype=tf.int32, name='range')
sess = tf.Session()
result = sess.run(r, feed_dict={x: raw_lidar})
print(r)
问题是,x.shape[0]在构建计算图时是none。所以我无法使用范围构建张量。它给出了一个错误。
ValueError: Cannot convert an unknown Dimension to a Tensor: ?
对问题的任何建议或帮助。
提前致谢
x.shape[0]
当 运行 此代码是图形模式时,可能还不存在。如果你想要一个值,你需要使用tf.shape(x)[0]
.
documentation for tf.Tensor.get_shape
中有关该行为的更多信息。摘录(重点是我的):
tf.Tensor.get_shape() is equivalent to tf.Tensor.shape.
When executing in a tf.function or building a model using tf.keras.Input, Tensor.shape may return a partial shape (including None for unknown dimensions). See tf.TensorShape for more details.
>>> inputs = tf.keras.Input(shape = [10])
>>> # Unknown batch size
>>> print(inputs.shape)
(None, 10)
The shape is computed using shape inference functions that are registered for each tf.Operation.
The returned tf.TensorShape is determined at build time, without executing the underlying kernel. It is not a tf.Tensor. If you need a shape tensor, either convert the tf.TensorShape to a tf.constant, or use the tf.shape(tensor) function, which returns the tensor's shape at execution time.
我有一个 n 维数组。我需要根据尺寸创建一维范围张量。
举个例子:
x = tf.placeholder(tf.float32, shape=[None,4])
r = tf.range(start=0, limit=, delta=x.shape[0],dtype=tf.int32, name='range')
sess = tf.Session()
result = sess.run(r, feed_dict={x: raw_lidar})
print(r)
问题是,x.shape[0]在构建计算图时是none。所以我无法使用范围构建张量。它给出了一个错误。
ValueError: Cannot convert an unknown Dimension to a Tensor: ?
对问题的任何建议或帮助。
提前致谢
x.shape[0]
当 运行 此代码是图形模式时,可能还不存在。如果你想要一个值,你需要使用tf.shape(x)[0]
.
documentation for tf.Tensor.get_shape
中有关该行为的更多信息。摘录(重点是我的):
tf.Tensor.get_shape() is equivalent to tf.Tensor.shape.
When executing in a tf.function or building a model using tf.keras.Input, Tensor.shape may return a partial shape (including None for unknown dimensions). See tf.TensorShape for more details.
>>> inputs = tf.keras.Input(shape = [10]) >>> # Unknown batch size >>> print(inputs.shape) (None, 10)
The shape is computed using shape inference functions that are registered for each tf.Operation. The returned tf.TensorShape is determined at build time, without executing the underlying kernel. It is not a tf.Tensor. If you need a shape tensor, either convert the tf.TensorShape to a tf.constant, or use the tf.shape(tensor) function, which returns the tensor's shape at execution time.