-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tensorforce_dqn_agent.py
63 lines (51 loc) · 1.68 KB
/
test_tensorforce_dqn_agent.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
from tensorforce import Agent, Environment
from tensorforce.agents import TensorforceAgent
import gym
# Pre-defined or custom environment
environment = Environment.create(
environment='gym', level='Acrobot', max_episode_timesteps=500
)
# Instantiate a Tensorforce agent
agent = Agent.create(
agent='tensorforce',
environment=environment, # alternatively: states, actions, (max_episode_timesteps)
memory=50000,
update=dict(unit='timesteps', batch_size=64),
optimizer=dict(type='adam', learning_rate=3e-4),
policy=dict(network='auto'),
objective='policy_gradient',
reward_estimation=dict(horizon=20)
)
# Train for 300 episodes
total_reward = 0
for i in range(300):
# Initialize episode
states = environment.reset()
terminal = False
while not terminal:
# Episode timestep
actions = agent.act(states=states)
states, terminal, reward = environment.execute(actions=actions)
agent.observe(terminal=terminal, reward=reward)
total_reward += reward
if i % 100 == 0:
print('episode:', i, "total reward:", total_reward/100)
total_reward=0
agent.save(directory='./models/', filename='tensorforce', format='hdf5')
print("trained for 300 episodes")
agent.close()
environment.close()
loaded_agent = TensorforceAgent.load(directory='./models/', filename='tensorforce', format='hdf5')
env = gym.make('CartPole-v1')
state = env.reset()
done = False
total_reward = 0
while done == False:
env.render()
action = loaded_agent.act(state, independent=True)
print(state, action)
state, reward, done, info = env.step(action)
total_reward+=1
print(env, total_reward)
loaded_agent.close()
env.close()