Python 有效计算Tensorflow中的成对排序损失函数

Python 有效计算Tensorflow中的成对排序损失函数,python,machine-learning,tensorflow,deep-learning,Python,Machine Learning,Tensorflow,Deep Learning,我目前正在tensorflow中实现 我实现了成对排序损失函数(本文第2.5节),如下所示: s_theta_y = tf.gather(tf.reshape(s_theta, [-1]), y_true_index) s_theta_c_temp = tf.reshape(tf.gather(tf.reshape(s_theta, [-1]), y_neg_index), [-1, classes_size]) s_theta_c = tf.reduce_max(s_theta_c_temp,

我目前正在tensorflow中实现

我实现了成对排序损失函数(本文第2.5节),如下所示:

s_theta_y = tf.gather(tf.reshape(s_theta, [-1]), y_true_index)
s_theta_c_temp = tf.reshape(tf.gather(tf.reshape(s_theta, [-1]), y_neg_index), [-1, classes_size])
s_theta_c = tf.reduce_max(s_theta_c_temp, reduction_indices=[1])
s_theta_y = tf.gather_nd(s_theta, y_t_index)
s_theta_c_temp = tf.gather_nd(s_theta, y_neg_index)
s_theta_c = tf.reduce_max(s_theta_c_temp, reduction_indices=[1])
我不得不使用tf.gather而不是tf.gather\n,因为后者还没有用梯度下降实现。我还必须用展平矩阵变换所有索引以使其正确

如果tf.gather\u nd是通过梯度下降实现的,那么我的代码如下所示:

s_theta_y = tf.gather(tf.reshape(s_theta, [-1]), y_true_index)
s_theta_c_temp = tf.reshape(tf.gather(tf.reshape(s_theta, [-1]), y_neg_index), [-1, classes_size])
s_theta_c = tf.reduce_max(s_theta_c_temp, reduction_indices=[1])
s_theta_y = tf.gather_nd(s_theta, y_t_index)
s_theta_c_temp = tf.gather_nd(s_theta, y_neg_index)
s_theta_c = tf.reduce_max(s_theta_c_temp, reduction_indices=[1])
s_theta是每个类别标签的计算分数,如本文所示。 y_true_index包含true类的索引,用于计算s_θy。y#u neg#u指数是所有负类的指数,其维数为#class-1或#class,即关系分类为其他

然而,有几个句子被归类为其他句子,因此,s_theta_y 不存在,我们不应该在计算时考虑它。为了处理这种情况,我有一个常数因子0来抵消这个项,为了使负类具有相同的维度向量,我只复制了索引的随机值,因为最后,我们只对所有负类(而不是索引)中的最大值感兴趣


有没有更有效的方法来计算损失函数中的这些项?我的印象是,使用tf.gather进行如此多的整形非常缓慢

当然这听起来像是你想要的,但在那里实现渐变之前,我会毫不犹豫地使用你的整形()解决方案,因为整形()实际上是免费的

看起来它做了很多工作,但都只是对形状信息进行快速错误检查。“工作”发生在第90行的CopyFrom中,听起来可能很昂贵,但实际上只是一个指针副本(复制指针的调用)

这是完全有意义的:底层缓冲区只是一个平面的数字数组,排序不依赖于形状信息。出于同样的原因,像tf.transpose()这样的东西通常需要复制