NotImplementedError: Learning rate schedule must override get_config
NotImplementedError: Learning rate schedule must override get_config
我使用 tf.keras 创建了自定义计划,但在保存模型时遇到此错误:
NotImplementedError: Learning rate schedule must override get_config
class 看起来像这样:
class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
def __init__(self, d_model, warmup_steps=4000):
super(CustomSchedule, self).__init__()
self.d_model = d_model
self.d_model = tf.cast(self.d_model, tf.float32)
self.warmup_steps = warmup_steps
def __call__(self, step):
arg1 = tf.math.rsqrt(step)
arg2 = step * (self.warmup_steps**-1.5)
return tf.math.rsqrt(self.d_model) * tf.math.minimum(arg1, arg2)
def get_config(self):
config = {
'd_model':self.d_model,
'warmup_steps':self.warmup_steps
}
base_config = super(CustomSchedule, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
当您使用自定义子类模型时,保存模型架构有点棘手。相反,使用 Model.save_weights() 仅保存权重更容易。
如果您将代码更改为此,您将不会看到该错误:
def get_config(self):
config = {
'd_model': self.d_model,
'warmup_steps': self.warmup_steps,
}
return config
我使用 tf.keras 创建了自定义计划,但在保存模型时遇到此错误:
NotImplementedError: Learning rate schedule must override get_config
class 看起来像这样:
class CustomSchedule(tf.keras.optimizers.schedules.LearningRateSchedule):
def __init__(self, d_model, warmup_steps=4000):
super(CustomSchedule, self).__init__()
self.d_model = d_model
self.d_model = tf.cast(self.d_model, tf.float32)
self.warmup_steps = warmup_steps
def __call__(self, step):
arg1 = tf.math.rsqrt(step)
arg2 = step * (self.warmup_steps**-1.5)
return tf.math.rsqrt(self.d_model) * tf.math.minimum(arg1, arg2)
def get_config(self):
config = {
'd_model':self.d_model,
'warmup_steps':self.warmup_steps
}
base_config = super(CustomSchedule, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
当您使用自定义子类模型时,保存模型架构有点棘手。相反,使用 Model.save_weights() 仅保存权重更容易。
如果您将代码更改为此,您将不会看到该错误:
def get_config(self):
config = {
'd_model': self.d_model,
'warmup_steps': self.warmup_steps,
}
return config