我使用tf.keras创建了一个自定义计划,在保存模型时遇到了这个错误:
NotImplementedError:学习进度计划必须覆盖get_config
这门课看起来如下:
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()))发布于 2020-05-21 05:30:33
当您使用自定义子类模型时,保存模型体系结构有点棘手。相反,只使用Model.save_weights()来保存权重比较容易。
如果将代码更改为此,您将不会看到该错误:
def get_config(self):
config = {
'd_model': self.d_model,
'warmup_steps': self.warmup_steps,
}
return confighttps://stackoverflow.com/questions/61557024
复制相似问题