Home > Net >  ValueError: Layer Discriminator expects 1 input(s), but it received 2 input tensors
ValueError: Layer Discriminator expects 1 input(s), but it received 2 input tensors

Time:11-08

I am trying to train a GAN model with the MNIST dataset. I think I have most of the pieces in place but I am getting this error:

ValueError: Layer Discriminator expects 1 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(64, 28, 28) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(64, 28, 28) dtype=float32>]

This comes from my train function when I call:

loss_dis = self.discriminator.train_on_batch(X_train_dis, y_train_dis)

Here you can see my full train function:

    def train(self, X_train, batch_size=128, epochs=2000, save_interval=200):
        half_batch = batch_size//2
        y_pos_train_dis = np.ones((half_batch, 1))
        y_neg_train_dis = np.zeros((half_batch, 1))
        y_train_GAN = np.ones((batch_size, 1))
        
        for epoch in range(epochs):
            # Generate training data for Discriminator

            #   random half_batch amount of real images
            X_pos_train_dis = X_train[np.random.randint(0, X_train.shape[0], half_batch)]
            
            #   random half_batch amount of generated fake images
            X_neg_train_dis = self.generator.predict(np.random.normal(0, 1, (half_batch, self.input_size[0])))

            #   Shuffle and append data using sklearn shuffle function
            X_train_dis, y_train_dis = shuffle(X_neg_train_dis, X_pos_train_dis), shuffle(y_neg_train_dis, y_pos_train_dis)
            
            # Generate training data for combined GAN model
            X_train_GAN = np.random.normal(0, 1, (batch_size, self.input_size[0]))
            
            # Train Discriminator
            loss_dis = self.discriminator.train_on_batch(X_train_dis, y_train_dis)
            
            # Train Generator
            loss_gen = self.GAN.train_on_batch(X_train_GAN, y_train_GAN)

and my initial model declaration:

def __init__(self, input_shape=(28,28,1), rand_vector_shape=(100,), lr=0.0002, beta=0.5):
        
        # Input sizes
        self.img_shape = input_shape
        self.input_size = rand_vector_shape
        
        # optimizer
        self.opt = tf.keras.optimizers.Adam(lr, beta)

        # Create Generator model
        self.generator = self.generator_model()
        self.generator.compile(loss='binary_crossentropy', optimizer = self.opt, metrics = ['accuracy'])
        
        # print(self.generator.summary())

        # Create Discriminator model
        self.discriminator = self.discriminator_model()
        self.discriminator.compile(loss='binary_crossentropy', optimizer = self.opt, metrics = ['accuracy'])
        
        # print(self.discriminator.summary())

        # Set the Discriminator as non trainable in the combined GAN model
        self.discriminator.trainable = False
        
        # Define model input and output
        input = tf.keras.Input(self.input_size)
        generated_img = self.generator(input)
        output = self.discriminator(generated_img)
        
        # Define and compile combined GAN model
        self.GAN = tf.keras.Model(input, output, name="GAN")
        self.GAN.compile(loss='binary_crossentropy', optimizer = self.opt, metrics=['accuracy'])

        return None
        
    def discriminator_model(self):
        """Create discriminator model."""
        model = tf.keras.models.Sequential(name='Discriminator')
        model.add(layers.Flatten())
        model.add(layers.Dense(units=512, kernel_initializer='normal', activation='relu'))
        model.add(layers.Dense(units=256, kernel_initializer='normal', activation='relu'))
        model.add(layers.Dense(units=1, kernel_initializer='normal', activation='sigmoid'))

        return model

    def generator_model(self):
        """Create generator model."""
        model = tf.keras.models.Sequential(name='Generator')
        model.add(layers.Dense(units=256, kernel_initializer='normal', activation='relu'))
        model.add(layers.Dense(units=512, kernel_initializer='normal', activation='relu'))
        model.add(layers.Dense(units=1024, kernel_initializer='normal', activation='relu'))
        model.add(layers.Dense(units=np.prod(self.img_shape), kernel_initializer='normal', activation='relu'))
        model.add(layers.Reshape((28,28)))
        
        return model

I can post the full code if that would be helpful but I imagine this is a very small mistake somewhere. I looked around online and it seems sometimes this is related to using [] instead of () but that does not seem to be the case in my code (at least from what I can see).

CodePudding user response:

I can imagine that the problem is coming directly from your shuffle function:

Try concatenating your pairs of data and then using tf.random.shuffle(tensor) like:

X_train_dis, y_train_dis = tf.random.shuffle(tf.concat([X_neg_train_dis, X_pos_train_dis], axis=0)), tf.random.shuffle(tf.concat([y_neg_train_dis, y_pos_train_dis], axis=0))

CodePudding user response:

It looks like the issue was that Shuffle was returning two lists rather than a concatenated one so I switched the syntax to:

X_train_dis, y_train_dis = tf.concat(shuffle(X_neg_train_dis, X_pos_train_dis, random_state=0), axis=0), tf.concat(shuffle(y_neg_train_dis, y_pos_train_dis, random_state=0), axis=0)

Note, this is using the Sklearn shuffle function.

  • Related