forked from keon/3-min-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcartpole_dqn.py
115 lines (89 loc) · 3.33 KB
/
cartpole_dqn.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
# coding: utf-8
# # 카트폴 게임 마스터하기
import gym
import random
import math
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from collections import deque
import matplotlib.pyplot as plt
# ### 하이퍼파라미터
# 하이퍼파라미터
EPISODES = 50 # 애피소드 반복횟수
EPS_START = 0.9 # 학습 시작시 에이전트가 무작위로 행동할 확률
EPS_END = 0.05 # 학습 막바지에 에이전트가 무작위로 행동할 확률
EPS_DECAY = 200 # 학습 진행시 에이전트가 무작위로 행동할 확률을 감소시키는 값
GAMMA = 0.8 # 할인계수
LR = 0.001 # 학습률
BATCH_SIZE = 64 # 배치 크기
# ## DQN 에이전트
class DQNAgent:
def __init__(self):
self.model = nn.Sequential(
nn.Linear(4, 256),
nn.ReLU(),
nn.Linear(256, 2)
)
self.optimizer = optim.Adam(self.model.parameters(), LR)
self.steps_done = 0
self.memory = deque(maxlen=10000)
def memorize(self, state, action, reward, next_state):
self.memory.append((state,
action,
torch.FloatTensor([reward]),
torch.FloatTensor([next_state])))
def act(self, state):
eps_threshold = EPS_END + (EPS_START - EPS_END) * math.exp(-1. * self.steps_done / EPS_DECAY)
self.steps_done += 1
if random.random() > eps_threshold:
return self.model(state).data.max(1)[1].view(1, 1)
else:
return torch.LongTensor([[random.randrange(2)]])
def learn(self):
if len(self.memory) < BATCH_SIZE:
return
batch = random.sample(self.memory, BATCH_SIZE)
states, actions, rewards, next_states = zip(*batch)
states = torch.cat(states)
actions = torch.cat(actions)
rewards = torch.cat(rewards)
next_states = torch.cat(next_states)
current_q = self.model(states).gather(1, actions)
max_next_q = self.model(next_states).detach().max(1)[0]
expected_q = rewards + (GAMMA * max_next_q)
loss = F.mse_loss(current_q.squeeze(), expected_q)
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
# ## 학습 준비하기
# `gym`을 이용하여 `CartPole-v0`환경을 준비하고 앞서 만들어둔 DQNAgent를 agent로 인스턴스화 합니다.
# 자, 이제 `agent` 객체를 이용하여 `CartPole-v0` 환경과 상호작용을 통해 게임을 배우도록 하겠습니다.
env = gym.make('CartPole-v0')
agent = DQNAgent()
score_history = []
# ## 학습 시작
for e in range(1, EPISODES+1):
state = env.reset()
steps = 0
while True:
env.render()
state = torch.FloatTensor([state])
action = agent.act(state)
next_state, reward, done, _ = env.step(action.item())
# 게임이 끝났을 경우 마이너스 보상주기
if done:
reward = -1
agent.memorize(state, action, reward, next_state)
agent.learn()
state = next_state
steps += 1
if done:
print("에피소드:{0} 점수: {1}".format(e, steps))
score_history.append(steps)
break
plt.plot(score_history)
plt.ylabel('score')
plt.show()