keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
62.15k stars 19.49k forks source link

Error when creating Siamese Network using functional API #6376

Closed mdshopon closed 7 years ago

mdshopon commented 7 years ago

I created a Siamese Network for paraphrase classification. Here is my code

input_sentence = Input(shape=(25,))

input_a = Input(shape=(25,))
input_b = Input(shape=(25,))

input_embedding = (Embedding(nb_words + 1, EMBEDDING_DIM, input_length=MAX_SEQUENCE_LENGTH, mask_zero=True, trainable=False,
                         weights=[word_embedding_matrix]))(input_sentence)
LSTM_layer = LSTM(output_dim=128, init='he_normal',activation='relu')(input_embedding)

main1=Model(inputs=[input_sentence], outputs=[LSTM_layer])
model1=main1(input_a)
model2=main1(input_b)

concatenated = merge([model1, model2], mode='concat',concat_axis=1)
out = Dense(1, activation='sigmoid')(concatenated)

main_model = Model(input=[input_a,input_b], output=[out])

main_model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

main_model.fit([Q1_train,Q2_train],y_train,nb_epoch=3, batch_size=128,verbose=1,shuffle=True,validation_data=([Q1_test,Q2_test],y_test))

When fitting the data in the model the following error raised

raise MissingInputError(error_msg, variable=r)
theano.gof.fg.MissingInputError: Input 0 of the graph (indices start from 0), used to compute Elemwise{neq,no_inplace}(/input_1, InplaceDimShuffle{x,x}.0), was not provided and not given a value. Use the Theano flag exception_verbosity='high', for more information on this error. 
Backtrace when that variable is created:

  File "/home/codehead/MEGA/Python_Workspace/Siamese_Quora/Siamese with Functional API.py", line 58, in <module>
    input_sentence = Input(shape=(25,))
  File "/home/codehead/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1388, in Input
    input_tensor=tensor)
  File "/home/codehead/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1299, in __init__
    name=self.name)
  File "/home/codehead/anaconda2/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 184, in placeholder
    x = T.TensorType(dtype, broadcast)(name)

Backtrace when the variable is created:
  File "/home/codehead/MEGA/Python_Workspace/Siamese_Quora/Siamese with Functional API.py", line 58, in <module>
    input_sentence = Input(shape=(25,))
  File "/home/codehead/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1388, in Input
    input_tensor=tensor)
  File "/home/codehead/anaconda2/lib/python2.7/site-packages/keras/engine/topology.py", line 1299, in __init__
    name=self.name)
  File "/home/codehead/anaconda2/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 184, in placeholder
    x = T.TensorType(dtype, broadcast)(name)
Imorton-zd commented 7 years ago

Are you sure that the Model can get input? Maybe, you can take two input to feed into input_embedding and LSTM_layer respectively.

stale[bot] commented 7 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.