无法使用Keras和两个相同的RNN模型为生成器和鉴别器创建GAN

时间:2019-09-30 10:59:15

标签: python keras deep-learning keras-layer gan

我有一个代码片段,其中包含两个相同的RNN模型,试图创建创生对抗网络。

错误:ValueError:带有2维的输入的归约维2无效。对于“ sequential_1 / simple_rnn_1 / Sum”(运算符:“ Sum”),其输入形状为[?,19],[2],并且计算出的输入张量为:input [1] = <1 2>。

我收到上面提供的错误,我真的不明白为什么。

目前没有合适的数据,我只是调试这3个函数(创建生成器,创建鉴别器,创建GAN)

我已经尝试调试它并更改值,我真的不明白问题是什么。

# define the standalone discriminator model
def define_discriminator(in_shape=(44,19)):
    model = Sequential()
    #3D tensor with shape (batch_size, timesteps, input_dim). Keras convention says batch size is ommited.
    #Input shape is (sample size, time steps, number of input features)
    model.add(SimpleRNN(units=19, return_sequences=True, input_shape=in_shape))
    model.add(SimpleRNN(units=30, return_sequences=True))
    model.add(SimpleRNN(units=30))
    model.add(Dense(units=1))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Dense(19, activation='sigmoid'))
    # compile model
    opt = Adam(lr=0.0002)
    model.compile(loss='mean_absolute_error', optimizer=opt, metrics=['accuracy'])
    #metrics=[keras_metrics.precision(), keras_metrics.recall()
    return model

# define the standalone generator model
def define_generator(in_shape=(44,19)):
    # foundation for 7x7 image
    model = Sequential()
    # 3D tensor with shape (batch_size, timesteps, input_dim).
    # Input shape is (sample size, time steps, number of input features)
    model.add(SimpleRNN(units=19, return_sequences=True,input_shape=in_shape))
    model.add(SimpleRNN(units=30, return_sequences=True))
    model.add(SimpleRNN(units=30))
    model.add(Dense(units=1))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Dense(19, activation='sigmoid'))
    # compile model
    opt = Adam(lr=0.0002)
    model.compile(loss='mean_absolute_error', optimizer=opt, metrics=['accuracy'])
    # metrics=[keras_metrics.precision(), keras_metrics.recall()
    return model

# define the combined generator and discriminator model, for updating the generator
def define_gan(g_model, d_model):
    # make weights in the discriminator not trainable
    d_model.trainable = False
    # connect them
    model = Sequential()
    # add generator
    model.add(g_model)
    # add the discriminator
    model.add(d_model)
    # compile model
    opt = Adam(lr=0.0002)
    model.compile(loss='mean_absolute_error', optimizer=opt)
    return model

在这一点上,我只想能够对44行19列的数据进行GAN训练。

谢谢!

0 个答案:

没有答案