-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
79 lines (56 loc) · 1.62 KB
/
train.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
import gymnasium as gym
from agent import Agent
import matplotlib.pyplot as plt
# env = gym.make("LunarLander-v2", render_mode="human")
env = gym.make("LunarLander-v3")
agent = Agent(
state_dim=env.observation_space.shape[0],
hidden_dim=256,
action_dim=env.action_space.n,
device='cpu',
)
# pretrain
# agent.load()
reward_per_step = []
reward_per_episode = []
entropy_step = []
value_step = []
for episode_i in range(500):
state, info = env.reset()
episode_return = 0
done = False
while not done:
action = agent.take_action(state)
next_state, reward, terminated, truncated, info = env.step(action)
if terminated or truncated:
done = True
agent.buffer.states.append(state)
agent.buffer.actions.append(action)
agent.buffer.rewards.append(reward)
agent.buffer.next_states.append(next_state)
agent.buffer.dones.append(done)
state = next_state
episode_return += reward
reward_per_step.append(reward)
entropy_step.append(agent.entropy)
value_step.append(agent.value)
print(f'{episode_i=} {episode_return=}')
reward_per_episode.append(episode_return)
agent.update()
if episode_i % 100 == 0:
agent.save()
agent.save()
plt.subplot(2, 2, 1)
plt.plot(reward_per_episode)
plt.title("reward_per_episode")
plt.subplot(2, 2, 2)
plt.plot(reward_per_step)
plt.title("reward_per_step")
plt.subplot(2, 2, 3)
plt.plot(entropy_step)
plt.title("entropy")
plt.subplot(2, 2, 4)
plt.plot(value_step)
plt.title("value")
plt.show()
env.close()