-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstream_ac_discrete.py
155 lines (137 loc) · 6.79 KB
/
stream_ac_discrete.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
import os, pickle, argparse
import torch
import numpy as np
import torch.nn as nn
import gymnasium as gym
import torch.nn.functional as F
from torch.distributions import Categorical
from optim import ObGD as Optimizer
from time_wrapper import AddTimeInfo
from normalization_wrappers import NormalizeObservation, ScaleReward
from sparse_init import sparse_init
def initialize_weights(m):
if isinstance(m, nn.Linear):
sparse_init(m.weight, sparsity=0.9)
m.bias.data.fill_(0.0)
class Actor(nn.Module):
def __init__(self, n_obs=11, n_actions=3, hidden_size=128):
super(Actor, self).__init__()
self.fc_layer = nn.Linear(n_obs, hidden_size)
self.hidden_layer = nn.Linear(hidden_size, hidden_size)
self.fc_pi = nn.Linear(hidden_size, n_actions)
self.apply(initialize_weights)
def forward(self, x):
x = self.fc_layer(x)
x = F.layer_norm(x, x.size())
x = F.leaky_relu(x)
x = self.hidden_layer(x)
x = F.layer_norm(x, x.size())
x = F.leaky_relu(x)
pref = self.fc_pi(x)
return pref
class Critic(nn.Module):
def __init__(self, n_obs=11, hidden_size=128):
super(Critic, self).__init__()
self.fc_layer = nn.Linear(n_obs, hidden_size)
self.hidden_layer = nn.Linear(hidden_size, hidden_size)
self.linear_layer = nn.Linear(hidden_size, 1)
self.apply(initialize_weights)
def forward(self, x):
x = self.fc_layer(x)
x = F.layer_norm(x, x.size())
x = F.leaky_relu(x)
x = self.hidden_layer(x)
x = F.layer_norm(x, x.size())
x = F.leaky_relu(x)
return self.linear_layer(x)
class StreamAC(nn.Module):
def __init__(self, n_obs=11, n_actions=3, hidden_size=32, lr=1.0, gamma=0.99, lamda=0.8, kappa_policy=3.0, kappa_value=2.0):
super(StreamAC, self).__init__()
self.gamma = gamma
self.policy_net = Actor(n_obs=n_obs, n_actions=n_actions, hidden_size=hidden_size)
self.value_net = Critic(n_obs=n_obs, hidden_size=hidden_size)
self.optimizer_policy = Optimizer(self.policy_net.parameters(), lr=lr, gamma=gamma, lamda=lamda, kappa=kappa_policy)
self.optimizer_value = Optimizer(self.value_net.parameters(), lr=lr, gamma=gamma, lamda=lamda, kappa=kappa_value)
def pi(self, x):
preferences = self.policy_net(x)
probs = F.softmax(preferences, dim=-1)
return probs
def v(self, x):
return self.value_net(x)
def sample_action(self, s):
x = torch.from_numpy(s).float()
probs = self.pi(x)
dist = Categorical(probs)
return dist.sample().numpy()
def update_params(self, s, a, r, s_prime, done, entropy_coeff, overshooting_info=False):
done_mask = 0 if done else 1
s, a, r, s_prime, done_mask = torch.tensor(np.array(s), dtype=torch.float), torch.tensor(np.array(a)), \
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, v_prime = self.v(s), self.v(s_prime)
td_target = r + self.gamma * v_prime * done_mask
delta = td_target - v_s
probs = self.pi(s)
dist = Categorical(probs)
log_prob_pi = -(dist.log_prob(a)).sum()
value_output = -v_s
entropy_pi = -entropy_coeff * dist.entropy().sum() * torch.sign(delta).item()
self.optimizer_value.zero_grad()
self.optimizer_policy.zero_grad()
value_output.backward()
(log_prob_pi + entropy_pi).backward()
self.optimizer_policy.step(delta.item(), reset=done)
self.optimizer_value.step(delta.item(), reset=done)
if overshooting_info:
v_s, v_prime = self.v(s), self.v(s_prime)
td_target = r + self.gamma * v_prime * done_mask
delta_bar = td_target - v_s
if torch.sign(delta_bar * delta).item() == -1:
print("Overshooting Detected!")
def main(env_name, seed, lr, gamma, lamda, total_steps, entropy_coeff, kappa_policy, kappa_value, debug, overshooting_info, render=False):
torch.manual_seed(seed); np.random.seed(seed)
env = gym.make(env_name, render_mode='human', max_episode_steps=10_000) if render else gym.make(env_name, max_episode_steps=10_000)
env = gym.wrappers.FlattenObservation(env)
env = gym.wrappers.RecordEpisodeStatistics(env)
env = ScaleReward(env, gamma=gamma)
env = NormalizeObservation(env)
env = AddTimeInfo(env)
agent = StreamAC(n_obs=env.observation_space.shape[0], n_actions=env.action_space.n, lr=lr, gamma=gamma, lamda=lamda, kappa_policy=kappa_policy, kappa_value=kappa_value)
if debug:
print("seed: {}".format(seed), "env: {}".format(env.spec.id))
returns, term_time_steps = [], []
s, _ = env.reset(seed=seed)
for t in range(1, total_steps+1):
a = agent.sample_action(s)
s_prime, r, terminated, truncated, info = env.step(a)
agent.update_params(s, a, r, s_prime, terminated or truncated, entropy_coeff, overshooting_info)
s = s_prime
if terminated or truncated:
if debug:
print("Episodic Return: {}, Time Step {}".format(info['episode']['r'][0], t))
returns.append(info['episode']['r'][0])
term_time_steps.append(t)
terminated, truncated = False, False
s, _ = env.reset()
env.close()
save_dir = "data_stream_ac_{}_lr{}_gamma{}_lamda{}_entropy_coeff{}".format(env.spec.id, lr, gamma, lamda, entropy_coeff)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
with open(os.path.join(save_dir, "seed_{}.pkl".format(seed)), "wb") as f:
pickle.dump((returns, term_time_steps, env_name), f)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Stream AC(λ)')
parser.add_argument('--env_name', type=str, default='CartPole-v1')
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('--total_steps', type=int, default=500_000)
parser.add_argument('--entropy_coeff', type=float, default=0.01)
parser.add_argument('--kappa_policy', type=float, default=3.0)
parser.add_argument('--kappa_value', type=float, default=2.0)
parser.add_argument('--debug', action='store_true')
parser.add_argument('--overshooting_info', action='store_true')
parser.add_argument('--render', action='store_true')
args = parser.parse_args()
main(args.env_name, args.seed, args.lr, args.gamma, args.lamda, args.total_steps, args.entropy_coeff, args.kappa_policy, args.kappa_value, args.debug, args.overshooting_info, args.render)