Search code examples
pythonkerasdeep-learningkeras-2

'Tensor' object has no attribute 'compile'


Following is the code snippet:

    merged_model = Sequential()
    merged_model = concatenate([model1.output, model2.output, model3.output, model4.output, model5.output])
    x = BatchNormalization()(merged_model)

    x = Dense(300)(x)
    x = PReLU()(x)
    x = Dropout(0.2)(x)
    x = BatchNormalization()(x)

    x = Dense(1)(x)
    out = Activation('sigmoid')(x)

    mergerd_model = Model([model1.input, model2.input, model3.input, model4.input, model5.input], [out])

    merged_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

    merged_model.fit([x1, x2, x3, x4, x5], y=y, batch_size=384, nb_epoch=20,
             verbose=1, validation_split=0.1, shuffle=True, callbacks=[checkpoint])

But when I try to run it, I get following error:

    Traceback (most recent call last):
  File "t1.py", line 167, in <module>
    merged_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
AttributeError: 'Tensor' object has no attribute 'compile'

When I tried viewing the type of merged_model I get this:

<class 'tensorflow.python.framework.ops.Tensor'>

Solution

  • The main problem is a typo: when constructing the model, it should be merged_model not mergerd_model (i.e. remove the extra "r"). However, the following line is not needed as well since you are using Functional API and you can remove it:

    merged_model = Sequential()