【发布时间】:2021-07-09 05:56:46
【问题描述】:
我想随着步长的增加改变对损失施加的权重。为此,我使用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
【问题讨论】:
标签: python tensorflow machine-learning keras tensorflow2.0