实现基于LSTM的Siamese NN架构

时间:2017-10-16 13:49:45

标签: python keras-2

问题是在给定模态答案或正确答案的情况下比较给定学生对教师的答案,并为学生答案分配成绩。经过大量的研究,带有LSTM的连体结构似乎是神经网络架构的一个很好的选择,因为这个问题非常类似于找到两对句子之间的文本相似性。 我目前采用了最简单的架构,其中一个嵌入层使用预先训练的word2vec模型,在嵌入的顶部添加LSTM,采用多对一架构,然后使用余弦相似度计算正确答案与学生给定答案之间的相似性

模型创建似乎很好,但我在使用余弦相似度量合并两个LSTM时遇到问题

这是我的代码..

true_ans_model = Sequential()
true_ans_model.add(Embedding(nbwords+1, EMBEDDING_DIM, weights= weightMatrixWithPadding], mask_zero=True, trainable=False ))
x_left = true_ans_model.add(LSTM(MAX_SENTENCE_LENGTH,return_sequences=False))

stud_ans_model = Sequential()
stud_ans_model.add(Embedding(nbwords+1, EMBEDDING_DIM, weights=[weightMatrixWithPadding], mask_zero=True, trainable=False ))
stud_ans_model.add(LSTM(MAX_SENTENCE_LENGTH, return_sequences=False))

merge_lstms = merge([x_left, x_right], mode='cos', dot_axes=1)

这是错误:

ValueError                                Traceback (most recent call last)
~\Anaconda3\lib\site-packages\keras\engine\topology.py in assert_input_compatibility(self, inputs)
   418             try:
--> 419                 K.is_keras_tensor(x)
420             except ValueError:

~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in is_keras_tensor(x)
392                           tf.SparseTensor)):
--> 393         raise ValueError('Unexpectedly found an instance of type `' + str(type(x)) + '`. '
394                          'Expected a symbolic tensor instance.')

ValueError: Unexpectedly found an instance of type `<class 'NoneType'>`. Expected a symbolic tensor instance.

During handling of the above exception, another exception occurred:
~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in is_keras_tensor(x)
392                           tf.SparseTensor)):
--> 393         raise ValueError('Unexpectedly found an instance of type `' + str(type(x)) + '`. '
 394                          'Expected a symbolic tensor instance.')

ValueError: Unexpectedly found an instance of type `<class 'NoneType'>`. 
Expected a symbolic tensor instance.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
    <ipython-input-41-7a6e14b23349> in <module>()
----> 1 merge_lstms = merge([x_left, x_right], mode='cos', dot_axes=1)
  #final_layer = dot([x_left,x_right], axes=1, normalize=True)
  predictions = Dense(1, activation = 'sigmoid')(final_layer)

 ~\Anaconda3\lib\site-packages\keras\legacy\layers.py in merge(inputs, mode, concat_axis, dot_axes, output_shape, output_mask, arguments, name)
  466                             arguments=arguments,
  467                             name=name)
--> 468         return merge_layer(inputs)
  469 
470 

~\Anaconda3\lib\site-packages\keras\engine\topology.py in __call__(self, inputs, **kwargs)
550                 # Raise exceptions in case the input is not compatible
551                 # with the input_spec specified in the layer constructor.
--> 552                 self.assert_input_compatibility(inputs)
553 
554                 # Collect input shapes to build layer.

~\Anaconda3\lib\site-packages\keras\engine\topology.py in assert_input_compatibility(self, inputs)
    423                                  'Received type: ' +
    424                                  str(type(x)) + '. Full input: ' +
--> 425                                  str(inputs) + '. All inputs to the 
layer '
    426                                  'should be tensors.')


ValueError: Layer merge_3 was called with an input that isn't a symbolic tensor. Received type: <class 'NoneType'>. Full input: [None, None]. All inputs to the layer should be tensors.

0 个答案:

没有答案