-
Notifications
You must be signed in to change notification settings - Fork 5
/
RLtoMP4.py
69 lines (58 loc) · 2.42 KB
/
RLtoMP4.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
import gym
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
def Predict():
env = gym.make("CartPole-v0")
trainingX, trainingY = GetData(env)
model = CreateModel()
model.fit(trainingX, trainingY, epochs=5)
from gym import wrappers # Output MP4
env = wrappers.Monitor(env, '/tmp/cartpole-experiment-0') # Output MP4
scores = []
for _ in range(50): #trials
observation = env.reset()
score = 0
for step in range(500): #sim_steps
env.render() # Output MP4
action = np.argmax(model.predict(observation.reshape(1,4)))
observation, reward, done, _ = env.step(action)
score += reward
if done:
break
scores.append(score)
print(np.mean(scores))
def GetData(env):
trainingX, trainingY = [], []
scores = []
for i_episode in range(10000): #trials
observation = env.reset()
score = 0
training_sampleX, training_sampleY = [], []
for t in range(500): #sim_steps
action = env.action_space.sample()
one_hot_action = np.zeros(2)
one_hot_action[action] = 1
training_sampleX.append(observation)
training_sampleY.append(one_hot_action)
observation, reward, done, info = env.step(action)
score += reward
if done:
break
if score > 50:
scores.append(score)
trainingX += training_sampleX
trainingY += training_sampleY
trainingX, trainingY = np.array(trainingX), np.array(trainingY)
print(np.mean(scores))
return trainingX, trainingY
def CreateModel():
model = Sequential()
model.add(Dense(2, input_shape=(4,), activation="softmax"))
model.compile(
loss="categorical_crossentropy",
optimizer="adam",
metrics=["accuracy"])
return model
if __name__ == "__main__":
Predict()