Skip to content

keras.ops.repeat cannot return an exptected shape when x is a KerasTensor and the axis is None #19821

@Zhaopudark

Description

@Zhaopudark

Hello. Thank you for your contributions and maintenance for the best Keras.

I'm following the instructions of Conditional GAN (code samples, uses Keras 3), and focusing on the keras.ops.repeat function that is used in it.

I have found, maybe, if the input tensor of keras.ops.repeat is a symbolic tensor, i.e., the keras.KerasTensor, and the arg axis is None, the returned one will not be my expected one.

As the following:

batch_size = 64
class_num = 10
a = keras.KerasTensor(shape=(batch_size, class_num), dtype=tf.float32)
a = a[:, :, None, None] # [B,10,1,1]
b = keras.ops.repeat(a, repeats=[28 * 28])
print(b.shape)# (784,)
# expected output: (501760,)

If assign axis, it works as expected:

a = keras.KerasTensor(shape=(batch_size, class_num), dtype=tf.float32)
a = a[:, :, None, None] # [B,10,1,1]
b = keras.ops.repeat(a, repeats=[28 * 28],axis=0)
print(b.shape)# (784, 10, 1, 1)
# expected output: (784, 10, 1, 1)

If not use the symbolic tensor, it also works as expected:

a = keras.random.normal(shape=(batch_size, class_num), dtype=tf.float32)
a = a[:, :, None, None] # [B,10,1,1]
b = keras.ops.repeat(a, repeats=[28 * 28])
print(b.shape)# (501760,)
# expected output: (501760,)

So, is the above a bug?

And my environment is:

  • Keras: Version: 3.3.3
  • Numpy: Version: 1.26.4
  • TensorFlow: Version: 2.16.1

Thanks in advance.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions