Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow自定义函数:ValueError:没有为任何变量提供渐变_Python_Tensorflow_Keras_Loss Function - Fatal编程技术网

Python Tensorflow自定义函数:ValueError:没有为任何变量提供渐变

Python Tensorflow自定义函数:ValueError:没有为任何变量提供渐变,python,tensorflow,keras,loss-function,Python,Tensorflow,Keras,Loss Function,在自定义损失函数中使用K.round函数时,出现以下错误: ValueError: No gradients provided for any variable: ['sequential_20/dense_240/kernel:0', 'sequential_20/dense_240/bias:0', 'sequential_20/dense_241/kernel:0', 'sequential_20/dense_241/bias:0', 'sequential_20/dense_242/ke

在自定义损失函数中使用
K.round
函数时,出现以下错误:

ValueError: No gradients provided for any variable: ['sequential_20/dense_240/kernel:0', 'sequential_20/dense_240/bias:0', 'sequential_20/dense_241/kernel:0', 'sequential_20/dense_241/bias:0', 'sequential_20/dense_242/kernel:0', 'sequential_20/dense_242/bias:0', 'sequential_20/dense_243/kernel:0', 'sequential_20/dense_243/bias:0', 'sequential_20/dense_244/kernel:0', 'sequential_20/dense_244/bias:0', 'sequential_20/dense_245/kernel:0', 'sequential_20/dense_245/bias:0', 'sequential_20/dense_246/kernel:0', 'sequential_20/dense_246/bias:0', 'sequential_20/dense_247/kernel:0', 'sequential_20/dense_247/bias:0', 'sequential_20/dense_248/kernel:0', 'sequential_20/dense_248/bias:0', 'sequential_20/dense_249/kernel:0', 'sequential_20/dense_249/bias:0', 'sequential_20/dense_250/kernel:0', 'sequential_20/dense_250/bias:0', 'sequential_20/dense_251/kernel:0', 'sequential_20/dense_251/bias:0'].
这是我的工作代码示例:(如果我在没有
K.round
的情况下使用loss函数,这就是工作)

有什么建议吗


谢谢

本例中的问题很简单,
K.round
不是一个可微函数,因此不提供梯度(结果为无),您不能将任何不可微函数用作损失函数。

问题已解决。这与Tensorflow版本有关
K.round
2.2.0
中受支持。我的问题在
2.1.0

中,问题已解决。这与Tensorflow版本有关
K.round
2.2.0
中受支持。我的问题在
2.1.0
添加答案和解决方案并接受它,然后,您实际上没有提供任何版本信息。因此,我将答案留在这里,因为一些人可能会因为不支持的版本而面临此问题,并认为它很有帮助
def adjusted_loss(y_true, y_pred): 
    y_pred = K.round(y_pred / 1000) * 1000
    loss = y_pred - y_true
    return loss

model = tf.keras.Sequential()
model.add(layers.Dense(1, activation = LeakyReLU()))
model.compile(loss= adjusted_loss, optimizer= opt)