Пріоритезоване повторне відтворення досвіду

Глибоке навчання з підкріпленням у Python

Timothée Carayol

Principal Machine Learning Engineer, Komment

Не всі досвіди однаково корисні

 

  • Experience Replay:
    • Рівномірна вибірка може проґавити важливі спогади
  • Prioritized Experience Replay:
    • Присвоює кожному досвіду пріоритет на основі помилок TD
    • Зосереджується на прикладах з високим навчальним потенціалом

 

Студенти навчаються в бібліотеці

Глибоке навчання з підкріпленням у Python

Prioritized Experience Replay (PER)

 

for step = 1 to T do:
    # Take optimal action according to value function
    # Observe next state and reward
    # Append transition to replay buffer

# Give it highest priority (1)
# Sample a batch of past transitions
# Based on priority (2)
# Calculate TD errors for the batch
# Calculate the loss and update the Q Network
# Use importance sampling weights (4)
# Update priority of sampled transitions (3)
# Increase importance sampling over time. (5)

(1) Нові переходи додаються з найвищим пріоритетом $p_i = \max_k(p_k)$

(2) Вибирайте перехід $i$ з імовірністю $$P(i) = p_i^{\alpha} / \sum_k p_k^{\alpha}\ \ \ \ \ \ \ \ (0<\alpha<1)$$

(3) Для вибраних переходів оновіть пріоритет до їхньої помилки TD: $p_i = |\delta_i| + \varepsilon$

(4) Використовуйте ваги importance sampling $$w_i = \left( \frac{1}{N} \cdot \frac{1}{P(i)} \right)^\beta\ \ \ \ \ \ \ \ (0<\beta<1)$$

(5) Поступово збільшуйте $\beta$ до 1

Глибоке навчання з підкріпленням у Python

Реалізація PER

def __init__(self, capacity, alpha=0.6, beta=0.4, beta_increment=0.001, epsilon=0.001):
    # Initialize memory buffer
    self.memory = deque(maxlen=capacity)

# Store parameters and initialize priorities self.alpha, self.beta, self.beta_increment, self.epsilon = (alpha, beta, beta_increment, epsilon) self.priorities = deque(maxlen=capacity)
...
Глибоке навчання з підкріпленням у Python

Реалізація PER

...

def push(self, state, action, reward, next_state, done):
    # Append experience to memory buffer
    experience_tuple = (state, action, reward, next_state, done)
    self.memory.append(experience_tuple)

# Set priority of new transition to maximum priority max_priority = max(self.priorities) if self.memory else 1.0 self.priorities.append(max_priority)
...
Глибоке навчання з підкріпленням у Python

Реалізація PER

def sample(self, batch_size):
    priorities = np.array(self.priorities)
    # Calculate sampling probabilities
    probabilities = priorities**self.alpha / np.sum(priorities**self.alpha)

# Randomly select sampled indices indices = np.random.choice(len(self.memory), batch_size, p=probabilities)
# Calculate weights weights = (1 / (len(self.memory) * probabilities)) ** self.beta weights /= np.max(weights) states, actions, rewards, next_states, dones = zip(*[self.memory[idx] for idx in indices]) weights = [weights[idx] for idx in indices] states, actions, rewards, next_states, dones = (zip(*[self.memory[idx] for idx in indices]))
# Return tensors states = torch.tensor(states, dtype=torch.float32) ... # Repeat for rewards, next_states, dones, weights actions = torch.tensor(actions, dtype=torch.long).unsqueeze(1) return (states, actions, rewards, next_states, dones, indices, weights)
Глибоке навчання з підкріпленням у Python

Реалізація PER

...

def update_priorities(self, indices, td_errors: torch.Tensor):
    # Update priorities for sampled transitions
    for idx, td_error in zip(indices, td_errors.abs()):
        self.priorities[idx] = abs(td_error.item()) + self.epsilon

def increase_beta(self): # Increment beta towards 1 self.beta = min(1.0, self.beta + self.beta_increment)
Глибоке навчання з підкріпленням у Python

PER у тренувальному циклі DQN

 

  1. У коді до циклу:

    buffer = PrioritizedReplayBuffer(capacity)
    
  2. На початку кожного епізоду:

    buffer.increase_beta()
    

3. На кожному кроці:

# After selecting an action
buffer.push(state, action, reward, 
            next_state, done)
...

# Before calculating the TD errors: replay_buffer.sample(batch_size) ...
# After calculating the TD errors buffer.update_priorities(indices, td_errors)
loss = torch.sum(weights * (td_errors ** 2))
Глибоке навчання з підкріпленням у Python

PER у дії: Cartpole

100 навчальних запусків у середовищі Cartpole:

  1. з Prioritized Experience Replay
  2. з Uniform Experience Replay
  • PER навчається швидше й дає кращі результати, ніж рівномірне повторення досвіду

Криві навчання показують, що PER навчається швидше

 

Після 100 епох: Cartpole, нестабільний після 100 епох

 

Після 400 епох: Cartpole, стабільний після 400 епох

Глибоке навчання з підкріпленням у Python

PER у дії: середовища Atari

 

  • Значне зростання продуктивності з PER у середовищах Atari

Стовпчикова діаграма порівнює результати humans, DQN, DDQN, Dueling DDQN, Prioritized DDQN і Prioritized Dueling DQN. Перші чотири збігаються з діаграмою з попереднього уроку про Dueling DQN. Остання показує, що введення Prioritized Experience Replay покращує результати DDQN.

1 https://arxiv.org/abs/2303.11634
Глибоке навчання з підкріпленням у Python

Давайте потренуємось!

Глибоке навчання з підкріпленням у Python

Preparing Video For Download...