-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlegacy_code.py
47 lines (39 loc) · 1.75 KB
/
legacy_code.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
# Tensorflow/Keras Imports
from keras import Sequential
from keras.layers import Dense, Flatten, Convolution2D
from keras.optimizers import Adam
# Reinforcement learning imports
from rl.agents import DQNAgent
from rl.memory import SequentialMemory
from rl.policy import LinearAnnealedPolicy, EpsGreedyQPolicy
# Function to build a keras NeuralNetwork model for DeepQLearning
def build_model(height, width, channels, actions):
model = Sequential()
# Setup convolutional layers
model.add(Convolution2D(32, (8,8), strides=(4,4), activation="relu", input_shape=(3, height, width, channels)))
model.add(Convolution2D(64, (4,4), strides=(2,2), activation="relu"))
model.add(Convolution2D(64, (3,3), activation="relu"))
model.add(Flatten())
# Set up dense layers
model.add(Dense(512, activation='relu'))
model.add(Dense(256, activation='relu'))
# Set up output layer
model.add(Dense(actions, activation='linear'))
return model
# Function to build a DeepQ Learning agent based on the keras AI model
def build_agent(model, actions):
policy = LinearAnnealedPolicy(EpsGreedyQPolicy(), attr='eps', value_max=1., value_min=.1, value_test=.2, nb_steps=10000)
memory = SequentialMemory(limit=1000, window_length=3)
dqn = DQNAgent(model=model, memory=memory, policy=policy,
enable_dueling_network=True, dueling_type='avg',
nb_actions=actions, nb_steps_warmup=1000)
return dqn
def old_rl_model():
# Build the model and the agent
# NOTE: OLD way
# model = build_model(height, width, channels, actions)
# agent = build_agent(model, actions)
# # Compile model and run training
# agent.compile(Adam(lr=1e-4))
# agent.fit(env, nb_steps=1000, visualize=True, verbose=2)
pass