python - Tensorflow 中的成对排名损失函数

标签 python tensorflow machine-learning deep-learning

我正在 Tensorflow 中实现这篇论文 CR-CNN 。 论文中使用的损失函数的项取决于张量和真实标签的运行时值。据我所知,Tensorflow 创建一个静态计算图,然后在 session 中执行它。我发现很难实现本文中提到的预测和损失函数,因为它们都在运行时动态变化。 我尝试在代码中使用 tf.cond() 但这导致“无”作为渐变。因此我的网络根本没有接受训练。

class_scores = tf.matmul(pooled, W_classes)

n_correct = tf.Variable(0, trainable=True)

for t in xrange(batch_size):
    max_arg = tf.cast(tf.argmax(class_scores[t], 1), tf.int32)
    #true_class = tf.constant(0)
    true_class = tf.cast(tf.argmax(Y[t], 1), tf.int32)

    pred_class = tf.Variable(0,trainable=True)
    value = class_scores[t][max_arg]
    tf.cond(value <= 0, lambda: tf.assign(pred_class, 0), lambda: tf.assign(pred_class, max_arg + 1))
    tf.cond(tf.equal(true_class, pred_class), lambda: tf.add(n_correct, 1), lambda: tf.add(n_correct, 0))

    #print(value)

accuracy = tf.cast(n_correct, tf.float32)/tf.cast(batch_size, tf.float32)

在这里,我通过计算正确预测的数量来计算准确性。

损失函数也有类似的方法:

gamma = tf.constant(2.0) 
m_plus = tf.constant(2.5)   
m_minus = tf.constant(0.5)

batch_loss = tf.Variable(0.0, trainable=True)


for t in xrange(batch_size):
    max_arg = tf.cast(tf.argmax(class_scores[t], 1), tf.int32)
    true_class = tf.cast(tf.argmax(Y[t], 1), tf.int32)

    top2_val, top2_i = tf.nn.top_k(class_scores[t], 2, sorted=True)  

    pred_class = tf.Variable(0, trainable=True)
    true_score = tf.Variable(0.0, trainable=True)
    neg_score = tf.Variable(0.0, trainable=True)

    value = class_scores[t][max_arg]

    tf.cond(value <= 0, lambda: tf.assign(pred_class, 0), lambda: tf.assign(pred_class, max_arg + 1))

    tf.cond(tf.equal(true_class, 0), lambda: tf.assign(true_score, 0), lambda: tf.assign(true_score, class_scores[t][true_class-1]))

    tf.cond(tf.equal(true_class, 0), lambda: tf.assign(neg_score, value), lambda: tf.cond(tf.equal(true_class, pred_class), 
                lambda: tf.assign(neg_score, top2_val[1]), lambda: tf.assign(neg_score, value)))

    example_loss = tf.Variable(0.0, trainable=True) 

    tf.cond(tf.equal(true_class, 0), lambda: tf.assign(example_loss, tf.log(1 + tf.exp(tf.multiply(gamma, m_minus + neg_score)))), 
                    lambda: tf.assign(example_loss, tf.log(1 + tf.exp(tf.multiply(gamma, m_plus - true_score))) + tf.log(1 + tf.exp(tf.multiply(gamma, m_minus + neg_score)))))
    batch_loss = tf.add(batch_loss, example_loss)

    #print(neg_score)

batch_loss = batch_loss/batch_size
train_step = tf.train.GradientDescentOptimizer(learning_rate=lambda_t).minimize(batch_loss)

但是网络没有得到训练。 谁能建议如何在 tensorflow 中做到这一点?

最佳答案

这段代码存在一些问题,按原样它是行不通的。我建议您尝试使用tensorflow eager执行,因为您在这里遇到的概念问题在那里不存在(您不需要 tf.cond 或 tf.Variable 来解决您的问题,例如)。

此代码示例如何使用 tf.cond 的问题在于 tf.cond 本质上是函数式的(它将操作添加到图表中,这些操作仅在您执行时才会执行)使用 tf.cond 的返回值)。因此,您的代码需要以某种方式链接 tf.conds (可能通过 tf.control_dependency)以使它们执行。

但是,您在训练示例中也使用了tf.Variables。 Tensorflow 无法通过对 tf.Variable 的赋值进行反向传播,因此您需要替换对 tf.assign 及其 friend 的调用,返回变量的新值并使用它 python 。

关于python - Tensorflow 中的成对排名损失函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47941891/

相关文章:

python-3.x - 形状与多类分类器不兼容

python - 将包含列表项的字典展开为字典对列表

c# - Python 到 C# 的 RGBA 值转换不起作用

python - 对未知代码设置限制的最佳方法是什么?

python - 为什么对 tensorflow 张量进行 pickle 会失败?

matlab - 无需工具箱即可在 MATLAB 中实现遗传算法

python - 如何将 Python 插件添加到 Gnu Global

python-3.x - 如何将注意力层添加到 Bi-LSTM

python - 将 tensorflow 检查点加载为 keras 模型

machine-learning - 两个不同数量主题的 LDA 结果之间的相似性?