我想随着步骤的增加改变施加在损失上的权重。为了实现这一点,我使用了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) #我想在这里使用步骤数来降低权重。 loss = -tf.reduce_sum(weight*y_true*tf.math.log(y_pred))/y_shape[1]/y_shape[2] #对CategoricalCrossentropy施加权重 return loss
回答:
编辑(这是因为你没有告诉函数step的值,这将是一个局部变量,函数无法收集,因为它有自己的局部变量。)
我假设你通过迭代来设置步骤。简单地将其作为call函数的输入添加进去。
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) #我想在这里使用步骤数来降低权重。 loss = -tf.reduce_sum(weight*y_true*tf.math.log(y_pred))/y_shape[1]/y_shape[2] #对CategoricalCrossentropy施加权重 return losskeras_weight = 1keras_example = CategoricalCrossentropy(keras_weight)for step in range(1, max_step+1): # 假设步骤不能等于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 #再次假设步骤不能等于0 def call(self, y_true, y_pred): weight = self.weight*np.exp(-1.0*self.step) #我想在这里使用步骤数来降低权重。 loss = -tf.reduce_sum(weight*y_true*tf.math.log(y_pred))/y_shape[1]/y_shape[2] #对CategoricalCrossentropy施加权重 self.step += 1 # 增加步骤 return loss
希望这对你有帮助