0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Tensorflow 1.15でoptimizerのlearning_rateをいじるLearningRateScheduler

Last updated at Posted at 2023-02-22

Tensorflow 1.15でoptimizerのlearning_rateをいじりたい

実装環境の事情があってtf1.15の学習をしなくてはならなくなりました。
それで学習結果を絞りあげたいのでoptimizerのlearning_rateを絞っていきたいのですが…
2.xxでは adam.lr みたいに直接触れるのでなんとでもなって扱いやすいですが1.xxでは

optimizer.lrを見ると

<tf.Variable 'Adamax_2/learning_rate:0' shape=() dtype=float32>

となっていて、そうはいかないみたいで。

LearningRateScheduler

LearningRateSchedulerが使えるということで調べるも、これもバージョンに違いがあるのか?ググってでた記事のコードそのままだとエラーが出たりします。

ReduceLROnPlateau

ReduceLROnPlateauというlossとかval_lossとか見てて何回か改善が見られなかったらlearning_rateを指定した分減衰してくれる便利そうなのもあるのですが、なんかこれ動いてるのかどうかいまいちわからなかったです。今learning_rateを下げたよ!とかなんとかprintとかしてくれないと結果が微妙なものだと評価しずらいというか、やりがいがないというか。

コード

でいろいろ試して結局動いたコードが

from keras.callbacks import LearningRateScheduler

def step_decay(epoch,learning_rate):
    
    print( f"step_decay,epoch="+str(epoch)+",lr="+str(learning_rate))
    
    if epoch >= 10: learning_rate *= 0.2
    if epoch >= 20: learning_rate *= 0.2
    return learning_rate

lr_decay = LearningRateScheduler(step_decay)

:
:
model.fit_generator(
    train_data_generator,
    validation_data=(test_x, test_y),
    steps_per_epoch=1000,
    callbacks=[lr_decay],
    epochs=50
)

これなら、step_decayの中でなんとでもなるし、printを出したりも自由にできます。

よかったよかった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?