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

我正在尝试使用 MNIST 数据集训练 GAN 模型。我想我已经完成了大部分工作,但我收到了这个错误:

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>]

这来自我调用时的训练函数:

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

在这里你可以看到我的完整训练功能:

    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)

和我的初始模型声明:

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

如果有帮助,我可以post完整的代码,但我认为这是某个地方的一个非常小的错误。我在网上四处看看,有时这似乎与使用 [] 而不是 () 有关,但在我的代码中似乎并非如此(至少从我所见)。

看起来问题是 Shuffle 返回了两个列表而不是一个串联的列表,所以我将语法切换为:

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)

注意,这是使用 Sklearn shuffle 函数。

我可以想象问题直接来自您的 shuffle 函数:

尝试连接您的数据对,然后使用 tf.random.shuffle(tensor) 如:

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))