Prioritized experience replay बफ़र
आप PrioritizedExperienceReplay क्लास पेश करेंगे, एक डेटा स्ट्रक्चर जिसे आप आगे चलकर Prioritized Experience Replay के साथ DQN इम्प्लीमेंट करने में उपयोग करेंगे।
PrioritizedExperienceReplay, ExperienceReplay क्लास का उन्नत रूप है जिसका उपयोग आप अब तक अपने DQN एजेंट्स को ट्रेन करने के लिए कर रहे थे। एक prioritized experience replay बफ़र यह सुनिश्चित करता है कि इससे सैंपल किए गए ट्रांजिशन, यूनिफॉर्म सैंपलिंग की तुलना में, एजेंट के सीखने के लिए अधिक उपयोगी हों।
अभी के लिए, मेथड्स .__init__(), .push(), .update_priorities(), .increase_beta() और .__len__() इम्प्लीमेंट करें। अंतिम मेथड .sample() अगला अभ्यास का विषय होगा।
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Deep Reinforcement Learning
अभ्यास निर्देश
.push()में, ट्रांजिशन की प्रायोरिटी को बफ़र में अधिकतम प्रायोरिटी से इनिशियलाइज़ करें (या यदि बफ़र खाली है तो 1 से)।.update_priorities()में, प्रायोरिटी को संबंधित TD error के परिमाण के बराबर सेट करें; edge cases को कवर करने के लिएself.epsilonजोड़ें।.increase_beta()में, beta कोself.beta_incrementसे बढ़ाएँ; सुनिश्चित करें किbeta1 से कभी अधिक न हो।
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
class PrioritizedReplayBuffer:
def __init__(
self, capacity, alpha=0.6, beta=0.4, beta_increment=0.001, epsilon=0.01
):
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)
# Initialize the transition's priority
max_priority = ____
self.memory.append(experience_tuple)
self.priorities.append(max_priority)
def update_priorities(self, indices, td_errors):
for idx, td_error in zip(indices, td_errors.tolist()):
# Update the transition's priority
self.priorities[idx] = ____
def increase_beta(self):
# Increase beta if less than 1
self.beta = ____
def __len__(self):
return len(self.memory)
buffer = PrioritizedReplayBuffer(capacity=3)
buffer.push(state=[1,3], action=2, reward=1, next_state=[2,4], done=False)
print("Transition in memory buffer:", buffer.memory)
print("Priority buffer:", buffer.priorities)