-
Notifications
You must be signed in to change notification settings - Fork 10
/
stream_td.py
217 lines (191 loc) · 8.69 KB
/
stream_td.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
import argparse, os
import torch
import numpy as np
import torch.nn as nn
import gymnasium as gym
import torch.nn.functional as F
from optim import ObGD as Optimizer
from normalization_wrappers import NormalizeObservation, ScaleReward
from sparse_init import sparse_init
import matplotlib.pyplot as plt
import pandas as pd
class Trace(gym.Wrapper):
def __init__(self, shape=(), beta=0.99):
self.mean = np.zeros(shape, "float64")
self.beta = beta
self.count = 0
def update(self, x):
self.count += 1
self.mean = self.beta * self.mean + (1 - self.beta) * x
return self.mean / (1 - self.beta ** self.count)
def reset(self):
self.mean = np.zeros_like(self.mean)
self.count = 0
class ObservationTraces(gym.Wrapper, gym.utils.RecordConstructorArgs):
def __init__(self, env: gym.Env, beta: float = 0.999):
gym.utils.RecordConstructorArgs.__init__(self, beta=beta)
gym.Wrapper.__init__(self, env)
try:
self.num_envs = self.get_wrapper_attr("num_envs")
self.is_vector_env = self.get_wrapper_attr("is_vector_env")
except AttributeError:
self.num_envs = 1
self.is_vector_env = False
if self.is_vector_env:
self.trace = Trace(shape=self.single_observation_space.shape, beta=beta)
else:
self.trace = Trace(shape=self.observation_space.shape, beta=beta)
def step(self, action):
obs, rews, terminateds, truncateds, infos = self.env.step(action)
if self.is_vector_env:
obs = self.get_trace(obs)
else:
obs = self.get_trace(np.array([obs]))[0]
if terminateds or truncateds:
self.trace.reset()
return obs, rews, terminateds, truncateds, infos
def reset(self, **kwargs):
obs, info = self.env.reset(**kwargs)
if self.is_vector_env:
obs = self.get_trace(obs)
return obs, info
else:
obs = self.get_trace(np.array([obs]))[0]
return obs, info
def get_trace(self, obs):
return self.trace.update(obs)
class ETTEnvironment(gym.Env):
def __init__(self, dataset_path="ETTm2.csv"):
super(ETTEnvironment, self).__init__()
# download the dataset from https://github.com/zhouhaoyi/ETDataset
download_url = "https://raw.githubusercontent.com/zhouhaoyi/ETDataset/refs/heads/main/ETT-small/ETTm2.csv"
if not os.path.exists(dataset_path):
import urllib.request
urllib.request.urlretrieve(download_url, dataset_path)
self.current_step = 0
self.observation_space = gym.spaces.Box(low=-np.inf, high=np.inf, shape=(7,), dtype=np.float32)
self.spec = gym.envs.registration.EnvSpec("ETTEnvironment-v0")
self.df = self.process_data(dataset_path)
def process_data(self, dataset_path):
self.df = pd.read_csv(dataset_path)
self.df = self.df.drop(columns=["date"])
self.df = self.df.astype(np.float64)
self.df["original_cumulant"] = self.df.iloc[:, -1]
self.scaling_value = self.df.iloc[:, -1].max() - self.df.iloc[:, -1].min()
self.add_value = self.df.iloc[:, -1].min()
self.df.iloc[:, -1] = (self.df.iloc[:, -1] - self.add_value) / self.scaling_value
return self.df
def reset(self, seed=None, options={}):
self.current_step = 0
self.max_steps = len(self.df) - 1.0
return self._get_observation(), {}
def step(self, action=None):
self.current_step += 1
observation = self._get_observation()
reward = self._calculate_reward()
done = self.current_step >= self.max_steps
return observation, reward, done, 0, {}
def _get_observation(self):
return self.df.iloc[self.current_step, :-1].values
def _calculate_reward(self):
return self.df.iloc[self.current_step+1, -1]
def close(self):
os.remove("ETTm2.csv")
def initialize_weights(m):
if isinstance(m, nn.Linear):
sparse_init(m.weight, sparsity=0.9)
m.bias.data.fill_(0.0)
class StreamTD(nn.Module):
def __init__(self, n_obs=7, hidden_size=128, lr=1.0, gamma=0.9, lamda=0.9, kappa_value=2.0):
super(StreamTD, self).__init__()
self.gamma = gamma
self.fc1_v = nn.Linear(n_obs, hidden_size)
self.hidden_v = nn.Linear(hidden_size, hidden_size)
self.fc_v = nn.Linear(hidden_size, 1)
self.apply(initialize_weights)
self.optimizer = Optimizer(list(self.parameters()), lr=lr, gamma=gamma, lamda=lamda, kappa=kappa_value)
def v(self, x):
x = self.fc1_v(x)
x = F.layer_norm(x, x.size())
x = F.leaky_relu(x)
x = self.hidden_v(x)
x = F.layer_norm(x, x.size())
x = F.leaky_relu(x)
return self.fc_v(x)
def predict(self, s):
s = torch.tensor(np.array(s), dtype=torch.float)
return self.v(s).item()
def update_params(self, s, r, s_prime, done, overshooting_info=False):
done_mask = 0 if done else 1
s, r, s_prime, done_mask = torch.tensor(np.array(s), dtype=torch.float), torch.tensor(np.array(r)),\
torch.tensor(np.array(s_prime), dtype=torch.float), torch.tensor(np.array(done_mask), dtype=torch.float)
v_s = self.v(s)
td_target = r + self.gamma * self.v(s_prime) * done_mask
delta = td_target - v_s
value_output = -v_s
self.optimizer.zero_grad()
value_output.backward()
self.optimizer.step(delta.item(), reset=done)
if overshooting_info:
td_target = r + self.gamma * self.v(s_prime) * done_mask
delta_bar = td_target - self.v(s)
if torch.sign(delta_bar * delta).item() == -1:
print("Overshooting Detected!")
def main(seed, lr, gamma, lamda, total_steps, kappa_value, debug, overshooting_info):
torch.manual_seed(seed); np.random.seed(seed)
env = ETTEnvironment()
env = gym.wrappers.RecordEpisodeStatistics(env)
env = ObservationTraces(env, beta=0.9999)
env = NormalizeObservation(env)
env = ScaleReward(env, gamma=gamma)
agent = StreamTD(n_obs=env.observation_space.shape[0], lr=lr, gamma=gamma, lamda=lamda, kappa_value=kappa_value)
if debug:
print("seed: {}".format(seed), "env: {}".format(env.spec.id))
s, _ = env.reset()
episodic_return = 0
errors, predictions, actual_returns = [], [], []
for t in range(1, total_steps+1):
s_prime, c, terminated, _, info = env.step(None)
agent.update_params(s, c, s_prime, terminated, overshooting_info=overshooting_info)
s = s_prime
episodic_return = gamma * episodic_return + c * np.sqrt(env.reward_stats.var+ 1e-8).squeeze()
prediction = agent.predict(s) * np.sqrt(env.reward_stats.var + 1e-8).squeeze()
error = (episodic_return - prediction) ** 2
errors.append(error)
predictions.append(prediction)
actual_returns.append(episodic_return)
if terminated:
s, _ = env.reset()
break
if debug and t % 1000 == 0:
print("Episodic Return: {}, Prediction: {}, Time Step {}, Error {}".format(episodic_return, prediction, t, error))
env.close()
plt.figure(figsize=(12, 4))
plt.plot(actual_returns, label="Actual Return", linewidth=3.0, color="tab:green")
plt.plot(predictions, label="Prediction", linewidth=3.0, color="tab:blue")
plt.xlim([0, 5000])
plt.xlabel("Time Step", fontsize=20)
plt.ylabel("Normalized Oil Temp.", fontsize=20)
plt.legend()
plt.savefig("td_ettm2_start.pdf", bbox_inches='tight')
plt.figure(figsize=(12, 4))
plt.plot(actual_returns, label="Actual Return", linewidth=3.0, color="tab:green")
plt.plot(predictions, label="Prediction", linewidth=3.0, color="tab:blue")
plt.xlim([total_steps-5000, total_steps])
plt.ylim([35, 85])
plt.xlabel("Time Step", fontsize=20)
plt.ylabel("Normalized Oil Temp.", fontsize=20)
plt.legend()
plt.savefig("td_ettm2_end.pdf", bbox_inches='tight')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Stream TD(λ)')
parser.add_argument('--seed', type=int, default=0)
parser.add_argument('--lr', type=float, default=1.0)
parser.add_argument('--gamma', type=float, default=0.99)
parser.add_argument('--lamda', type=float, default=0.8)
parser.add_argument('--kappa_value', type=float, default=2.0)
parser.add_argument('--total_steps', type=int, default=68_000)
parser.add_argument('--debug', action='store_true')
parser.add_argument('--overshooting_info', action='store_true')
args = parser.parse_args()
main(args.seed, args.lr, args.gamma, args.lamda, args.total_steps, args.kappa_value, args.debug, args.overshooting_info)