-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneural_network.py
192 lines (150 loc) · 6.79 KB
/
neural_network.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers, Sequential, optimizers
from constants import *
from enums.direction import *
import matplotlib.pyplot as plt
from envs.deep_snake_env import DeepSnakeEnv
class NeuralNetwork:
def __init__(self, env: DeepSnakeEnv, path=None) -> None:
self.env = env # import env
self.state_shape = env.observation_space.shape # the state space
self.action_shape = env.action_space.n # the action space
self.gamma = 0.9 # decay rate of past observations
self.alpha = 1e-4 # learning rate in the policy gradient
self.learning_rate = 0.01 # learning rate in deep learning
if not path:
self.model = self._create_model() # build model
else:
self.model = tf.keras.models.load_model(path) # import model
# record observations
self.states = []
self.gradients = []
self.rewards = []
self.probs = []
self.discounted_rewards = []
self.total_rewards = []
def _create_model(self):
''' builds the model using keras'''
model = Sequential()
# input shape is of observations
# model.add(layers.CategoryEncoding(num_tokens=4)),
model.add(layers.Dense(observation_shape,
input_shape=(observation_shape,), activation="relu"))
# add a relu layer
model.add(layers.Dense(observation_shape * 2, activation="relu"))
model.add(layers.Dense(observation_shape * 2, activation="relu"))
model.add(layers.Dense(observation_shape * 2, activation="relu"))
# output shape is according to the number of action
# The softmax function outputs a probability distribution over the actions
model.add(layers.Dense(3, activation="softmax"))
model.compile(loss="categorical_crossentropy",
optimizer=optimizers.Adam(lr=self.learning_rate))
model.build((None, ROWS_AMOUNT * COLS_AMOUNT))
print(model.summary())
return model
def hot_encode_action(self, action):
'''encoding the actions into a binary list'''
action_encoded = np.zeros(self.action_shape, np.float32)
action_encoded[action] = 1
return action_encoded
def remember(self, state, action, action_prob, reward):
'''stores observations'''
encoded_action = self.hot_encode_action(action)
self.gradients.append(encoded_action-action_prob)
self.states.append(state)
self.rewards.append(reward)
self.probs.append(action_prob)
def get_action(self, state):
'''samples the next action based on the policy probabilty distribution
of the actions'''
# transform state
state = state.reshape(1, -1)
# get action probably
action_probability_distribution = self.model.predict(
state, batch_size=1).flatten()
# norm action probability distribution
# sample action
action = np.random.choice(a=self.action_shape, size=1,
p=action_probability_distribution)
return action, action_probability_distribution
def get_discounted_rewards(self, rewards):
'''Use gamma to calculate the total reward discounting for rewards
Following - \gamma ^ t * Gt'''
discounted_rewards = []
cumulative_total_return = 0
# iterate the rewards backwards and and calc the total return
for reward in rewards[::-1]:
cumulative_total_return = (
cumulative_total_return*self.gamma)+reward
discounted_rewards.insert(0, cumulative_total_return)
# normalize discounted rewards
mean_rewards = np.mean(discounted_rewards)
std_rewards = np.std(discounted_rewards)
norm_discounted_rewards = (discounted_rewards -
mean_rewards)/(std_rewards+1e-7) # avoiding zero div
return norm_discounted_rewards
def update_policy(self):
'''Updates the policy network using the NN model.
This function is used after the MC sampling is done - following
\delta \theta = \alpha * gradient + log pi'''
# get X
states = np.array(self.states).reshape(-1, observation_shape)
# get Y
gradients = np.vstack(self.gradients)
rewards = np.vstack(self.rewards)
discounted_rewards = self.get_discounted_rewards(rewards)
gradients *= discounted_rewards
gradients = self.alpha*np.vstack([gradients])+self.probs
X = states
y = gradients
history = self.model.train_on_batch(X, y)
self.states, self.probs, self.gradients, self.rewards = [], [], [], []
return history
def train(self, episodes, rollout_n=1, render_n=50):
'''train the model
episodes - number of training iterations
rollout_n- number of episodes between policy update
render_n - number of episodes between env rendering '''
env = self.env
total_rewards = np.zeros(episodes)
total_steps = np.zeros(episodes)
for episode in range(episodes):
# each episode is a new game env
env.reset()
state = env.game.get_observation()
done = False
episode_reward = 0 # record episode reward
steps = 0
while not done and steps < MAX_TURNS:
# play an action and record the game state & reward per episode
steps += 1
action, prob = self.get_action(state)
next_state, reward, done, _ = env.step(action)
self.remember(state, action, prob, reward)
state = next_state
episode_reward += reward
# if episode%render_n==0: ## render env to visualize.
if RENDER_MODE == 'human':
env.render()
if done or steps == MAX_TURNS:
# update policy
if episode % rollout_n == 0:
print('Episode: %d, Reward: %d, Steps: %d' %
(episode, episode_reward, steps))
history = self.update_policy()
total_rewards[episode] = episode_reward
total_steps[episode] = steps
self.episodes = np.array(range(episodes)) + 1
self.total_rewards = total_rewards
self.total_steps = total_steps
def plot(self):
'''Plots the reward per episode'''
plt.plot(self.episodes, self.total_rewards)
plt.plot(self.episodes, self.total_steps, '-.')
plt.xlabel('Episodes')
plt.ylabel('Total Reward (-)')
plt.show()
def save_model(self, path):
'''Saves the model to the specified path'''
self.model.save(path)