环境
python3 keras==2.0.6
思路
- 输入:由三部分组成(paper1的sequence+paper2的sequence+规则)
- 输出:一部分(二分类)
学习资料
简单说明
因为我搭建深度模型的经验不太多,所以其实对于很多的代码使用方法都不是特别了解,所以花费了一些时间来学习keras的介绍文档,收获很多。
URL
keras中文文档
merge用法
文本预处理
序列预处理
keras各种layers
embedding层用法
。。。
模型详细说明
简单说明
这是我自己画的一个当前基于keras搭建的模型的示意图(后续可能会根据运行效果进行修改)
模型图
模型创新点
- 使用了 语义 和 规则 的嵌入的拼接
- 在 语义 部分的处理上,使用了双向LSTM,可以更好的获取语义的上下文信息
维度变化
- Inputs:
paper_first:150 paper_second:150 rule:3 - Embedding
paper_first:(150,256) paper_second:(150,256) - LSTM
paper_first:(150,512) paper_second(150,512) - Dense
paper_first:(150,1024)->(150,512) paper_second:(150,1024)->(150,512) - Max Pooling
- paper_first:512 paper_Second:512
- Merge
1024 - Dense
7 - Merge
7+3=10 - Dropout
10 - Dense
2
代码
print('building model...')
embedding_layer = Embedding(len(word_index) + 1,
256,
weights=[embedword_matrix],
input_length=150, trainable=True)
sequence_input1 = Input(shape=(150,), name="first_paper")
sequence_input2 = Input(shape=(150,), name="second_paper")
sequence_input3=Input(shape=(3,),name="rule")
embedded_sequences1 = embedding_layer(sequence_input1)
embedded_sequences2 = embedding_layer(sequence_input2)
LSTM_Left1 = LSTM(512, implementation=2, return_sequences=True, go_backwards=False)(embedded_sequences1)
LSTM_Right1 = LSTM(512, implementation=2, return_sequences=True, go_backwards=True)(embedded_sequences1)
concat1 = merge([LSTM_Left1,LSTM_Right1], mode='concat', concat_axis=-1)
LSTM_Left2 = LSTM(512, implementation=2, return_sequences=True, go_backwards=False)(embedded_sequences2)
LSTM_Right2 = LSTM(512, implementation=2, return_sequences=True, go_backwards=True)(embedded_sequences2)
concat2 = merge([LSTM_Left2,LSTM_Right2], mode='concat', concat_axis=-1)
z1 = Dense(512, activation='tanh')(concat1)
z2 = Dense(512, activation='tanh')(concat2)
z1_MaxPool = Lambda(lambda x: K.max(x, axis=1), output_shape=(512,))(z1)
z2_MaxPool = Lambda(lambda x: K.max(x, axis=1), output_shape=(512,))(z2)
concat=merge([z1_MaxPool,z2_MaxPool], mode='concat', concat_axis=-1)
model_final = Dense(7, activation='relu')(concat)
model_final=merge([model_final,sequence_input3],mode='concat',concat_axis=-1)
model_final = Dropout(0.5)(model_final)
model_final = Dense(2, activation='softmax')(model_final)
model = Model(input=[sequence_input1, sequence_input2,sequence_input3],
outputs=model_final)
adam = optimizers.adam(lr=0.0001)
model.compile(loss='binary_crossentropy',
optimizer=adam,
metrics=['accuracy'])
print(model.summary())
cur_time = time.strftime('%Y-%m-%d-%H-%M', time.localtime(time.time()))
checkpointer = ModelCheckpoint(filepath="model/_model-{epoch:02d}.hdf5", period=1)
model.fit([index_pad_array0_first,index_pad_array0_second,threeRules],y,
epochs=3, batch_size=128, verbose=1,
callbacks=[checkpointer])
model.save("model/final_model" + '.h5')
运行结果部分截图
下面的输出就是print(model.summary)的结果,主要展示了构建的整个模型的各个层级结构。
总结
模型构建过程的一个难点其实就是不同层之间维度的对应关系,这也是经常报错的一个地方。
至此,模型已经基本上搭建完了,当然后续还有许多工作要做,包括测试数据的处理,各种exception的处理,以及代码的整理和整合等等。