[英]Trying to create GAN: InvalidArgumentError: Matrix size-incompatible
I am quite new to the field but I am trying to create a Generative Adversarial Network to generate Music. 我是该领域的新手,但我正在尝试创建一个生成对抗网络以生成音乐。 I have a model that is a combination of the Generator and Discriminator but when I train it, it gives me an error.
我有一个结合了生成器和鉴别器的模型,但是当我训练它时,它给了我一个错误。 Something about the output it doesn't like.
关于输出的一些不喜欢的东西。 I am using Keras sequential.
我正在使用Keras顺序。 Any help would be much appreciated.
任何帮助将非常感激。
As I understand it the input and output should have the same dimensions in any Keras model. 据我了解,在任何Keras模型中,输入和输出应具有相同的尺寸。 My input shape - (300, 30, 1).
我的输入形状-(300,30,1)。 Output shape - (300, 1).
输出形状-(300,1)。 And when I train them separately they don't cause an error.
当我分别训练它们时,它们不会引起错误。 But when I combine them in a separate model they start to give an error - particularly in the discriminator's last line ->
Dense(1, activation='sigmoid')
但是,当我将它们组合到单独的模型中时,它们开始出现错误-特别是在鉴别器的最后一行中->
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
So the error itself is 所以错误本身是
InvalidArgumentError: Matrix size-incompatible: In[0]: [300,1], In[1]: [30,30] [[{{node sequential_22/dense_37/MatMul}}]]
Whenever I remove the Dense(1, sigmoid) layer of the discriminator it works but I need that layer for binary classification. 每当我删除鉴别器的Dense(1,Sigmoid)层时,它都起作用,但是我需要该层进行二进制分类。 Maybe I need to rebuild the model or just do a small fix but anyway all suggestions are appreciated.
也许我需要重建模型或只是做一个小的修复,但是无论如何所有建议都值得赞赏。
Welcome to stackoverflow. 欢迎使用stackoverflow。 This error occurs because,
your model needs (30,30) but you are feeding it (300,1)
. 发生此错误的原因是,
your model needs (30,30) but you are feeding it (300,1)
。
There are a few changes that would be better here: 这里有一些更改可能会更好:
model.add(Flatten(input_shape=((30, 1))))
is at wrong place. model.add(Flatten(input_shape=((30, 1))))
在错误的地方。 It should be below before dense
layer. dense
层之前。 Or as you are building a RNN, I would argue you don't need a Flatten
layer. Flatten
层。 bidirectional LSTMs
. bidirectional LSTMs
。 batchnormalization
. batchnormalization
。 There are a few other changes needed for the whole network, you can see this great article for music generation . 整个网络还需要进行其他一些更改,您可以查看这篇伟大的音乐生成文章 。 Hope this helps!!
希望这可以帮助!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.