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 5 years have passed since last update.

強化学習43 ChainerRLの学習方法を読む optimizerから追跡してみる

Last updated at Posted at 2019-12-25

中学生から大学生のAI初学者を対象にしています。
習うより慣れろできましたが、少し習います。
まずは、ソースコードを読むことから始めます。
githubからダウンロードすると便利です。
https://github.com/chainer/chainerrl

git clone https://github.com/chainer/chainerrl.git

ダウンロードしたフォルダー chainerrlを基準にソースコードの読みます。
ソースコードは、chainerrl/chainerrlの中にあります。
pythonでimportするときのフォルダー指定は、chainerrl/cainerrl/agentsの場合、

from chinerrl.agent import DQN

のようになります。

さて、機械学習なる部分の学習(train)は、optimizerがやります。
本シリーズでは、optimizerは、agentのオブジェクト生成(初期化)に引数として渡されていました。

基本1 optimizerはagentに入る

※強調したいだけなので、見出しじゃないです。
これは、agentが自分の中にあるニューラルネットワークの訓練を、自分で行っているということです。
それでは、どのタイミングでagentは訓練をするのか調べてみます。
強化学習では、agentがobservationを受け取り、actionを返します。この部分を読むと、

action = agent.act_and_train(observation)

となっています。

 基本2 agent.act_and_trainで学習する。

そこで、agentの中身を見ます。
chainerrl/agents/dqn.py
optimizerで検索します。検索は、Edit-Findでやりましょう。
class DQNのdef__init__にあります。
159行目のself.optimizer=optimizerで定義され、
def updateの271行目にself.optimizer.update()とあるので、
agent.update()されたときに、optimizer.update()もされることがわかります。

 基本3 agent.update()されたときに、optimizer.update()もされる

それでは、update()はいつされるかですが、
def__init__の173行目から176行目までに
update_func=self.update
と書いてあります。これは、その下に続く、self.replay_updaterに引数として渡されています。

 基本4 agent.update()は、replay_updaterに引数で渡される。

そして、self.replay_updaterは、act_and_trainの405行目で実行されています。
これで、基本2につながりました。
以下の順番で呼び出されるみたいです。
1 agent.act_and_train
2 self.replay_updater.update_if_necessary(self.t)
3 agent.update
4 optimizer.update

一部、読んでいないところがあります。
replay_updaterのところです。

from chainerrl.replay_buffer import ReplayUpdater

なので、chainerrl/replay_buffer.pyを読みます。
230行目にReplayUpdaterがあります。
262行目から続くdef update_if_necessaryの中に、277行目と280行目にself.update_funcがありました。
ついでにここを読んでみると、episodeごとにupdateするのか、そうでないのかわかります。また、batchsizeをここで指定していることもわかります。

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?