class Model:
def __init__(self):
self.model = Sequential()
self.model.add(Conv2D(24, 3, 2, 'valid', input_shape=(75, 75, 3)))
self.model.add(BatchNormalization())
self.model.add(Conv2D(24, 3, 2))
self.model.add(BatchNormalization())
self.model.add(Conv2D(24, 3, 2))
self.model.add(BatchNormalization())
self.model.add(Conv2D(24, 3, 2))
self.model.add(BatchNormalization())
self.model.add(Flatten())
def get_model(self):
return self.model
class CNN_MLP:
def __init__(self):
model = Model()
self.model = model.get_model()
self.optimizer = optimizers
def get_model(self):
self.model = self.extend(self.model)
return self.model
def extend(self, model):
self.model = model
self.sequence = Input(shape=(75, 75, 3), name='Sequence')
self.features = Input(shape=(11, ), name='Features')
conv_sequence = self.model(self.sequence)
merged_features = concatenate([conv_sequence, self.features])
fc1 = Dense(256, activation='relu')(merged_features)
fc2 = Dense(256, activation='relu')(fc1)
logits = Dense(10, activation='softmax')(fc2)
# In the following statement I am getting the error
self.model = Model(inputs=[self.sequence, self.features], outputs=[logits])
return self.model
我正在尝试执行以上代码,并遇到上述错误。我使用的是Keras的 2.2.4-tf 版本。我无法理解该错误的原因。
有人可以帮助我识别并解决问题吗?
谢谢!
编辑1:完整追溯:
<ipython-input-29-5112dc1649fd> in <module>()
1 if args.model == 'CNN_MLP':
2 model = CNN_MLP()
----> 3 model = model.get_model()
1 frames
<ipython-input-28-6491bbcf21c5> in get_model(self)
6
7 def get_model(self):
----> 8 self.model = self.extend(self.model)
9 return self.model
10
<ipython-input-28-6491bbcf21c5> in extend(self, model)
20 logits = Dense(10, activation='softmax')(fc2)
21
---> 22 self.model = Model(inputs=[self.sequence, self.features], outputs=[logits])
23 return self.model
TypeError: __init__() got an unexpected keyword argument 'inputs'
答案 0 :(得分:2)
您定义了一个名为Model
的类,因此它遮盖了该类keras.models.Model
,因此,当您尝试实例化Model
时,它将使用您的类而不是Keras'。
一个简单的解决方案是完全限定通话中的程序包名称:
self.model = keras.models.Model(inputs=[self.sequence, self.features], outputs=[logits])