INNER CODE UNIT · Python
choose_action
rasbt/machine-learning-book · ch19/cartpole/main.py:68
def choose_action(self, state):
if np.random.rand() <= self.epsilon:
return np.random.choice(self.action_size)
with torch.no_grad():
q_values = self.model(torch.tensor(state, dtype=torch.float32))[0]
return torch.argmax(q_values).item() # returns action
def _learn(self, batch_samples):
batch_states, batch_targets = [], []
for transition in batch_samples:
s, a, r, next_s, done = transition
with torch.no_grad():
if done:
target = r
else:
pred = self.model(torch.tensor(next_s, dtype=torch.float32))[0]
target = r + self.gamma * pred.max()