MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / replay

Method replay

pytorch/rl_trader.py:311–342  ·  view source on GitHub ↗
(self, batch_size=32)

Source from the content-addressed store, hash-verified

309
310
311 def replay(self, batch_size=32):
312 # first check if replay buffer contains enough data
313 if self.memory.size < batch_size:
314 return
315
316 # sample a batch of data from the replay memory
317 minibatch = self.memory.sample_batch(batch_size)
318 states = minibatch['s']
319 actions = minibatch['a']
320 rewards = minibatch['r']
321 next_states = minibatch['s2']
322 done = minibatch['d']
323
324 # Calculate the target: Q(s',a)
325 target = rewards + (1 - done) * self.gamma * np.amax(predict(self.model, next_states), axis=1)
326
327 # With the PyTorch API, it is simplest to have the target be the
328 # same shape as the predictions.
329 # However, we only need to update the network for the actions
330 # which were actually taken.
331 # We can accomplish this by setting the target to be equal to
332 # the prediction for all values.
333 # Then, only change the targets for the actions taken.
334 # Q(s,a)
335 target_full = predict(self.model, states)
336 target_full[np.arange(batch_size), actions] = target
337
338 # Run one training step
339 train_one_step(self.model, self.criterion, self.optimizer, states, target_full)
340
341 if self.epsilon > self.epsilon_min:
342 self.epsilon *= self.epsilon_decay
343
344
345 def load(self, name):

Callers 1

play_one_episodeFunction · 0.45

Calls 3

train_one_stepFunction · 0.85
predictFunction · 0.70
sample_batchMethod · 0.45

Tested by

no test coverage detected