I am creating a custom loss function using tf.raw_ops namespace to train my model using keras. Here is my loss function:
Loss(pred,label)= { 0.0 if pred?label<=0.1, 1.0 elsewhere
comparing_tensor = tf.convert_to_tensor([0.1, 0.1, 0.1])
def custom_loss(y_pred, y_true):
loss_tensor = tf.raw_ops.Abs(x=y_pred - y_true) # get the abs diff between y_true and y_pred
boolean_tensor = tf.raw_ops.Greater(x=loss_tensor, y=comparing_tensor) # get a boolean tensor based on Greater operation. Example: [True, False, True]
binary_tensor = tf.raw_ops.Cast(x=boolean_tensor, DstT=tf.float32) # convert boolean to bianry tensor Example: [1.0, 0.0, 1.0]
mean_tensor= tf.raw_ops.Mean(input=binary_tensor, axis=-1) # get mean of binary tensor, 2/3=0.66
loss = tf.raw_ops.Reshape(tensor=mean_tensor, shape=(1,1), name=None) # reshape mean tensor to get desired shape
return loss
And then I am using this in my
Keras.model.compile(opt=SDG, loss=custom_loss, metrics=['mse])
I am getting an error
ValueError: No gradients provided for any variable: ['conv2d/kernel:0', 'conv2d/bias:0', 'conv2d_1/kernel:0', 'conv2d_1/bias:0', 'conv2d_2/kernel:0', 'conv2d_2/bias:0', 'conv2d_3/kernel:0', 'conv2d_3/bias:0', 'conv2d_4/kernel:0', 'conv2d_4/bias:0', 'dense/kernel:0', 'dense/bias:0', 'x/kernel:0', 'x/bias:0'].**
I know this may be because some of the tf.operations
that I am using are not differentiable, or doesn't have a gradient. However, I have checked this page https://docs.w3cub.com/tensorflow~2.3/raw_ops It shows which operations are differentiable and which are not. All of my operations are differentiable. I am not sure what am I missing. Any help is appreciated.
question from:
https://stackoverflow.com/questions/66062973/tensorflow-custom-loss-function-error-no-gradients-provided-for-any-variable 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…