美文网首页
12句对匹配实战-(2)Match_pyramid

12句对匹配实战-(2)Match_pyramid

作者: 弟弟们的哥哥 | 来源:发表于2019-10-13 21:29 被阅读0次
image.png

两个句子从一开始就交互,这样就会获得更准确的关系。图中可以将单词与单词之间的相似度看成是像素,那么对于两个单词数为M,N的句子,其相似度矩阵就是M*N,然后!就可以用卷积搞事情了.

1.两个词之间的相似度,可以用cosine距离来计算。也可以用点积: image.png

2.两层CNN网络进行特征提取。由于上一层的相似度矩阵Shape不一致,在第一层CNN后面进行maxpool的时候,要使用动态pool。

  1. 最后用两层全连接对CNN结果进行转换,使用softmax得到最终分类概率。
with tf.name_scope('embeddings'):
    self._m_token_embeddings = tf.Variable(
        tf.truncated_normal(
            [self._m_config["vocab_size"], self._m_config["embedding_dim"]],
            stddev=0.1
        ),
        name="token_embeddings"
    )
    embedded_sent1 = tf.nn.embedding_lookup(self._m_token_embeddings, self._m_ph_sent1)
    embedded_sent2 = tf.nn.embedding_lookup(self._m_token_embeddings, self._m_ph_sent2)

    dropout_embedded_sent1 = tf.nn.dropout(embedded_sent1, keep_prob=self._m_ph_keep_prob)
    dropout_embedded_sent2 = tf.nn.dropout(embedded_sent2, keep_prob=self._m_ph_keep_prob)

    # 构建相似性矩阵,并且使用CNN对齐分类
    # sent.shape = [batch_size, sequence_length, dim]
    picture = tf.matmul(dropout_embedded_sent1, dropout_embedded_sent2, transpose_b=True)
    self._m_picture = tf.expand_dims(picture, axis=-1)

pooled_outputs = []
for i, filter_size in enumerate(self._m_config["filter_sizes"]):
    with tf.name_scope("conv-max-pool-%s" % filter_size):
        filter_shape = [filter_size, filter_size, 1, 1]
        conv_weight = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W")

        conv_features = tf.nn.conv2d(
            input=self._m_picture,
            filter=conv_weight,
            strides=[1, 1, 1, 1],
            padding="SAME")

        maxpool_features = tf.nn.max_pool(
            value=conv_features,
            ksize=[1, 4, 4, 1],
            strides=[1, 4, 4, 1],
            padding='VALID',
            name="pool")
        pooled_outputs.append(tf.layers.flatten(tf.squeeze(maxpool_features, axis=3)))

self._m_cnn_features = tf.concat(pooled_outputs, 1)
self._m_cnn_features_dropout = tf.nn.dropout(self._m_cnn_features, self._m_ph_keep_prob)

with tf.name_scope("full_connected_layer"):
    feature_size = self._m_cnn_features_dropout.shape.as_list()[1]
    W = tf.get_variable(
        name="W",
        shape=[feature_size, 2],
        initializer=tf.contrib.layers.xavier_initializer())
    b = tf.Variable(tf.constant(0.1, shape=[2]), name="b")
    self._m_logits = tf.nn.xw_plus_b(self._m_cnn_features_dropout, W, b)

with tf.name_scope("loss"):
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(
                        labels=self._m_ph_label, logits=self._m_logits)
    self._m_loss = tf.reduce_mean(cross_entropy)

with tf.name_scope("accuracy"):
    self._m_prediction = tf.argmax(self._m_logits, axis=1)
    correct = tf.equal(self._m_prediction, tf.argmax(self._m_ph_label, axis=1))
    self._m_accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

with tf.name_scope("optimizer"):
    self._m_global_step = tf.Variable(0, name="global_step", trainable=False)
    self._m_optimizer = tf.train.AdamOptimizer(self._m_config["learning_rate"])
    self._m_train_op = self._m_optimizer.minimize(
                            self._m_loss, global_step=self._m_global_step)

用现在比较火的attention来做,可以吗?下一章来继续讲

相关文章

网友评论

      本文标题:12句对匹配实战-(2)Match_pyramid

      本文链接:https://www.haomeiwen.com/subject/rfxwpctx.html