-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_classes.py
163 lines (134 loc) · 6.03 KB
/
model_classes.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
#/usr/bin/env python3
import numpy as np
import scipy.stats as st
import operator
from functools import reduce
import torch
import torch.nn as nn
from torch.autograd import Function
from torch.nn.parameter import Parameter
import torch.optim as optim
from qpth.qp import QPFunction
from constants import *
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self, X, Y, hidden_layer_sizes):
super(Net, self).__init__()
# Linear -> BatchNorm -> ReLU -> Dropout layers
layer_sizes = [X.shape[1]] + hidden_layer_sizes
layers = reduce(operator.add,
[[nn.Linear(a,b), nn.BatchNorm1d(b), nn.ReLU(), nn.Dropout(p=0.2)]
for a,b in zip(layer_sizes[0:-1], layer_sizes[1:])])
layers += [nn.Linear(layer_sizes[-1], Y.shape[1]*2)]
self.net = nn.Sequential(*layers)
def forward(self, x):
prediction = self.net(x)
mu = prediction[:,0:24]
sigma = prediction[:,24:]
sigma = F.softplus(sigma)+1e-6
return mu, sigma
def GLinearApprox(gamma_under, gamma_over):
#Linear (gradient) approximation of G function at z
class GLinearApproxFn(Function):
@staticmethod
def forward(ctx, z, mu, sig):
ctx.save_for_backward(z, mu, sig)
p = st.norm(mu.cpu().numpy(),sig.cpu().numpy())
res = torch.DoubleTensor((gamma_under + gamma_over) * p.cdf(
z.cpu().numpy()) - gamma_under)
if USE_GPU:
res = res.cuda()
return res
@staticmethod
def backward(ctx, grad_output):
z, mu, sig = ctx.saved_tensors
p = st.norm(mu.cpu().numpy(),sig.cpu().numpy())
pz = torch.tensor(p.pdf(z.cpu().numpy()), dtype=torch.double, device=DEVICE)
dz = (gamma_under + gamma_over) * pz
dmu = -dz
dsig = -(gamma_under + gamma_over)*(z-mu) / sig * pz
return grad_output * dz, grad_output * dmu, grad_output * dsig
return GLinearApproxFn.apply
def GQuadraticApprox(gamma_under, gamma_over):
""" Quadratic (gradient) approximation of G function at z"""
class GQuadraticApproxFn(Function):
@staticmethod
def forward(ctx, z, mu, sig):
ctx.save_for_backward(z, mu, sig)
p = st.norm(mu.cpu().numpy(),sig.cpu().numpy())
res = torch.DoubleTensor((gamma_under + gamma_over) * p.pdf(
z.cpu().numpy()))
if USE_GPU:
res = res.cuda()
return res
@staticmethod
def backward(ctx, grad_output):
z, mu, sig = ctx.saved_tensors
p = st.norm(mu.cpu().numpy(),sig.cpu().numpy())
pz = torch.tensor(p.pdf(z.cpu().numpy()), dtype=torch.double, device=DEVICE)
dz = -(gamma_under + gamma_over) * (z-mu) / (sig**2) * pz
dmu = -dz
dsig = (gamma_under + gamma_over) * ((z-mu)**2 - sig**2) / \
(sig**3) * pz
return grad_output * dz, grad_output * dmu, grad_output * dsig
return GQuadraticApproxFn.apply
class SolveSchedulingQP(nn.Module):
""" Solve a single SQP iteration of the scheduling problem"""
def __init__(self, params):
super(SolveSchedulingQP, self).__init__()
self.c_ramp = params["c_ramp"]
self.n = params["n"]
D = np.eye(self.n - 1, self.n) - np.eye(self.n - 1, self.n, 1)
self.G = torch.tensor(np.vstack([D,-D]), dtype=torch.double, device=DEVICE)
self.h = (self.c_ramp * torch.ones((self.n - 1) * 2, device=DEVICE)).double()
self.e = torch.DoubleTensor()
if USE_GPU:
self.e = self.e.cuda()
def forward(self, z0, mu, dg, d2g):
nBatch, n = z0.size()
Q = torch.cat([torch.diag(d2g[i] + 1).unsqueeze(0)
for i in range(nBatch)], 0).double()
p = (dg - d2g*z0 - mu).double()
G = self.G.unsqueeze(0).expand(nBatch, self.G.size(0), self.G.size(1))
h = self.h.unsqueeze(0).expand(nBatch, self.h.size(0))
out = QPFunction(verbose=False)(Q, p, G, h, self.e, self.e)
return out
class SolveScheduling(nn.Module):
""" Solve the entire scheduling problem, using sequential quadratic
programming. """
def __init__(self, params):
super(SolveScheduling, self).__init__()
self.params = params
self.c_ramp = params["c_ramp"]
self.n = params["n"]
D = np.eye(self.n - 1, self.n) - np.eye(self.n - 1, self.n, 1)
self.G = torch.tensor(np.vstack([D,-D]), dtype=torch.double, device=DEVICE)
self.h = (self.c_ramp * torch.ones((self.n - 1) * 2, device=DEVICE)).double()
self.e = torch.DoubleTensor()
if USE_GPU:
self.e = self.e.cuda()
def forward(self, mu, sig):
nBatch, n = mu.size()
# Find the solution via sequential quadratic programming,
# not preserving gradients
z0 = mu.detach()
mu0 = mu.detach()
sig0 = sig.detach()
for i in range(20):
dg = GLinearApprox(self.params["gamma_under"],
self.params["gamma_over"])(z0, mu0, sig0)
d2g = GQuadraticApprox(self.params["gamma_under"],
self.params["gamma_over"])(z0, mu0, sig0)
z0_new = SolveSchedulingQP(self.params)(z0, mu0, dg, d2g)
solution_diff = (z0-z0_new).norm().item()
#print("+ SQP Iter: {}, Solution diff = {}".format(i, solution_diff))
z0 = z0_new
if solution_diff < 1e-10:
break
# Now that we found the solution, compute the gradient-propagating
# version at the solution
dg = GLinearApprox(self.params["gamma_under"],
self.params["gamma_over"])(z0, mu, sig)
d2g = GQuadraticApprox(self.params["gamma_under"],
self.params["gamma_over"])(z0, mu, sig)
return SolveSchedulingQP(self.params)(z0, mu, dg, d2g)