简体   繁体   English

ValueError:层鉴别器需要 1 个输入,但它收到 2 个输入张量

[英]ValueError: Layer Discriminator expects 1 input(s), but it received 2 input tensors

I am trying to train a GAN model with the MNIST dataset.我正在尝试使用 MNIST 数据集训练 GAN 模型。 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).我在网上环顾四周,似乎有时这与使用[]而不是()但在我的代码中似乎并非如此(至少从我所见)。

It looks like the issue was that Shuffle was returning two lists rather than a concatenated one so I switched the syntax to:看起来问题是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)

Note, this is using the Sklearn shuffle function.请注意,这是使用 Sklearn shuffle函数。

I can imagine that the problem is coming directly from your shuffle function:我可以想象问题直接来自您的shuffle函数:

Try concatenating your pairs of data and then using tf.random.shuffle(tensor) like:尝试连接你的数据对,然后使用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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:层顺序需要 1 个输入,但它收到 239 个输入张量 - ValueError: Layer sequential expects 1 input(s), but it received 239 input tensors ValueError:model 层需要 21 个输入,但它接收到 1 个输入张量 - ValueError: Layer model expects 21 input(s), but it received 1 input tensors ValueError:层顺序_16 需要 1 个输入,但它收到 8 个输入张量 - ValueError: Layer sequential_16 expects 1 input(s), but it received 8 input tensors 尝试在 Tensorflow 中组合数字和文本特征:ValueError:层模型需要 2 个输入,但它接收到 1 个输入张量 - Attempting to Combine Numeric and Text Features in Tensorflow: ValueError: Layer model expects 2 input(s), but it received 1 input tensors ValueError:Layer 需要 2 个输入,但在训练 CNN 时收到 1 个输入张量 - ValueError: Layer expects 2 input(s), but it received 1 input tensors when training a CNN ValueError:层顺序需要 1 个输入,但它接收到 250 个输入张量 - ValueError: Layer sequential expects 1 inputs, but it received 250 input tensors ValueError:层 sequential_20 需要 1 个输入,但它收到了 2 个输入张量 - ValueError: Layer sequential_20 expects 1 inputs, but it received 2 input tensors 我的输入层需要是什么形状? 我不断收到 ValueError:层“模型”需要 1 个输入,但它收到了 2 个输入张量 - What shape does my Input layer need to be? I keep getting ValueError: Layer "model" expects 1 input(s), but it received 2 input tensors Tensorflow &amp; Keras 层“顺序”需要 1 个输入,但它接收到 2 个输入张量 - Tensorflow & Keras Layer "sequential" expects 1 input(s), but it received 2 input tensors 口罩识别。 错误 - “ValueError:层“model_1”需要 1 个输入,但它接收到 3 个输入张量”,因为帧中出现了多个面 - Mask Recognition. Error - "ValueError: Layer "model_1" expects 1 input(s), but it received 3 input tensors" as more than one face appears in frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM