keras 参数必须是字符串或数字,而不是“ExponentialDecay”

u0sqgete  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(171)

我使用Tensorflow 2.4.0,并尝试对学习率执行指数衰减,如下所示:

learning_rate_scheduler = tf.keras.optimizers.schedules.ExponentialDecay(initial_learning_rate=0.1, decay_steps=1000, decay_rate=0.97, staircase=False)

并使用这种衰减方法开始我的优化器的学习率:

optimizer_to_use = Adam(learning_rate=learning_rate_scheduler)

模型编制如下

model.compile(loss=metrics.contrastive_loss, optimizer=optimizer_to_use, metrics=[accuracy])

列车运行良好,直到第三个历元,此时显示以下错误:

File "train_contrastive_siamese_network_inception.py", line 163, in run_experiment
    history = model.fit([pairTrain[:, 0], pairTrain[:, 1]], labelTrain[:], validation_data=([pairTest[:, 0], pairTest[:, 1]], labelTest[:]), batch_size=config.BATCH_SIZE, epochs=config.EPOCHS, callbacks=callbacks)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 1145, in fit
    callbacks.on_epoch_end(epoch, epoch_logs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py", line 432, in on_epoch_end
    callback.on_epoch_end(epoch, numpy_logs)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py", line 2542, in on_epoch_end
    old_lr = float(K.get_value(self.model.optimizer.lr))
TypeError: float() argument must be a string or a number, not 'ExponentialDecay'

我检查了这个问题,甚至提出了在官方keras Forum,但没有成功,即使在那里.另外,该文档明确指出:
LearningRateSchedule示例可以作为任何优化器的learning_rate参数传入。
会是什么问题呢?

daolsyd0

daolsyd01#

传入model.compile()的参数不完全一样,您在损失参数loss=metrics.contrastive_loss中定义了metrics,应该是tfa.losses.ContrastiveLoss()
如果您使用的是TensorFlow 2.4,则需要安装特定版本的tensorflow_addons(0.10 - 0.14之间)才能访问和使用tensorflow addons API-ContrastiveLoss

固定编码为:

model.compile(loss = tfa.losses.ContrastiveLoss(), 
              optimizer = optimizer_to_use, 
              metrics = ['accuracy'])

(这里附上复制的代码gist供您参考。)

相关问题