Python में Deep Reinforcement Learning
Timothée Carayol
Principal Machine Learning Engineer, Komment

for step = 1 to T do: # Take optimal action according to value function # Observe next state and reward # Append transition to replay buffer# इसे सबसे ऊँची प्राथमिकता (1) दें# पिछले ट्रांज़िशन का एक बैच सैंपल करें# प्राथमिकता के आधार पर (2)# बैच के लिए TD errors निकालें# लॉस निकालें और Q Network अपडेट करें# importance sampling weights उपयोग करें (4)# सैंपल किए गए ट्रांज़िशन की प्राथमिकता अपडेट करें (3)# समय के साथ importance sampling बढ़ाएँ. (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 error से अपडेट करें: $p_i = |\delta_i| + \varepsilon$
(4) importance sampling weights उपयोग करें $$w_i = \left( \frac{1}{N} \cdot \frac{1}{P(i)} \right)^\beta\ \ \ \ \ \ \ \ (0<\beta<1)$$
(5) धीरे-धीरे $\beta$ को 1 की ओर बढ़ाएँ
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)# पैरामीटर स्टोर करें और प्राथमिकताएँ इनिशियलाइज़ करें self.alpha, self.beta, self.beta_increment, self.epsilon = (alpha, beta, beta_increment, epsilon) self.priorities = deque(maxlen=capacity)...
... def push(self, state, action, reward, next_state, done): # अनुभव को मेमोरी बफर में जोड़ें experience_tuple = (state, action, reward, next_state, done) self.memory.append(experience_tuple)# नए ट्रांज़िशन की प्राथमिकता को अधिकतम पर सेट करें max_priority = max(self.priorities) if self.memory else 1.0 self.priorities.append(max_priority)...
def sample(self, batch_size): priorities = np.array(self.priorities) # सैंपलिंग प्रायिकताएँ निकालें probabilities = priorities**self.alpha / np.sum(priorities**self.alpha)# सैंपल्ड इंडिसीज़ को रैंडमली चुनें indices = np.random.choice(len(self.memory), batch_size, p=probabilities)# वेट्स निकालें 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]))# टेन्सर्स रिटर्न करें 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)
... def update_priorities(self, indices, td_errors: torch.Tensor): # सैंपल्ड ट्रांज़िशन की प्राथमिकताएँ अपडेट करें for idx, td_error in zip(indices, td_errors.abs()): self.priorities[idx] = abs(td_error.item()) + self.epsilondef increase_beta(self): # beta को 1 की ओर बढ़ाएँ self.beta = min(1.0, self.beta + self.beta_increment)
प्री-लूप कोड में:
buffer = PrioritizedReplayBuffer(capacity)
हर एपिसोड की शुरुआत में:
buffer.increase_beta()
3. हर स्टेप पर:
# एक्शन चुनने के बाद buffer.push(state, action, reward, next_state, done) ...# TD errors निकालने से पहले: replay_buffer.sample(batch_size) ...# TD errors निकालने के बाद buffer.update_priorities(indices, td_errors)loss = torch.sum(weights * (td_errors ** 2))
Cartpole एनवायरनमेंट में 100 ट्रेनिंग रन:

100 epochs के बाद:

400 epochs के बाद:


Python में Deep Reinforcement Learning