-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathtrain.py
136 lines (111 loc) · 4.69 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import copy
import pylab
import random
import numpy as np
from environment import Env
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
# 상태가 입력, 각 행동의 확률이 출력인 인공신경망 생성
class REINFORCE(tf.keras.Model):
def __init__(self, action_size):
super(REINFORCE, self).__init__()
self.fc1 = Dense(24, activation='relu')
self.fc2 = Dense(24, activation='relu')
self.fc_out = Dense(action_size, activation='softmax')
def call(self, x):
x = self.fc1(x)
x = self.fc2(x)
policy = self.fc_out(x)
return policy
# 그리드월드 예제에서의 REINFORCE 에이전트
class REINFORCEAgent:
def __init__(self, state_size, action_size):
# 상태의 크기와 행동의 크기 정의
self.state_size = state_size
self.action_size = action_size
# REINFORCE 하이퍼 파라메터
self.discount_factor = 0.99
self.learning_rate = 0.001
self.model = REINFORCE(self.action_size)
self.optimizer = Adam(lr=self.learning_rate)
self.states, self.actions, self.rewards = [], [], []
# 정책신경망으로 행동 선택
def get_action(self, state):
policy = self.model(state)[0]
policy = np.array(policy)
return np.random.choice(self.action_size, 1, p=policy)[0]
# 반환값 계산
def discount_rewards(self, rewards):
discounted_rewards = np.zeros_like(rewards)
running_add = 0
for t in reversed(range(0, len(rewards))):
running_add = running_add * self.discount_factor + rewards[t]
discounted_rewards[t] = running_add
return discounted_rewards
# 한 에피소드 동안의 상태, 행동, 보상을 저장
def append_sample(self, state, action, reward):
self.states.append(state[0])
self.rewards.append(reward)
act = np.zeros(self.action_size)
act[action] = 1
self.actions.append(act)
# 정책신경망 업데이트
def train_model(self):
discounted_rewards = np.float32(self.discount_rewards(self.rewards))
discounted_rewards -= np.mean(discounted_rewards)
discounted_rewards /= np.std(discounted_rewards)
# 크로스 엔트로피 오류함수 계산
model_params = self.model.trainable_variables
with tf.GradientTape() as tape:
tape.watch(model_params)
policies = self.model(np.array(self.states))
actions = np.array(self.actions)
action_prob = tf.reduce_sum(actions * policies, axis=1)
cross_entropy = - tf.math.log(action_prob + 1e-5)
loss = tf.reduce_sum(cross_entropy * discounted_rewards)
entropy = - policies * tf.math.log(policies)
# 오류함수를 줄이는 방향으로 모델 업데이트
grads = tape.gradient(loss, model_params)
self.optimizer.apply_gradients(zip(grads, model_params))
self.states, self.actions, self.rewards = [], [], []
return np.mean(entropy)
if __name__ == "__main__":
# 환경과 에이전트 생성
env = Env(render_speed=0.01)
state_size = 15
action_space = [0, 1, 2, 3, 4]
action_size = len(action_space)
agent = REINFORCEAgent(state_size, action_size)
scores, episodes = [], []
EPISODES = 200
for e in range(EPISODES):
done = False
score = 0
# env 초기화
state = env.reset()
state = np.reshape(state, [1, state_size])
while not done:
# 현재 상태에 대한 행동 선택
action = agent.get_action(state)
# 선택한 행동으로 환경에서 한 타임스텝 진행 후 샘플 수집
next_state, reward, done = env.step(action)
next_state = np.reshape(next_state, [1, state_size])
agent.append_sample(state, action, reward)
score += reward
state = next_state
if done:
# 에피소드마다 정책신경망 업데이트
entropy = agent.train_model()
# 에피소드마다 학습 결과 출력
print("episode: {:3d} | score: {:3d} | entropy: {:.3f}".format(
e, score, entropy))
scores.append(score)
episodes.append(e)
pylab.plot(episodes, scores, 'b')
pylab.xlabel("episode")
pylab.ylabel("score")
pylab.savefig("./save_graph/graph.png")
# 100 에피소드마다 모델 저장
if e % 100 == 0:
agent.model.save_weights('save_model/model', save_format='tf')