[英]InvalidArgumentError: Matrix size-incompatible: In[0]: [32,21], In[1]: [128,1]
[英]Trying to create GAN: InvalidArgumentError: Matrix size-incompatible
我是该领域的新手,但我正在尝试创建一个生成对抗网络以生成音乐。 我有一个结合了生成器和鉴别器的模型,但是当我训练它时,它给了我一个错误。 关于输出的一些不喜欢的东西。 我正在使用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
。 整个网络还需要进行其他一些更改,您可以查看这篇伟大的音乐生成文章 。 希望这可以帮助!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.