python - 计算keras LSTM中向量之间的距离和角度

标签 python neural-network deep-learning keras lstm

enter image description here我正在使用 keras 来检测问题对之间的相似性。模型结构似乎工作正常,但它在 model.fit 函数上给我错误。我什至检查了输入数据的数据类型,它是 numpy.ndarray。在这方面的任何指示,我将不胜感激。

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 arrays but instead got the following list of 2 arrays: [array([[ 0, 0, 0, ..., 251, 46, 50], [ 0, 0, 0, ..., 7, 40, 6935], [ 0, 0, 0, ..., 17, 314, 2317], ..., [ 0, ...

def Angle(inputs):

    length_input_1=K.sqrt(K.sum(tf.pow(inputs[0],2),axis=1,keepdims=True))
    length_input_2=K.sqrt(K.sum(tf.pow(inputs[1],2),axis=1,keepdims=True))
    result=K.batch_dot(inputs[0],inputs[1],axes=1)/(length_input_1*length_input_2)
    angle = tf.acos(result)
    return angle

def Distance(inputs):

    s = inputs[0] - inputs[1]
    output = K.sum(s ** 2,axis=1,keepdims=True)
    return output    




y=data.is_duplicate.values
tk=text.Tokenizer()
tk.fit_on_texts(list(data.question1.values)+list(data.question2.values))


question1 = tk.texts_to_sequences(data.question1.values)
question1 = sequence.pad_sequences(question1,maxlen=MAX_LEN)

question2 = tk.texts_to_sequences(data.question2.values)
question2 = sequence.pad_sequences(question2,maxlen=MAX_LEN)


word_index = tk.word_index
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
num_features = 300
num_workers = multiprocessing.cpu_count()
context_size = 5
downsampling = 7.5e-06
seed = 1
min_word_count = 5
hs = 1
negative = 5


Quora_word2vec = gensim.models.Word2Vec(

    sg=0,
    seed=1,
    workers=num_workers,
    min_count=min_word_count,
    size=num_features,
    window=context_size,  # (2 and 5)
    hs=hs,  # (1 and 0)
    negative=negative,  # (5 and 10)
    sample=downsampling  # (range (0, 1e-5). )

)

Quora_word2vec = gensim.models.KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin',binary=True)
embedding_matrix=np.zeros((len(word_index)+1,300))

for word , i in tqdm(word_index.items()): #i is index

    try:

        embedding_vector =  Quora_word2vec[word] #Exception is thrown if there is key error
        embedding_matrix[i] = embedding_vector

    except Exception as e:  #If word is not found continue

        continue

--------问题1--------

model1 = Sequential()
print "Build Model"

model1.add(Embedding(
    len(word_index)+1,
    300,
    weights=[embedding_matrix],
    input_length=MAX_LEN
    ))

model1.add(SpatialDropout1D(0.2))
model1.add(TimeDistributed(Dense(300, activation='relu')))
model1.add(Lambda(lambda x: K.sum(x, axis=1), output_shape=(300,)))

print model1.summary()

#--------问题2--------#

model2=Sequential()

model2.add(Embedding(
    len(word_index) + 1,
    300,
    weights=[embedding_matrix],
    input_length=MAX_LEN
    ))  # Embedding layer
model2.add(SpatialDropout1D(0.2))
model2.add(TimeDistributed(Dense(300, activation='relu')))
model2.add(Lambda(lambda x: K.sum(x, axis=1), output_shape=(300,)))


print model2.summary()


#---------Merged------#

#Here you get question embedding

#Calculate distance between vectors
Distance_merged_model=Sequential()
Distance_merged_model.add(Merge(layers=[model1, model2], mode=Distance, output_shape=(1,)))

print Distance_merged_model.summary()

#Calculate Angle between vectors

Angle_merged_model=Sequential()
Angle_merged_model.add(Merge(layers=[model1,model2],mode=Angle,output_shape=(1,)))

print Angle_merged_model.summary()


neural_network = Sequential()
neural_network.add(Dense(2,input_shape=(1,)))
neural_network.add(Dense(1))
neural_network.add(Activation('sigmoid'))

print neural_network.summary()


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

checkpoint = ModelCheckpoint('weights.h5', monitor='val_acc', save_best_only=True, verbose=2)

print type(question1)
print type(question2)
neural_network.fit([question1,question2],y=y, batch_size=384, epochs=10,
                 verbose=1, validation_split=0.3, shuffle=True, callbacks=[checkpoint])

最佳答案

您没有将最后两层连接到密集层中,只是让您的神经网络成为唯一传入数据的网络,因为您正在该层上进行编译和拟合,而没有距离和角度网络连接到您的最终层密集。

获取网络 #1 和 #2 的所有内容在合并层中似乎都是正确的,但您需要执行类似于以下操作的操作:

neural_network = Sequential()
neural_network.add(Merge([Distance_merged_model, Angle_merged_model], mode='concat'))
neural_network.add(Dense(2,input_shape=(1,)))
neural_network.add(Dense(1))
neural_network.add(Activation('sigmoid'))

关于python - 计算keras LSTM中向量之间的距离和角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44137260/

相关文章:

python - Opencv——检测眼睛瞳孔(中)

python - 为什么redis可以绑定(bind)到这个端口,而我不能通过python?

python - Tensorflow 和 TFlearn 错误 - 意外参数 'keepdims'

tensorflow - 如何在 keras 中预测时禁用 dropout?

python - 访问自定义损失函数中的特定元素?

python - 对列表进行排序,然后按原始顺序给出元素的索引

python - 如何在不使用 ORM 的情况下使用 Flask Web 应用程序配置 postgresql?

c++ - 咖啡转换图像集 : symbol lookup error

tensorflow - Keras需要很长时间才能根据model.load()进行首次预测

tensorflow - 如何在迁移学习期间卡住 batch-norm 层