Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
keras中的断言错误_Keras_Keras Layer - Fatal编程技术网

keras中的断言错误

keras中的断言错误,keras,keras-layer,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 u

我试图在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)


我认为这是你代码中的一个输入错误。。。请将生成器的最后一层更改为:

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

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

错误源于以下事实:第一个模型输出的张量形状
(batch\u size,1)
,而第二个模型输入的输入形状为
(batch\u size,100)
。因此出现了断言错误


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

什么断言错误?你的问题中没有包括它。我试图在keras中移植这个pytorch代码。作者使用1个输入大小作为生成器,100个输入大小作为鉴别器。我在这里谈论的是生成器的输出,而不是输入。在代码中,他们使用一些函数来生成数据。请参见代码中的预处理函数。你不能复制一个算法,然后跳过一些类似的部分:)修复im建议修复im建议也很好,唯一的区别是它使用隐式分布。你提供的链接使用一个显式分布*根据我提出的修正,神经网络学习数据分布本身,而不是使用正态定律或任何东西
G.add(Dense(1, activation='sigmoid'))
G.add(Dense(100, activation='sigmoid'))