人岗匹配中attention机制的使用

思路

首先,现在的情况是一份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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值