通过步长增加自定义损失函数的重量

Reducing weight in custom loss function with step increase

我想随着步长的增加改变对损失施加的权重。为此,我使用 subclass of tf.keras.losses.Loss。然而,它的函数中的参数,如 __init__() 或 call() 似乎无法在计算过程中获得进展。

如何在 tf.keras.losses.Loss 的子类中获取步数?

这是我的代码。

class CategoricalCrossentropy(keras.losses.Loss):
    def __init__(self, weight, name="example"):
        super().__init__(name=name)
        self.weight = weight

    def call(self, y_true, y_pred):

        weight = self.weight*np.exp(-1.0*step) #I'd like to use step number here to reduce weight.
        loss = -tf.reduce_sum(weight*y_true*tf.math.log(y_pred))/y_shape[1]/y_shape[2] #impose weight on CategoricalCrossentropy
        
        return loss

编辑(因为你没有告诉函数 step 的值是什么,这将是一个局部变量,函数将无法收集,因为它有自己的局部变量。)

我假设您正在通过迭代设置步骤。只需将其作为输入添加到调用函数中即可。

class CategoricalCrossentropy(keras.losses.Loss):
    def __init__(self, weight, name="example"):
        super().__init__(name=name)
        self.weight = weight

    def call(self, step, y_true, y_pred):

        weight = self.weight*np.exp(-1.0*step) #I'd like to use step number here to reduce weight.
        loss = -tf.reduce_sum(weight*y_true*tf.math.log(y_pred))/y_shape[1]/y_shape[2] #impose weight on CategoricalCrossentropy
        
        return loss

keras_weight = 1
keras_example = CategoricalCrossentropy(keras_weight)

for step in range(1, max_step+1):     # assuming step cannot = 0
    loss = keras_example.call(1, y_true, y_perd)

如果您希望该步骤成为对象记住的内容,您只需添加一个属性即可。

class CategoricalCrossentropy1(keras.losses.Loss):
    def __init__(self, weight, name="example"):
        super().__init__(name=name)
        self.weight = weight
        self.step = 1           #again, assuming step cannot = 0

    def call(self, y_true, y_pred):
        weight = self.weight*np.exp(-1.0*self.step) #I'd like to use step number here to reduce weight.
        loss = -tf.reduce_sum(weight*y_true*tf.math.log(y_pred))/y_shape[1]/y_shape[2] #impose weight on CategoricalCrossentropy
        
        self.step += 1  # add to step

        return loss

希望这对您有所帮助