Tensorflow 自定义学习率调度程序给出意外的 EagerTensor 类型错误

问题描述 投票:0回答:1

下面是我的自定义 LR Scheduler,它是tensorflow.keras.optimizers.schedules.LearningRateSchedule 的子类,出现错误

TypeError: Cannot convert -0.5 to EagerTensor of dtype int64
。真的很困惑为什么 Eagertensor 与此自定义类的返回调用的简单平方反比计算相关..

class lr_schedule(tensorflow.keras.optimizers.schedules.LearningRateSchedule):
    def __init__(self, dim_embed, warmup_steps):
        self.dim_embed = dim_embed
        self.warmup_steps = warmup_steps
    def __call__(self, step):
        return (self.dim_embed ** -0.5) * min((step ** -0.5), step * (self.warmup_steps ** -1.5))

与此错误没有特别相关,但这是一个自定义 LR 调度程序,它复制“Attention is All You Need”论文中使用的预热调度程序..

python tensorflow machine-learning deep-learning transformer-model
1个回答
2
投票

我昨天才遇到这个问题。这是一个类型强制问题,因为传递给

step
__call__
的值是 int64,因此数学会将所有内容转换为 int64。

对于您的具体情况,这可能应该解决它:

class lr_schedule(tensorflow.keras.optimizers.schedules.LearningRateSchedule):
    def __init__(self, dim_embed, warmup_steps):
        self.dim_embed = tensorflow.cast(dim_embed, dtype=tensorflow.float32)
        self.warmup_steps = tensorflow.cast(warmup_steps, dtype=tensorflow.float32)
    def __call__(self, step):
        step = tensorflow.cast(step, dtype=tensorflow.float32)
        return (self.dim_embed ** -0.5) * min((step ** -0.5), step * (self.warmup_steps ** -1.5))

© www.soinside.com 2019 - 2024. All rights reserved.