Python 属性错误:';顺序';无属性';获得u形';合并模型时
我试图创建两个连续的模型(每个模型都基于不同的数据集-不同的图像进行训练)。然后,我想取它们输出的平均值,并添加一个softmax层,以基于两个连续模型为基础提供单个分类输出。下面是我的代码,但我得到一个属性错误,它说“Sequential”对象没有属性“get_shape” 完整错误代码为:Python 属性错误:';顺序';无属性';获得u形';合并模型时,python,multiple-columns,keras,conv-neural-network,multiclass-classification,Python,Multiple Columns,Keras,Conv Neural Network,Multiclass Classification,我试图创建两个连续的模型(每个模型都基于不同的数据集-不同的图像进行训练)。然后,我想取它们输出的平均值,并添加一个softmax层,以基于两个连续模型为基础提供单个分类输出。下面是我的代码,但我得到一个属性错误,它说“Sequential”对象没有属性“get_shape” 完整错误代码为: Traceback (most recent call last): File "Mergedmodels.pyu", line 135, in <module> merged =
Traceback (most recent call last):
File "Mergedmodels.pyu", line 135, in <module>
merged = average ([modelo, modelN1])
File "G:\Anaconda\lib\site-packages\keras\layers\merge.py", line 481, in average
return Average(**kwargs)(inputs)
File "G:\Anaconda\lib\site-packages\keras\engine\topology.py", line 542, in _ call_input_shapes.append(K.int_sshape(x_elem))
File "G:\Anaconda\lib\site-packages\keras\backend\tensorflow_backend.py", line 411, in int_shape
shape = x.get_shape()
AttributeError: 'Sequential' object has no attribute 'get_shape'
这种组合顺序模型的方法在Keras2.0中似乎不起作用 因为作用于张量而不是层。这就是错误消息的原因,该错误消息表示顺序模型没有
get\u shape()
方法<代码>获取形状()仅存在于张量上
下面是一个复制错误的示例:
mod1 = Sequential()
mod1.add(Dense(1, input_shape=(10,)))
mod2 = Sequential()
mod2.add(Dense(1, input_shape=(10,)))
avg = average([mod1, mod2]) # throws AttributeError
解决这个问题的一个简单方法是使用
两个模型的输出,然后进行softmax层。例如:
X1 = np.random.rand(10, 10)
X2 = np.random.rand(10, 10)
Y = np.random.choice(2, 10)
mod1 = Sequential()
mod1.add(Dense(16, input_shape=(10,)))
mod2 = Sequential()
mod2.add(Dense(16, input_shape=(10,)))
# so use the outputs of the models to do the average over
# this way we do averaging over tensor __not__ models.
avg = average([mod1.output, mod2.output])
dense = Dense(1, activation="sigmoid")(avg)
# the two inputs are the inputs to the sequential models
# and the output is the dense layer
mod3 = Model(inputs=[mod1.input, mod2.input], outputs=[dense])
mod3.compile(loss='binary_crossentropy', optimizer='sgd')
mod3.fit([X1, X2], Y)
这种组合顺序模型的方法在Keras2.0中似乎不起作用 因为作用于张量而不是层。这就是错误消息的原因,该错误消息表示顺序模型没有
get\u shape()
方法<代码>获取形状()仅存在于张量上
下面是一个复制错误的示例:
mod1 = Sequential()
mod1.add(Dense(1, input_shape=(10,)))
mod2 = Sequential()
mod2.add(Dense(1, input_shape=(10,)))
avg = average([mod1, mod2]) # throws AttributeError
解决这个问题的一个简单方法是使用
两个模型的输出,然后进行softmax层。例如:
X1 = np.random.rand(10, 10)
X2 = np.random.rand(10, 10)
Y = np.random.choice(2, 10)
mod1 = Sequential()
mod1.add(Dense(16, input_shape=(10,)))
mod2 = Sequential()
mod2.add(Dense(16, input_shape=(10,)))
# so use the outputs of the models to do the average over
# this way we do averaging over tensor __not__ models.
avg = average([mod1.output, mod2.output])
dense = Dense(1, activation="sigmoid")(avg)
# the two inputs are the inputs to the sequential models
# and the output is the dense layer
mod3 = Model(inputs=[mod1.input, mod2.input], outputs=[dense])
mod3.compile(loss='binary_crossentropy', optimizer='sgd')
mod3.fit([X1, X2], Y)
在哪一行抛出属性错误?在第135行:合并=平均([modelo,modelN1])在哪一行抛出属性错误?在第135行:合并=平均([modelo,modelN1])谢谢!你能给我指一个使用函数API的好教程吗?我还没有用过它。很高兴我能提供帮助,keras文档上有一些好的教程:这里是一个链接。我在回答中也提到了这一点。如果这个答案帮助了你,记得接受它,这样它可以帮助其他人获得keras文档,这个链接帮助我了解了如何创建keras模型,加入模型,只培训模型的一部分,等等:谢谢!非常感谢。你能给我指一个使用函数API的好教程吗?我还没有用过它。很高兴我能提供帮助,keras文档上有一些好的教程:这里是一个链接。我在回答中也提到了这一点。如果这个答案帮助了你,记得接受它,这样它可以帮助其他人获得keras文档,这个链接帮助我了解了如何创建keras模型,加入模型,只培训模型的一部分,等等:谢谢!