keras - keras中的断言错误

标签 keras keras-layer

我正在尝试在 keras 中编写 GAN,但在运行它时遇到此断言错误。经过搜索我发现问题的最可能原因是旧版本的theano。我将 theano 更新到最新的 github 开发版本 0.9.0beta1 但我仍然遇到同样的错误。

from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.optimizers import SGD

print "Setting up decoder"
D = Sequential()
D.add(Dense(100, input_dim=100, activation='relu'))
D.add(Dropout(0.5))
D.add(Dense(50, activation='relu'))
D.add(Dropout(0.5))
D.add(Dense(1, activation='sigmoid'))
sgd = SGD(lr=0.01, momentum=0.1)
D.compile(loss='binary_crossentropy', optimizer=sgd)

print "Setting up generator"
G = Sequential()
G.add(Dense(1, input_dim=1, activation='relu'))
G.add(Dropout(0.5))
G.add(Dense(50, activation='relu'))
G.add(Dropout(0.5))
G.add(Dense(1, activation='sigmoid'))
sgd = SGD(lr=0.01, momentum=0.1)
G.compile(loss='binary_crossentropy', optimizer=sgd)

print "Setting up combined net"
gen_dec = Sequential()
gen_dec.add(G)
D.trainable=False
gen_dec.add(D)
gen_dec.compile(loss='binary_crossentropy', optimizer=sgd)
gen_dec.summary()

问题发生在gen_dec.add(D)这一段

assert input_shape[-1] and input_shape[-1] == self.input_dim
AssertionError

最佳答案

我认为这是你代码中的错字......请将生成器的最后一层从:

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

到:

G.add(Dense(100, activation='sigmoid'))

我猜你不希望你的生成器只产生 1 个像素,因为你的鉴别器需要 100 个输入。

错误来自于您的第一个模型输出形状为 (batch_size, 1) 的张量,而您的第二个模型输入的输入形状为 (batch_size, 100)。因此断言错误。

它现在正在我的笔记本电脑上编译。

关于keras - keras中的断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42323035/

相关文章:

python - Keras 图像大小错误

python - 如何使用 keras imagedatagenerator 为unet加载图像和掩码

machine-learning - Python/Keras/Theano 深度自动编码器的错误维度

python - Keras 在 model.fit() 之后删除图层

tensorflow - Keras后端: Difference between random_normal and random_normal_variable

python - 如何在 TF 2.2 中创建自定义 PreprocessingLayer

performance - 如何将准确度绘制为 Keras 中处理时间的函数?

计算对数的keras层?

python - Keras 连接层 : Difference between different types of concatenate functions

python - 获取 Keras 模型/层的输出