Advantage Actor Critic

Pythonで学ぶDeep Reinforcement Learning

Timothée Carayol

Principal Machine Learning Engineer, Komment

なぜ Actor-Critic か?

 

  • REINFORCEの限界:

    • 分散が大きい
    • サンプル効率が低い
  • Actor-Criticはクリティックを導入し、TD学習を可能にする

「agent」と書かれた大きな長方形。その中に「actor」「critic」と書かれた2つの小さな長方形。

Pythonで学ぶDeep Reinforcement Learning

Actor-Critic の直感的理解

テーブルを囲んで話す学生たち。周りに本とペン。

 

  • アクターネットワーク:

    • 意思決定を行う
    • 自分では評価できない
  • クリティックネットワーク:

    • 毎ステップでアクターにフィードバック
Pythonで学ぶDeep Reinforcement Learning

クリティックネットワーク

 

  • クリティックは状態価値関数を近似

クリティックネットワークの図。入力は状態、出力は価値関数。出力ノードは1つのみ。

  • アドバンテージ(TD誤差)に基づいて行動 a_t を評価

 

class Critic(nn.Module):
    def __init__(self, state_size):
        super(Critic, self).__init__()
        self.fc1 = nn.Linear(state_size, 64)
        self.fc2 = nn.Linear(64, 1)

def forward(self, state): x = torch.relu(self.fc1(torch.tensor(state))) value = self.fc2(x) return value
critic_network = Critic(8)
Pythonで学ぶDeep Reinforcement Learning

Actor-Critic の動作

 

  • 各ステップで:
    • アクターが行動を選択(REINFORCEのポリシーネットと同様)

上部: 「agent」と書かれた大きな長方形。その中に「actor」「critic」と書かれた小さな長方形が2つ。下部: 別の長方形に「environment」。

Pythonで学ぶDeep Reinforcement Learning

Actor-Critic の動作

 

  • 各ステップで:
    • アクターが行動を選択(REINFORCEのポリシーネットと同様)
    • クリティックが報酬と状態を観測

「action」と書かれた赤い矢印が、ActorからEnvironmentへ。

Pythonで学ぶDeep Reinforcement Learning

Actor-Critic の動作

 

  • 各ステップで:
    • アクターが行動を選択(REINFORCEのポリシーネットと同様)
    • クリティックが報酬と状態を観測
    • クリティックがTD誤差を評価
    • アクターとクリティックがTD誤差で重みを更新

「State」「Reward」と書かれた2本の赤い矢印が、EnvironmentからCriticへ。

Pythonで学ぶDeep Reinforcement Learning

Actor-Critic の動作

 

  • 各ステップで:
    • アクターが行動を選択(REINFORCEのポリシーネットと同様)
    • クリティックが報酬と状態を観測
    • クリティックがTD誤差を評価
    • アクターとクリティックがTD誤差で重みを更新
    • 更新後のアクターが新しい状態を観測

「TD error」と書かれた矢印がCriticからActorへ。

Pythonで学ぶDeep Reinforcement Learning

Actor-Critic の動作

 

  • 各ステップで:
    • アクターが行動を選択(REINFORCEのポリシーネットと同様)
    • クリティックが報酬と状態を観測
    • クリティックがTD誤差を評価
    • アクターとクリティックがTD誤差で重みを更新
    • 更新後のアクターが新しい状態を観測
  • … 繰り返し

Stateの矢印がActorにも向かうようになる。

Pythonで学ぶDeep Reinforcement Learning

A2C の損失関数

 

クリティック

クリティックの損失関数。クリティックには二乗TD誤差を用いる: Lc(theta c) = ((r_t + gamma * V theta c (s t + 1)) - V theta c) squared

  • クリティック損失: 二乗TD誤差

 

アクター

アクターの損失関数。各時刻tで次を用いる: L(theta) = −(行動の対数確率) × (TD誤差/アドバンテージ)。

  • TD誤差はクリティックの評価を反映
  • TD誤差が正の行動の確率を上げる
Pythonで学ぶDeep Reinforcement Learning

損失の計算方法

 

def calculate_losses(critic_network, action_log_prob, 
                     reward, state, next_state, done):

# Critic provides the state value estimates value = critic_network(state)
next_value = critic_network(next_state)
td_target = (reward + gamma * next_value * (1-done))
td_error = td_target - value
# Apply formulas for actor and critic losses actor_loss = -action_log_prob * td_error.detach()
critic_loss = td_error ** 2
return actor_loss, critic_loss

 

 

  • TD誤差を計算
  • アクター損失を計算
    • .detach()で勾配のクリティックへの伝播を止める
  • クリティック損失を計算
Pythonで学ぶDeep Reinforcement Learning

Actor-Critic の学習ループ

for episode in range(10):
  state, info = env.reset()
  done = False
  while not done:

# Select action action, action_log_prob = select_action(actor, state)
next_state, reward, terminated, truncated, _ = env.step(action) done = terminated or truncated
# Calculate losses actor_loss, critic_loss = calculate_losses(critic, action_log_prob, reward, state, next_state, done)
# Update actor actor_optimizer.zero_grad(); actor_loss.backward(); actor_optimizer.step()
# Update critic critic_optimizer.zero_grad(); critic_loss.backward(); critic_optimizer.step()
state = next_state
Pythonで学ぶDeep Reinforcement Learning

Passons à la pratique !

Pythonで学ぶDeep Reinforcement Learning

Preparing Video For Download...