思路
首先,现在的情况是一份jd或者cv有多句话m,每句话有多个词n。
首先,要解决的问题是输入向量的格式问题,
embedding_shape=(batch_size,time_step,embedding_length,channel_size)
其中channel_size:每份jd或者cv中包含的句子个数;time_step:每个句子中词语的个数。
接着思路是,构建模型,将每个句子转为一个向量,然后将多个句子再转为一个向量。
具体模型如下,一个句子(多个时间序列性质的词语)经过双向LSTM获得output和state,然后state和output经过一系列的数组操作(与权重矩阵相乘--->relu--->softmat得到weight),得到每个时间步的权重后,再与每个时间步输出相乘,再reduce_sum,得到带有权重影响的向量作为结果。多个句子也执行上述操作,最终得到的是带有词权重和句子权重影响的向量。
```python
class BiLstmProcess(keras.Model):
def __init__(self,batch_size,units):
super(BiLstmProcess, self).__init__()
self.batch_size = batch_size
self.units = units
self.bilstm = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(self.units,return_sequences=True,return_state=True))
def call(self,x,hidden):
result = self.bilstm(x,initial_state=hidden)
output = result[0]
cstate = tf.concat([result[2],result[4]],axis=1)
return output,cstate
def initialize_hidden_state(self):
return [tf.zeros([self.batch_size,self.units]),
tf.zeros([self.batch_size, self.units]),
tf.zeros([self.batch_size, self.units]),
tf.zeros([self.batch_size, self.units])
]
class OneAttention(keras.Model):
def __init__(self,units):
super(OneAttention, self).__init__()
self.W1 = tf.keras.layers.Dense(units)
self.W2 = tf.keras.layers.Dense(units)
self.V = tf.keras.layers.Dense(1)
# cstate:代表经过lstm后,最后的cstate. houtput代表的是所有时间步的输出(每个时间步的hstate)
def call(self,cstate,houtput):
# shape:(batch_size,1,embedding_length)
cstate = tf.expand_dims(cstate,axis=1)
# shape:(baict_size,sentence_length,units) ----> shape:(batch_size,sentence_length,1)
score = self.V(tf.nn.relu(self.W1(houtput) + self.W2(cstate)))
# 获得α权重
attention_weights = tf.nn.softmax(score)
# 然后获取句子向量
# houtput.shape:(batch_size,sentence_length,embedding_length)
# attention_weights.shape:(batch_size,sentence_length,1)
context_vec = tf.reduce_sum(houtput*attention_weights,axis=1)
return context_vec
```
利用文本匹配模型simense网络,对生成的jd_vec和cv_vec进行合并操作,+dense+softmax