__init__() got an unexpected keyword argument 'inputs'

孤者浪人 提交于 2019-12-25 01:39:02

问题


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

I am trying to execute the above code and getting the above mentioned error. I am on version 2.2.4-tf of Keras. I am unable to understand the reason behind the error.

Could anyone help me identifying and thus fixing the issue?

Thank you!

Edit 1: Full traceback:

<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'

回答1:


You defined a class called Model, so this shadows the class keras.models.Model, so when you try to instance Model, it uses your class instead of Keras'.

A simple solution would be to fully qualify the package name in the call:

self.model = keras.models.Model(inputs=[self.sequence, self.features], outputs=[logits])


来源:https://stackoverflow.com/questions/56617806/init-got-an-unexpected-keyword-argument-inputs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!