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