我是该领域的新手,但我正在尝试创建一个生成对抗网络以生成音乐。我有一个模型,它是生成器和鉴别器的组合,但是当我训练它时,它给我一个错误。关于输出的一些不喜欢的东西。我正在使用Keras顺序。任何帮助将非常感激。

据我了解,在任何Keras模型中,输入和输出应具有相同的尺寸。我的输入形状-(300,30,1)。输出形状-(300,1)。当我分别训练它们时,它们不会引起错误。但是,当我将它们组合到一个单独的模型中时,它们开始出现错误-特别是在鉴别符的最后一行-> Dense(1, activation='sigmoid')

    def __generator(self):
        """ Declare generator """
        model = Sequential()
        model.add(LSTM(256, input_shape=(self.n_prev, 1), return_sequences=True))
        model.add(Dropout(0.6))
        model.add(LSTM(128, input_shape=(self.n_prev, 1), return_sequences=True))
        model.add(Dropout(0.6))
        model.add(LSTM(64, input_shape=(self.n_prev, 1), return_sequences=False))
        model.add(Dropout(0.6))
        model.add(Dense(1))

        print(model.summary())
        return model


    def __discriminator1b (self, width=300, height=30, channels=1):

        shape = (width, height, channels)
        model = Sequential()
        model.add(Flatten(input_shape=((30, 1))))
        model.add(Dense((height * channels), input_shape=(30, 1)))
        model.add(LeakyReLU(alpha=0.2))
        model.add(Dense(np.int64((height * channels)/2)))
        model.add(LeakyReLU(alpha=0.2))

        model.add(Dense(1, activation='sigmoid'))

        print(model.summary())
        return model


    def __gen_disc (self):
        model = Sequential()
        model.add(self.G)
        model.add(self.D)

        return model


Training:
self.G.train_on_batch(np.array(gen_noiseX), np.array(genY))
self.D.train_on_batch(np.array(gen_noiseX), disc_label)
self.GD.train_on_batch(np.array(gen_noiseX), y_mislabled)

Model Summaries:
Generator:
Layer (type)                 Output Shape              Param #
=================================================================
lstm_28 (LSTM)               (None, 30, 256)           264192
_________________________________________________________________
dropout_52 (Dropout)         (None, 30, 256)           0
_________________________________________________________________
lstm_29 (LSTM)               (None, 30, 128)           197120
_________________________________________________________________
dropout_53 (Dropout)         (None, 30, 128)           0
_________________________________________________________________
lstm_30 (LSTM)               (None, 64)                49408
_________________________________________________________________
dropout_54 (Dropout)         (None, 64)                0
_________________________________________________________________
dense_36 (Dense)             (None, 1)                 65
=================================================================
Total params: 510,785
Trainable params: 510,785
Non-trainable params: 0
_________________________________________________________________
None


Discriminator:

Layer (type)                 Output Shape              Param #
=================================================================
flatten_8 (Flatten)          (None, 30)                0
_________________________________________________________________
dense_37 (Dense)             (None, 30)                930
_________________________________________________________________
leaky_re_lu_15 (LeakyReLU)   (None, 30)                0
_________________________________________________________________
dense_38 (Dense)             (None, 15)                465
_________________________________________________________________
leaky_re_lu_16 (LeakyReLU)   (None, 15)                0
_________________________________________________________________
dense_39 (Dense)             (None, 1)                 16
=================================================================
Total params: 1,411
Trainable params: 1,411
Non-trainable params: 0
_________________________________________________________________
None


所以错误本身是

InvalidArgumentError: Matrix size-incompatible: In[0]: [300,1], In[1]: [30,30]   [[{{node sequential_22/dense_37/MatMul}}]]


每当我删除鉴别器的Dense(1,Sigmoid)层时,它都起作用,但是我需要该层进行二进制分类。也许我需要重建模型或只是做一个小的修复,但是无论如何所有建议都值得赞赏。

最佳答案

欢迎使用stackoverflow。
发生此错误的原因是your model needs (30,30) but you are feeding it (300,1)
这里有一些更改可能会更好:


在鉴别器中
model.add(Flatten(input_shape=((30, 1))))在错误的位置。它应该在dense层之前。或者在构建RNN时,我认为您不需要Flatten层。
使用bidirectional LSTMs
在生成器中,使用batchnormalization


整个网络还需要进行其他一些更改,您可以看到此great article for music generation。希望这可以帮助!!

关于python - 尝试创建GAN:InvalidArgumentError:矩阵大小不兼容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56614214/

10-12 23:28