शुरू करेंमुफ़्त में शुरू करें

PER बफर से सैंपलिंग

एजेंट को ट्रेन करने के लिए Prioritized Experience Buffer क्लास का उपयोग करने से पहले, आपको .sample() मेथड इम्प्लीमेंट करना है. यह मेथड आर्ग्युमेंट के रूप में वह सैंपल साइज़ लेता है जिसे आप ड्रॉ करना चाहते हैं, और रिटर्न में सैंपल किए गए ट्रांज़िशन tensors के रूप में देता है, साथ ही उनके मेमोरी बफर में इंडेक्स और उनका इम्पोर्टेंस वेट भी देता है.

आपके एनवायरनमेंट में 10 कैपेसिटी वाला एक बफर पहले से लोड है, जिससे आप सैंपल कर सकते हैं.

यह अभ्यास पाठ्यक्रम का हिस्सा है

Python में Deep Reinforcement Learning

पाठ्यक्रम देखें

अभ्यास निर्देश

  • प्रत्येक ट्रांज़िशन से जुड़ी सैंपलिंग प्रायिकता की गणना करें.
  • सैंपल में आने वाले ट्रांज़िशन के अनुरूप इंडेक्स ड्रॉ करें; np.random.choice(a, s, p=p) एरे a से प्रायिकता एरे p के आधार पर, रिप्लेसमेंट के साथ, साइज़ s का सैंपल लेता है.
  • प्रत्येक ट्रांज़िशन से जुड़ा इम्पोर्टेंस वेट निकालें.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

def sample(self, batch_size):
    priorities = np.array(self.priorities)
    # Calculate the sampling probabilities
    probabilities = ____ / np.sum(____)
    # Draw the indices for the sample
    indices = np.random.choice(____)
    # Calculate the importance weights
    weights = (1 / (len(self.memory) * ____)) ** ____
    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_tensor = torch.tensor(states, dtype=torch.float32)
    rewards_tensor = torch.tensor(rewards, dtype=torch.float32)
    next_states_tensor = torch.tensor(next_states, dtype=torch.float32)
    dones_tensor = torch.tensor(dones, dtype=torch.float32)
    weights_tensor = torch.tensor(weights, dtype=torch.float32)
    actions_tensor = torch.tensor(actions, dtype=torch.long).unsqueeze(1)
    return (states_tensor, actions_tensor, rewards_tensor, next_states_tensor,
            dones_tensor, indices, weights_tensor)

PrioritizedReplayBuffer.sample = sample
print("Sampled transitions:\n", buffer.sample(3))
कोड संपादित करें और चलाएँ