ValueError:Layer Discriminator 需要 1 个输入,但它接收到 2 个输入张量

Posted

技术标签:

【中文标题】ValueError:Layer Discriminator 需要 1 个输入,但它接收到 2 个输入张量【英文标题】:ValueError: Layer Discriminator expects 1 input(s), but it received 2 input tensors 【发布时间】:2021-12-20 19:58:19 【问题描述】:

我正在尝试使用 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

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

【问题讨论】:

【参考方案1】:

看起来问题是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 函数。

【讨论】:

【参考方案2】:

我可以想象问题直接来自您的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))

【讨论】:

哦,太晚了! ;) 这确实有帮助,我将它切换到上面的代码并得到了更好的结果。 我还有一个与此代码相关的问题,敬请期待:) ***.com/questions/69876271/… 去看看

以上是关于ValueError:Layer Discriminator 需要 1 个输入,但它接收到 2 个输入张量的主要内容,如果未能解决你的问题,请参考以下文章

ValueError:使用自定义回调绘制卷积层特征图时,函数模型的输出张量必须是 TensorFlow `Layer` 的输出

ValueError:Layer Discriminator 需要 1 个输入,但它接收到 2 个输入张量

ValueError:检查输入时出错:预期 keras_layer_input 有 4 个维度,但得到了形状为 (10, 1) 的数组

Keras LSTM ValueError: Input 0 of layer "sequential" is in compatible with the layer: expe

ValueError:Input 0 is in compatible with layer lstm_1: expected ndim=3,found ndim=4

ValueError: Input 0 is in compatible with layer model: expected shape=(None, 14999, 7), found shape=