【问题标题】:Keras loss function dependent on batch sizeKeras 损失函数取决于批量大小
【发布时间】:2019-06-24 12:53:12
【问题描述】:

我正在尝试在 Keras 中构建一个损失函数,其中我正在惩罚预测与一组给定值之间的最小距离。问题是我需要计算预测值和给定值之间的距离。

示例代码

def custom_loss(y_pred,y_test):


    #Given values
    centers=K.constant([[-2.5,-1],[-1.25,-2],[.5,-1],[1.5,.25]])
    num_centers=K.int_shape(centers)[0]


    #Begin constructing distance matrix
    height=K.int_shape(y_pred)[0]
    i=0
    current_center=K.reshape(K.repeat(K.reshape(centers[i,:],[1,-1]),height),[height,2])
    current_dist=K.sqrt(K.sum(K.square(y_pred-current_center),axis=1))


    #Values of distance matrix for first center
    Distance=K.reshape(current_dist,[height,1])


    for i in range(1,num_centers):
        current_center=K.reshape(K.repeat(K.reshape(centers[i,:],[1,-1]),height),[height,2])
        current_dist=K.sqrt(K.sum(K.square(y_pred-current_center),axis=-1))
        current_dist=K.reshape(current_dist,[height,1])


        #Iteratively concatenate distances of y_pred from remaining centers
        Distance=K.concatenate([Distance,current_dist],axis=-1)

    #Determine minimum distance from each predicted value to nearest center
    A=K.min(A,axis=1)


    #Return average minimum distance as loss
    return K.sum(A)/float(height)

但是,我无法消除函数对 y_pred 第一维的依赖性,该维是可变的。我正在使用数组广播来计算 y_pred 和每个给定值之间的差异,但我明确地使用批量大小进行广播,因为我不知道如何在不使用 Keras 中的批量大小的情况下执行此操作。但是,这会产生错误,因为在构建计算图时批量大小不是明确知道的。

如何避免显式广播?有没有更有效的计算这个距离矩阵的方法,因为目前的方法很笨拙?

【问题讨论】:

  • y_pred 的大小为 [?,2],其中第一个维度表示未指定的批量大小。

标签: python machine-learning keras


【解决方案1】:

您的损失函数可以使用隐式广播来实现,如下所示:

import keras.backend as K


def custom_loss(y_true, y_pred):
    centers = K.constant([[-2.5, -1], [-1.25, -2], [.5, -1], [1.5, .25]])

    # Expand dimensions to enable implicit broadcasting
    y_pred_r = y_pred[:, None, :]  # Shape: (batch_size, 1, 2)
    centers_r = centers[None, :, :]  # Shape: (1, nb_centers, 2)

    # Compute minimum distance to centers for each element
    distances = K.sqrt(K.sum(K.square(y_pred_r - centers_r), axis=-1))  # Shape=(batch_size, nb_centers)
    min_distances = K.min(distances, axis=-1)  # Shape=(batch_size,)

    # Output average of minimum distances
    return K.mean(min_distances)

注意:未经测试。

【讨论】:

    猜你喜欢
    • 2021-07-23
    • 1970-01-01
    • 2016-09-22
    • 2019-02-09
    • 2020-07-29
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    相关资源
    最近更新 更多