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

Class Model

rl2/cartpole/q_learning_bins.py:55–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

53
54
55class Model:
56 def __init__(self, env, feature_transformer):
57 self.env = env
58 self.feature_transformer = feature_transformer
59
60 num_states = 10**env.observation_space.shape[0]
61 num_actions = env.action_space.n
62 self.Q = np.random.uniform(low=-1, high=1, size=(num_states, num_actions))
63
64 def predict(self, s):
65 x = self.feature_transformer.transform(s)
66 return self.Q[x]
67
68 def update(self, s, a, G):
69 x = self.feature_transformer.transform(s)
70 self.Q[x,a] += 1e-2*(G - self.Q[x,a])
71
72 def sample_action(self, s, eps):
73 if np.random.random() < eps:
74 return self.env.action_space.sample()
75 else:
76 p = self.predict(s)
77 return np.argmax(p)
78
79
80def play_one(model, eps, gamma):

Callers 1

q_learning_bins.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected