admin管理员组文章数量:1398999
I'm trying to write a custom loss function to train a neural net with Keras/Tensorflow. However, when I train the model, I get the following error:
ValueError: No gradients provided for any variable: ['dense/kernel:0', 'dense/bias:0', 'dense_1/kernel:0', 'dense_1/bias:0', 'dense_2/kernel:0', 'dense_2/bias:0', 'dense_3/kernel:0', 'dense_3/bias:0', 'dense_4/kernel:0', 'dense_4/bias:0'].
Below is my loss function. The goal is to convert a tensor that has probabilities for coordinate boxes into a coordinate prediction and then compute the distance between the coordinates to use that as my loss.
def haversine_distance(lat1, lon1, lat2, lon2):
R = 3958.8 # Radius of the Earth in miles
# lat1, lon1, lat2, lon2 = map(tf.math.radians, [lat1, lon1, lat2, lon2])
dlat = lat2 - lat1
dlon = lon2 - lon1
a = tf.sin(dlat / 2.0) ** 2 + tf.cos(lat1) * tf.cos(lat2) * tf.sin(dlon / 2.0) ** 2
c = 2 * tf.atan2(tf.sqrt(a), tf.sqrt(1 - a))
return R * c
def custom_loss(y_true, y_pred):
# Convert box probabilities into coordinates
true_coords = tf.map_fn(get_coordinates, tf.cast(y_true, dtype=tf.float32)) # (batch_size, 2)
pred_coords = tf.map_fn(get_coordinates, tf.cast(y_pred, dtype=tf.float32)) # (batch_size, 2)
lat1, lon1 = tf.unstack(true_coords, axis=1)
lat2, lon2 = tf.unstack(pred_coords, axis=1)
return haversine_distance(lat1, lon1, lat2, lon2)
def get_coordinates(prediction, box_thresh=0.03):
total = 0
lat_p = 0
lon_p = 0
for i in range(x_boxes*y_boxes):
pred = tf.keras.backend.get_value(prediction[i])
if pred > box_thresh:
total = total + pred
for i in range(x_boxes*y_boxes):
pred = tf.keras.backend.get_value(prediction[i])
if pred > box_thresh:
lat_p += pred * centers[i][0] / total
lon_p += pred * centers[i][1] / total
result = [lat_p, lon_p]
return tf.convert_to_tensor(result, dtype=tf.float32)
I think it has to do with the function not being differentiable, but I'm not sure why it wouldn't be. Thanks for the help!
本文标签: Custom Lost Function in Tensorflow Keras has no GradientStack Overflow
版权声明:本文标题:Custom Lost Function in Tensorflow Keras has no Gradient - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744113844a2591418.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论