forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapprox_prediction.py
144 lines (117 loc) · 3.31 KB
/
approx_prediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# https://deeplearningcourses.com/c/artificial-intelligence-reinforcement-learning-in-python
# https://www.udemy.com/artificial-intelligence-reinforcement-learning-in-python
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import matplotlib.pyplot as plt
from grid_world import standard_grid, negative_grid
from iterative_policy_evaluation import print_values, print_policy
from sklearn.kernel_approximation import Nystroem, RBFSampler
GAMMA = 0.9
ALPHA = 0.01
ALL_POSSIBLE_ACTIONS = ('U', 'D', 'L', 'R')
def epsilon_greedy(greedy, s, eps=0.1):
# we'll use epsilon-soft to ensure all states are visited
# what happens if you don't do this? i.e. eps=0
p = np.random.random()
if p < (1 - eps):
return greedy[s]
else:
return np.random.choice(ALL_POSSIBLE_ACTIONS)
def gather_samples(grid, n_episodes=10000):
samples = []
for _ in range(n_episodes):
s = grid.reset()
samples.append(s)
while not grid.game_over():
a = np.random.choice(ALL_POSSIBLE_ACTIONS)
r = grid.move(a)
s = grid.current_state()
samples.append(s)
return samples
class Model:
def __init__(self, grid):
# fit the featurizer to data
samples = gather_samples(grid)
# self.featurizer = Nystroem()
self.featurizer = RBFSampler()
self.featurizer.fit(samples)
dims = self.featurizer.n_components
# initialize linear model weights
self.w = np.zeros(dims)
def predict(self, s):
x = self.featurizer.transform([s])[0]
return x @ self.w
def grad(self, s):
x = self.featurizer.transform([s])[0]
return x
if __name__ == '__main__':
# use the standard grid again (0 for every step) so that we can compare
# to iterative policy evaluation
grid = standard_grid()
# print rewards
print("rewards:")
print_values(grid.rewards, grid)
# state -> action
greedy_policy = {
(2, 0): 'U',
(1, 0): 'U',
(0, 0): 'R',
(0, 1): 'R',
(0, 2): 'R',
(1, 2): 'R',
(2, 1): 'R',
(2, 2): 'R',
(2, 3): 'U',
}
model = Model(grid)
mse_per_episode = []
# repeat until convergence
n_episodes = 10000
for it in range(n_episodes):
if (it + 1) % 100 == 0:
print(it + 1)
s = grid.reset()
Vs = model.predict(s)
n_steps = 0
episode_err = 0
while not grid.game_over():
a = epsilon_greedy(greedy_policy, s)
r = grid.move(a)
s2 = grid.current_state()
# get the target
if grid.is_terminal(s2):
target = r
else:
Vs2 = model.predict(s2)
target = r + GAMMA * Vs2
# update the model
g = model.grad(s)
err = target - Vs
model.w += ALPHA * err * g
# accumulate error
n_steps += 1
episode_err += err*err
# update state
s = s2
Vs = Vs2
mse = episode_err / n_steps
mse_per_episode.append(mse)
plt.plot(mse_per_episode)
plt.title("MSE per episode")
plt.show()
# obtain predicted values
V = {}
states = grid.all_states()
for s in states:
if s in grid.actions:
V[s] = model.predict(s)
else:
# terminal state or state we can't otherwise get to
V[s] = 0
print("values:")
print_values(V, grid)
print("policy:")
print_policy(greedy_policy, grid)