1

When I tried to run this:

p0 = Sequential()
p0.add(Embedding(vocabulary_size1, 50, weights=[embedding_matrix_passage], 
input_length=50, trainable=False))
p0.add(LSTM(64))
p0.add(Dense(256,name='FC1'))
p0.add(Activation('relu'))
p0.add(Dropout(0.5))
p0.add(Dense(50,name='out_layer'))
p0.add(Activation('sigmoid'))

q0 = Sequential()
q0.add(Embedding(vocabulary_size2,50,weights=embedding_matrix_query], 
input_length=50, trainable=False))
q0.add(LSTM(64))
q0.add(Dense(256,name='FC1'))
q0.add(Activation('relu'))
q0.add(Dropout(0.5))
q0.add(Dense(50,name='out_layer'))
q0.add(Activation('sigmoid'))

model = concatenate([p0.output, q0.output])
model = Dense(10)(model)
model = Activation('softmax')(model)
model.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics= 
['accuracy'])

it's giving me this error:

AttributeError             
--->  model.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics=['accuracy'])
today
  • 32,602
  • 8
  • 95
  • 115
pratikkarad
  • 114
  • 2
  • 13
  • As you define it, `model` is indeed a tensor, and not a model; you should switch all of your pipeline to the functional API - see [How to “Merge” Sequential models in Keras 2.0?](https://stackoverflow.com/questions/46397258/how-to-merge-sequential-models-in-keras-2-0) – desertnaut Dec 15 '18 at 21:11
  • Yes, your whole code makes little sense, you should use the functional API. – Dr. Snoopy Dec 15 '18 at 21:34

1 Answers1

1

As mentioned in the comments you need to use Keras Functional API to create models with branches, multiple inputs/outputs. However, there is no need to do this for all of your code, just for the last part:

concat = concatenate([p0.output, q0.output])
x = Dense(10)(concat)
out = Activation('softmax')(x)

model = Model([p0.input, q0.input], out)

model.compile(loss='categorical_crossentropy',optimizer='rmsprop', metrics=['accuracy'])
today
  • 32,602
  • 8
  • 95
  • 115
  • Interesting... So, we *can* use `p0.input` and `q0.input` in such a manner even if `p0` and `q0` are sequential models? – desertnaut Dec 16 '18 at 11:06
  • 1
    @desertnaut Sure, they are valid attributes because the corresponding models have been defined and also they are (placeholder) tensors after all. – today Dec 16 '18 at 11:11
  • 1
    A well-deserved upvote - seems you got both myself & Matias off-guard ;) – desertnaut Dec 16 '18 at 11:13