我是这个领域的新手,但我正在尝试创建一个生成对抗网络来生成音乐。我有一个模型,它是生成器和鉴别器的组合,但当我训练它时,它给了我一个错误。一些它不喜欢的输出。我使用的是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}}]]
每当我移除鉴别器的致密(1,sigmoid)层时,它就会起作用,但我需要这一层来进行二进制分类。也许我需要重建模型,或者只是做一个小的修复,但不管怎样,所有的建议都是值得感谢的。
发布于 2019-06-16 01:22:26
欢迎来到stackoverflow。出现此错误是因为,your model needs (30,30) but you are feeding it (300,1)
。
这里有几个更好的改变:
鉴别器model.add(Flatten(input_shape=((30, 1))))
中的
dense
层之前。或者,当您构建bidirectional LSTMs
.Flatten
层。整个网络还需要做一些其他更改,您可以看到这个great article for music generation。希望这能有所帮助!!
https://stackoverflow.com/questions/56614214
复制相似问题