-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontinuous.py
More file actions
145 lines (113 loc) · 4.68 KB
/
continuous.py
File metadata and controls
145 lines (113 loc) · 4.68 KB
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
import gym
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
class LinearNN(nn.Module):
def __init__(self, input_dim, output_dim):
super().__init__()
self.fc1 = nn.Linear(input_dim, 15)
self.fc2 = nn.Linear(15, output_dim)
self.last_x = torch.tensor([], dtype = torch.float32)
self.last_y = None
def forward(self, x):
if not self.training and torch.equal(self.last_x, x.cpu().float()):
return self.last_y
self.last_x = x.cpu().float()
self.last_y = self.fc2(torch.relu(self.fc1(x.float())))
return self.last_y
def initialize_zero(self):
self.fc1.weight.data.fill_(.0)
self.fc2.weight.data.fill_(.0)
def initialize_xavier(self):
torch.nn.init.xavier_uniform_(self.fc1.weight)
torch.nn.init.xavier_uniform_(self.fc2.weight)
class SigmoidTermination:
def __init__(self, rng, observation_space, lr, temp, device):
assert(isinstance(observation_space, gym.spaces.Box))
self.rng = rng
self.temp = temp
self.model = LinearNN(np.prod(observation_space.shape), 1).to(device)
# self.model.initialize_zero()
self.optim = torch.optim.Adam(self.model.parameters(), lr=lr)
self.model.eval()
def pmf(self, observation):
return F.sigmoid(self.model(observation)/self.temp).item()
def sample(self, observation):
return int(self.rng.uniform() < self.pmf(observation))
def update(self, observation, c, is_terminating):
self.model.train()
self.optim.zero_grad()
if is_terminating:
loss = - F.logsigmoid(self.model(observation)/self.temp) * c.item()
else:
loss = F.logsigmoid(- self.model(observation)/self.temp) * c.item()
loss.backward()
self.optim.step()
self.model.eval()
class SoftmaxQ:
def __init__(self, rng, observation_space, nactions, lr, temp, device):
assert(isinstance(observation_space, gym.spaces.Box))
self.rng = rng
self.nactions = nactions
self.temp = temp
# Here self.model stores the Q-values as well
self.q = LinearNN(np.prod(observation_space.shape), self.nactions).to(device)
# self.q.initialize_zero()
self.q_optim = torch.optim.Adam(self.q.parameters(), lr=lr)
self.q.eval()
def pmf(self, observation):
return F.softmax(self.q(observation)/self.temp, dim=0).detach().cpu().numpy()
def sample(self, observation):
p = self.pmf(observation)
return int(self.rng.choice(self.nactions, p=p))
def update_Q(self, observation, action, target):
self.q.train()
self.q_optim.zero_grad()
loss = (self.q(observation)[action] - torch.tensor(target).data) ** 2
loss.backward()
self.q_optim.step()
self.q.eval()
def value(self, observation, action=None):
if action is None:
return self.q(observation)
return self.q(observation)[action]
class SoftmaxAC:
def __init__(self, rng, observation_space, nactions, lr_actor, lr_critic, temp, device):
assert(isinstance(observation_space, gym.spaces.Box))
self.rng = rng
self.nactions = nactions
self.temp = temp
self.pi = LinearNN(np.prod(observation_space.shape), self.nactions).to(device)
# self.pi.initialize_zero()
# self.pi.initialize_xavier()
self.q = LinearNN(np.prod(observation_space.shape), self.nactions).to(device)
# self.q.initialize_zero()
# self.q.initialize_xavier()
self.pi_optim = torch.optim.Adam(self.pi.parameters(), lr=lr_actor)
self.q_optim = torch.optim.Adam(self.q.parameters(), lr=lr_critic)
self.pi.eval()
self.q.eval()
def pmf(self, observation):
return F.softmax(self.pi(observation)/self.temp, dim=0).detach().cpu().numpy()
def sample(self, observation):
p = self.pmf(observation)
return int(self.rng.choice(self.nactions, p=p))
def value(self, observation, action=None):
if action is None:
return self.q(observation)
return self.q(observation)[action]
def update_pi(self, observation, action, c):
self.pi.train()
self.pi_optim.zero_grad()
loss = - F.log_softmax(self.pi(observation)/self.temp, dim=0)[action] * c.item()
loss.backward()
self.pi_optim.step()
self.pi.eval()
def update_Q(self, observation, action, target):
self.q.train()
self.q_optim.zero_grad()
loss = (self.q(observation)[action] - torch.tensor(target).data) ** 2
loss.backward()
self.q_optim.step()
self.q.eval()