-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppo_lstm.py
275 lines (224 loc) · 8.98 KB
/
ppo_lstm.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# PPO-LSTM
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Categorical
from tensorboardX import SummaryWriter
from init import weight_init
# Hyperparameters
LEARNING_RATE = 0.00001
GAMMA = 0.98
LMBDA = 0.95
EPS_CLIP = 0.1
K_EPOCH = 5
N_EPI = 1000
N_BATCH = 20
T_HORIZON = 128
FEATURE_SIZE = 26
def save_model(model, save_dir, epoch_i, batch_i, desc=''):
if not os.path.exists(save_dir):
os.makedirs(save_dir)
save_path = os.path.join(save_dir,
'{}{}_{}_ckpt.tar'.format(desc,
epoch_i,
batch_i))
try:
state_dict = model.module.state_dict()
except AttributeError:
state_dict = model.state_dict()
torch.save(state_dict, save_path)
return save_path
class PPO(nn.Module):
def __init__(self, writer=None):
super(PPO, self).__init__()
self.data = []
self.fc1 = nn.Linear(FEATURE_SIZE, 64)
self.ln1 = nn.LayerNorm(64)
self.lstm = nn.LSTM(64, 32)
self.ln2 = nn.LayerNorm(32)
self.fc_pi = nn.Linear(32, 3)
self.fc_pi_amount = nn.Linear(32, 3)
self.fc_v = nn.Linear(32, 1)
self.optimizer = optim.Adam(self.parameters(), lr=LEARNING_RATE)
self.writer = writer
self.global_i = 0
def pi(self, x, hidden):
x = x.view(-1, FEATURE_SIZE)
x = F.relu(self.ln1(self.fc1(x)))
x = x.view(-1, 1, 64)
feature, lstm_hidden = self.lstm(x, hidden)
feature = self.ln2(feature)
x = self.fc_pi(feature)
action = F.softmax(x, dim=2)
amount = self.fc_pi_amount(feature)
amount = torch.sigmoid(amount)
return action, amount, lstm_hidden
def v(self, x, hidden):
x = F.relu(self.ln1(self.fc1(x)))
x = x.view(-1, 1, 64)
x, lstm_hidden = self.lstm(x, hidden)
v = self.fc_v(x)
return v
def put_data(self, transition):
self.data.append(transition)
def make_batch(self):
(s_lst, a_lst, r_lst,
s_prime_lst, prob_a_lst, amount_lst,
hidden_lst, done_lst) = [], [], [], [], [], [], [], []
for transition in self.data:
s, a, r, s_prime, prob_a, amount, hidden, done = transition
s_lst.append(s)
a_lst.append([a])
r_lst.append([r])
s_prime_lst.append(s_prime)
prob_a_lst.append([prob_a])
amount_lst.append([amount])
hidden_lst.append(hidden)
done_mask = 0 if done else 1
done_lst.append([done_mask])
(s, a, r,
s_prime, done_mask, prob_a,
amount) = (torch.tensor(s_lst, dtype=torch.float).cuda(),
torch.tensor(a_lst).cuda(),
torch.tensor(r_lst).cuda(),
torch.tensor(s_prime_lst, dtype=torch.float).cuda(),
torch.tensor(done_lst, dtype=torch.float).cuda(),
torch.tensor(prob_a_lst).cuda(),
torch.tensor(amount_lst).cuda())
self.data = []
return s, a, r, s_prime, done_mask, prob_a, amount, hidden_lst[0]
def train_net(self):
s, a, r, s_prime, done_mask, prob_a, amount, (h1, h2) = self.make_batch()
first_hidden = (h1.detach().cuda(), h2.detach().cuda())
for i in range(K_EPOCH):
v_prime = self.v(s_prime, first_hidden).squeeze(1)
td_target = r + GAMMA * v_prime * done_mask
v_s = self.v(s, first_hidden).squeeze(1)
delta = td_target - v_s
delta = delta.detach().cpu().numpy()
advantage_lst = []
advantage = 0.0
for item in delta[::-1]:
advantage = GAMMA * LMBDA * advantage + item[0]
advantage_lst.append([advantage])
advantage_lst.reverse()
advantage = torch.tensor(advantage_lst, dtype=torch.float).cuda()
pi, amount_a, _ = self.pi(s, first_hidden)
pi_a = pi.squeeze(1).gather(1, a)
amount_a = amount_a.squeeze(1).gather(1, a)
# a/b == log(exp(a)-exp(b))
ratio1 = torch.exp(torch.log(pi_a) - torch.log(prob_a))
ratio2 = torch.exp(torch.log(amount_a) - torch.log(amount)) # check
ratio = ratio1 + ratio2
surr1 = ratio * advantage
surr2 = torch.clamp(ratio, 1-EPS_CLIP, 1+EPS_CLIP) * advantage
loss = -torch.min(surr1, surr2) + \
F.smooth_l1_loss(v_s, td_target.detach())
if self.writer:
writer.add_scalar('loss', loss.mean().item(), self.global_i)
writer.add_scalar('delta', delta.mean().item(), self.global_i)
writer.add_scalar('action_ratio', ratio1.mean().item(), self.global_i)
writer.add_scalar('amount_ratio', ratio2.mean().item(), self.global_i)
self.optimizer.zero_grad()
loss.mean().backward(retain_graph=True)
self.optimizer.step()
self.global_i += 1
if __name__ == '__main__':
from stable_baselines.common.vec_env import DummyVecEnv
from env.CryptoTradingEnv import CryptoTradingEnv
import pandas as pd
# Load data
df = pd.read_csv('./data/upbit/upbit-btckrw-240m.csv', index_col=False)
df = df.sort_values('timestamp')
total = len(df)
train_ratio = 0.6
n_train = int(total * train_ratio)
train_df = df[:n_train]
test_df = df[n_train:].reset_index()
# Make Env
train_env = DummyVecEnv([lambda: CryptoTradingEnv(train_df)])
test_env = DummyVecEnv([lambda: CryptoTradingEnv(test_df)])
writer = SummaryWriter("./tensorboard/20190616_1/mlp_lstm_ppo")
# for i, row in train_df.iterrows():
# writer.add_scalars('train_price', {'open': row['open'],
# 'close': row['close'],
# 'low': row['low'],
# 'high': row['high']}, i)
# for i, row in test_df.iterrows():
# writer.add_scalars('test_price', {'open': row['open'],
# 'close': row['close'],
# 'low': row['low'],
# 'high': row['high']}, i)
model = PPO(writer)
model = model.cuda()
model.apply(weight_init)
score = 0.0
print_interval = 1
for n_epi in range(N_EPI):
hidden = (torch.zeros([1, 1, 32], dtype=torch.float).cuda(),
torch.zeros([1, 1, 32], dtype=torch.float).cuda())
s = train_env.reset()
done = False
for i in range(N_BATCH):
for t in range(T_HORIZON):
s_tensor = torch.from_numpy(s).float().cuda()
prob, amount, hidden = model.pi(s_tensor, hidden)
prob = prob.view(-1)
amount = amount.view(-1)
m = Categorical(prob)
action = m.sample().item()
amount = amount.detach().cpu().numpy()[action]
a = [(action, amount)]
s_prime, r, done, info = train_env.step(a)
model.put_data(
(s, action, r, s_prime, prob[action].item(), amount, hidden, done))
s = s_prime
score += r
if done:
break
model.train_net()
if n_epi % print_interval == 0 and n_epi != 0:
print("# of episode :{}, avg score : {:.1f}".format(
n_epi, (score/print_interval).item()))
score = 0.0
# train_env.close()
model.eval()
# validation
obs = train_env.reset()
train_env.current_step = 0
hidden = (torch.zeros([1, 1, 32], dtype=torch.float).cuda(),
torch.zeros([1, 1, 32], dtype=torch.float).cuda())
while True:
obs = torch.from_numpy(obs).cuda()
prob, amount, hidden = model.pi(s_tensor, hidden)
prob = prob.view(-1)
amount = amount.view(-1)
m = Categorical(prob)
action = m.sample().item()
amount = amount.detach().cpu().numpy()[action]
a = [(action, amount)]
obs, r, done, info = train_env.step(a)
if done:
break
train_env.render()
obs = test_env.reset()
test_env.current_step = 0
hidden = (torch.zeros([1, 1, 32], dtype=torch.float).cuda(),
torch.zeros([1, 1, 32], dtype=torch.float).cuda())
while True:
obs = torch.from_numpy(obs).cuda()
prob, amount, hidden = model.pi(s_tensor, hidden)
prob = prob.view(-1)
amount = amount.view(-1)
m = Categorical(prob)
action = m.sample().item()
amount = amount.detach().cpu().numpy()[action]
a = [(action, amount)]
obs, r, done, info = test_env.step(a)
if done:
break
test_env.render()
torch.save(model, "./ckpts/mlp_lstm.torch")
save_model("./ckpts/mlp_lstm.torch")