Tensorflow 没有为自定义分段损失函数的任何变量提供梯度

Tensorflow 没有为自定义分段损失函数的任何变量提供梯度,tensorflow,machine-learning,loss-function,gradient,Tensorflow,Machine Learning,Loss Function,Gradient,我创建了一个分段函数y是mnist标签,y是softmax预测结果,pen\u less和pen\u more是两个惩罚参数 loss = tf.reduce_sum(tf.where( tf.greater(tf.to_float(tf.argmax(y, 1)), tf.to_float(tf.argmax(y_, 1))), tf.pow(pen_less, tf.to_float(tf.argmax(y, 1)) - tf.to_float(tf.argmax(y_, 1

我创建了一个分段函数
y
是mnist标签,
y
是softmax预测结果,
pen\u less
pen\u more
是两个惩罚参数

loss = tf.reduce_sum(tf.where(
    tf.greater(tf.to_float(tf.argmax(y, 1)), tf.to_float(tf.argmax(y_, 1))),
    tf.pow(pen_less, tf.to_float(tf.argmax(y, 1)) - tf.to_float(tf.argmax(y_, 1))),
    tf.pow(pen_more, tf.to_float(tf.argmax(y, 1)) - tf.to_float(tf.argmax(y_, 1)))))

编辑:所以
tf。如果传递所有三个参数,
是可微的。我认为您的问题在于argmaxes:

import tensorflow as tf

x = tf.Variable([0, 1, 2])
tf.gradients(tf.argmax(x), x)
输出:

LookupError:没有为操作“ArgMax”定义渐变(操作类型:ArgMax)


如果你想要一个可微的损失函数,你需要避免argmax运算或找到一个聪明的方法来获得一个合适的伪梯度。

我以前也认为这是一个问题,但是像:loss=tf.reduce\u sum(tf.where(tf.more(y,y),(y-y_u)*loss\u more,(y_y-y)*loss\u less)这样的函数可以工作。我不知道为什么。编辑了我的答案。