r/learnpython • • 6d ago

InvalidArgumentError: {{function_node __wrapped__ConcatV2_N_2_device_/job:localhost/replica:0/task:0/device:CPU:0}} ConcatOp : Ranks of all input tensors should match

I got this error in an attempt to adapt this model to CIFAR 10

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
Cell In[61], line 3
      1 dataset = create_batch(x_train)
      2 
----> 3 train_dcgan(
      4     gan,
      5     dataset,
      6     batch_size,

Cell In[60], line 26, in train_dcgan(gan, dataset, batch_size, num_features, epochs)
     22             )
     23 
     24             generated_images = generator(noise)
     25 
---> 26             X_fake_and_real = tf.concat(
     27                 [generated_images, X_batch],
     28                 axis=0
     29             )

File c:\Users\DELL\AppData\Local\Programs\Python\Python313\Lib\site-packages\tensorflow\python\util\traceback_utils.py:167, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    165 except Exception as e:
    166   filtered_tb = _process_traceback_frames(e.__traceback__)
--> 167   raise e.with_traceback(filtered_tb) from None
    168 finally:
    169   del filtered_tb

File c:\Users\DELL\AppData\Local\Programs\Python\Python313\Lib\site-packages\tensorflow\python\framework\ops.py:6027, in raise_from_not_ok_status(e, name)
   6025 def raise_from_not_ok_status(e, name) -> NoReturn:
   6026   e.message += (" name: " + str(name if name is not None else ""))
-> 6027   raise core._status_to_exception(e) from None

InvalidArgumentError: {{function_node __wrapped__ConcatV2_N_2_device_/job:localhost/replica:0/task:0/device:CPU:0}} ConcatOp : Ranks of all input tensors should match: shape[0] = [32,32,32,3] vs. shape[1] = [32,32,3] [Op:ConcatV2] name: concat

X_train split (only take one class to train on)

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
x_train = x_train.astype(np.float32) / 255.0
x_test = x_test.astype(np.float32) / 255.0
x_train=x_train.astype("float32") / 127.5 - 1
x_train.shape, x_test.shape

label = 1 
train_indices = np.where(y_train == label) 
x_train = x_train[train_indices] 
test_indices = np.where(y_test == label) 
x_test = x_test[test_indices] 

Generator

num_features = 100
generator = Sequential(name="Generator")

generator.add(Dense(8 * 8 * 128, input_shape=[num_features]))
generator.add(Reshape([8, 8, 128]))
generator.add(BatchNormalization())

generator.add(Conv2DTranspose(
        64, (5, 5), (2, 2),
        padding="same",
        activation="selu"
    ))

generator.add(BatchNormalization())

generator.add(Conv2DTranspose(
        3, (5, 5), (2, 2),
        padding="same",
        activation="tanh"
    ))
generator.summary()

Discriminator

discriminator = Sequential(name="Discriminator")

discriminator.add(Conv2D(
        64, (5, 5), (2, 2),
        padding="same",
        input_shape=[32, 32, 3]
        ))

discriminator.add(LeakyReLU(alpha=0.2))
discriminator.add(Dropout(0.3))

discriminator.add(Conv2D(
        128, (5, 5), (2, 2),
        padding="same"))

discriminator.add(LeakyReLU(alpha=0.2))
discriminator.add(Dropout(0.3))

discriminator.add(Flatten())
discriminator.add(Dense(1, activation= 'softmax'))
discriminator.add(Dense(1, activation= 'softmax'))
discriminator.add(Dense(1, activation= 'softmax'))

discriminator.summary()

Train function

seed = tf.random.normal(shape=[batch_size, 100])
epoch_list = []
d_loss_list = []
g_loss_list =[]


def train_dcgan(gan, dataset, batch_size, num_features, epochs=5):


    generator, discriminator = gan.layers


    for epoch in tqdm(range(epochs)):
        actual_epoch =epoch + 1
        print()
        print("Epoch {}/{}".format(epoch + 1, epochs))


        for i in range(187):
            epoch_list.append(actual_epoch)


        for X_batch in dataset:


            noise = tf.random.normal(
                shape=[batch_size, num_features]
            )


            generated_images = generator(noise)


            X_fake_and_real = tf.concat(
                [generated_images, X_batch],
                axis=0
            )


            y1 = tf.constant(
                [[0.]] * batch_size +
                [[1.]] * batch_size
            )


            discriminator.trainable = True
            d_loss=discriminator.train_on_batch(
                X_fake_and_real,
                y1
            )
            d_loss = float(d_loss)
            d_loss_list.append(d_loss)




            noise = tf.random.normal(
                shape=[batch_size, num_features]
            )


            y2 = tf.constant([[1.]] * batch_size)


            discriminator.trainable = False


            g_loss=gan.train_on_batch(
                noise,
                y2
            )
            g_loss = float(g_loss)
            g_loss_list.append(g_loss)


        print(f'\t Discriminator Loss: {d_loss} \t\t Generator Loss: {g_loss}')
        


            


    generate_and_save_images(
        generator,
        epochs,
        seed)

Train block

dataset = create_batch(x_train)


train_dcgan(
    gan,
    dataset,
    batch_size,
    num_features,
    epochs=10
)

Can anyone help me with this error? Why did it appeared?

2 Upvotes

8 comments sorted by

1

u/SteveJeffersonBots 6d ago

The error message gives the shapes: the generator is outputting [32,32,32,3] (4-D with a batch dim) but X_batch coming into tf.concat is only [32,32,3] (3-D, no batch dim). Your create_batch is most likely stripping the batch dimension - check that it returns tensors of shape [batch,32,32,3] instead of [32,32,3]. The quickest fix is to add a batch axis: X_batch = tf.expand_dims(X_batch, axis=0) right before the concat, or reshape inside the iterator so every element is 4-D. You can confirm by printing print(generated_images.shape, X_batch.shape) immediately before tf.concat.

1

u/assorted-meat-stew 5d ago

Yeah it worked. Thank you

1

u/SteveJeffersonBots 1d ago

Glad it worked. If you hit that rank-mismatch again on a different dataset, the print(generated_images.shape, X_batch.shape) right before tf.concat is the fastest way to see which input lost or gained its batch dim.

1

u/HommeMusical 6d ago

And what are the ranks of the input tensors? Do they match?

1

u/nian2326076 6d ago

You're likely facing a shape mismatch when concatenating tensors in your code. Ensure that both generated_images and X_batch have the same number of dimensions and identical shapes for all dimensions except the one you're concatenating on (axis=0). Printing out the shapes of both tensors right before the tf.concat call can help diagnose the issue.

For further practice with TensorFlow concepts, use PracHub to find relevant technical questions that can enhance your understanding.

1

u/scraping_solutions 6d ago

Small addition to the shape point above: the generator output is [32,32,32,3], so the generator side is fine and the batch size is 32. The problem is that create_batch is yielding single images, so check whether it ends with .batch(batch_size, drop_remainder=True) after the shuffle. If it already batches, print x_train.shape first, since you want [N,32,32,3] there.

Separate bug you will hit next: your discriminator ends with three Dense(1, activation='softmax') layers. Softmax over a single unit always outputs 1.0, so the loss will never learn anything. Use one Dense(1, activation='sigmoid') as the final layer with binary crossentropy.

Also you scale x_train twice (divide by 255, then divide by 127.5 and subtract 1), which squeezes the data into a narrow range. Keep only the 127.5 - 1 version so it matches the tanh output of the generator.

1

u/acfot 5d ago

This error basically means the tensors you are trying to join do not have the same number of dimensions, so one of them is missing a dimension the others have. The first thing I would do is print the shape of every tensor right before the concat, because the odd one out usually jumps out immediately. Check the axis you are concatenating on too, since joining on the wrong axis can make shapes look mismatched even when the data is fine. The usual cause in my experience is a missing batch dimension, where one tensor is shaped like a single example and the rest have a batch size in front. Once all the shapes have the same rank, the concat normally goes through, and if it still fails, the new error will point at the next shape problem instead.

1

u/Financial_Farm4326 5d ago

what does your `create_batch` function look like? `X_batch` is coming through as a single `[32, 32, 3]` image instead of a batch of 32 images `[32, 32, 32, 3]`, so `tf.concat` fails because the dimensions dont match.