Reputation: 2478
I have an existing complex model. Inside there is tensor x
with shape (None, 128, 128, 3). First axis has dynamic shape, that should be materialized when batch is passed to feed_dict
in session.run
. However when I attempt to define broadcast operation to shape of x:
y = tf.broadcast_to(z, (x.shape[0], x.shape[1], x.shape[2], 1))
Exception is raised:
Failed to convert object of type <class 'tuple'> to
Tensor. Contents: (Dimension(None), Dimension(128),
Dimension(128), 1). Consider casting elements to a supported type.
Exception occurs when creating model, not when running it. Converting first element to number helps, but this is not the solution.
Upvotes: 1
Views: 215
Reputation: 59731
The .shape
attribute gives you the shape known at graph construction time, which is a tf.TensorShape
structure. If the shape of x
were fully known, you could get your code to work as follows:
y = tf.broadcast_to(z, (x.shape[0].value, x.shape[1].value, x.shape[2].value, 1))
However, in your case x
has an unknown first dimension. In order to use the actual tensor shape as a regular tf.Tensor
(with value only known at runtime), you can use tf.shape
:
x_shape = tf.shape(x)
y = tf.broadcast_to(z, (x_shape[0], x_shape[1], x_shape[2], 1))
Upvotes: 2