-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_model.py
34 lines (28 loc) · 958 Bytes
/
test_model.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
import gym
from actor_model import Actor
import tensorflow as tf
import tensorflow.keras.backend as K
import numpy as np
import sys
model_weight_file = sys.argv[1]
sess = tf.Session()
K.set_session(sess)
# Use env = gym.make('CartPole-v1').unwrapped
# to remove the max_episode_length limit of 500. Episode will run till Pole falls.
env = gym.make('CartPole-v1')
action_dim = env.action_space.n
observation_dim = env.observation_space.shape
# Creating actor model and setting it's weights
actor = Actor(sess, action_dim, observation_dim)
actor.model.load_weights(model_weight_file)
episode_reward = 0
TOTAL_EPISODES = 10
for _ in range(TOTAL_EPISODES):
state = env.reset()
done = False
while not done:
env.render()
next_state, reward, done, _ = env.step(np.argmax(actor.model.predict(np.expand_dims(state, axis=0))))
state = next_state
episode_reward += reward
print('Average Reward:', episode_reward / 10)