forked from google-research/google-research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoupled_deep_cph.py
299 lines (216 loc) · 7.77 KB
/
coupled_deep_cph.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# coding=utf-8
# Copyright 2021 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Definition of the proposed Coupled Deep Cox.
This module has the tensorflow definitions of the proposed Coupled Deep Cox
model and utility functions to train and evaluate the model.
The module depends on tensorflow 2.
Not designed to be called directly, would be called when running a function from
fair_survival_analysis.fair_survival_analysis
"""
from coupled_deep_cph_utils import partial_ll_loss
from coupled_deep_cph_utils import train_breslow
import lifelines
import numpy as np
from sklearn.utils import shuffle
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense
tf.keras.backend.set_floatx('float64')
class CoupledDeepCPH(Model):
"""Tensorflow model definition of the Coupled Deep CPH Survival Model.
The Coupled Deep CPH model involves learning shared representations for
the each demographic in the dataset.The representation then interacts with
multiple output heads to determine the log-partial hazard for an individual
in each group.
"""
def __init__(self, hidden):
super(CoupledDeepCPH, self).__init__()
self.rep1 = Dense(hidden, activation='relu', use_bias=False)
self.rep2 = Dense(hidden, activation='sigmoid', use_bias=False)
self.prot = Dense(1, use_bias=False, kernel_initializer='zeros')
self.nprot = Dense(1, use_bias=False, kernel_initializer='zeros')
def call(self, x):
x = self.rep2(self.rep1(x))
return self.prot(x), self.nprot(x)
class CPH(Model):
"""Tensorflow model definition for a standard CPH model.
The CPH model involves learning separate CPH model for each demographic in the
dataset.
"""
def __init__(self):
super(CPH, self).__init__()
self.prot = Dense(1, use_bias=False, kernel_initializer='zeros')
self.nprot = Dense(1, use_bias=False, kernel_initializer='zeros')
def call(self, x):
return self.prot(x), self.nprot(x)
class DeepCPH(Model):
"""Tensorflow model definition of the Deep CPH Survival Model.
Involves learning a separate Deep Surv/Faraggi-Simon network for each
demographic.
"""
def __init__(self, hidden):
super(DeepCPH, self).__init__()
self.rep1 = Dense(100, use_bias=False, activation='relu')
self.rep2 = Dense(100, use_bias=False, activation='relu')
self.prot = Dense(1, use_bias=False, kernel_initializer='zeros')
self.nprot = Dense(1, use_bias=False, kernel_initializer='zeros')
def call(self, x):
return self.prot(self.rep1(x)), self.nprot(self.rep2(x))
def train_step(model, x, t, e, a, optimizer, bs=256, lambd=1.0, seed=0):
"""Optimizes the model for one epoch.
Args:
model:
instance of CoupledDeepCPH class.
x:
a numpy array of input features (Training Data).
t:
a numpy vector of event times (Training Data).
e:
a numpy vector of event indicators (1 if event occured, 0 otherwise)
(Training Data).
a:
a numpy vector of the protected group membership (Training Data).
optimizer:
instance of tf.keras.optimizers (default is Adam)
bs:
int minibatch size.
lambd (float):
l2 penaly on the last layer.
seed:
random seed.
Returns:
None. Trains the model inplace.
"""
x, t, e, a = shuffle(x, t, e, a, random_state=seed)
n = x.shape[0]
batches = (n // bs) + 1
for i in range(batches):
xb = x[i * bs:(i + 1) * bs]
tb = t[i * bs:(i + 1) * bs]
eb = e[i * bs:(i + 1) * bs]
ab = a[i * bs:(i + 1) * bs]
with tf.GradientTape() as tape:
loss = partial_ll_loss(model, xb, tb, eb, ab, lambd)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
def test_step(model, x, t, e, a, loss='concordance', lambd=1.0):
"""Test the model and compute validation metric.
Args:
model:
instance of CoupledDeepCPH class.
x:
a numpy array of input features (Val/Test Data).
t:
a numpy vector of event times (Val/Test Data).
e:
a numpy vector of event indicators (1 if event occured, 0 otherwise)
(Val/Test Data).
a:
a numpy vector of the protected group membership (Val/Test Data).
loss (str):
string the loss metric to compute. one of 'concordance' or 'pll'.
lambd (float):
l2 penaly on the last layer.
Returns:
a float loss.
"""
if loss == 'concordance':
risks = np.zeros_like(a)
lrisksp, lrisksn = model(x)
lrisksp, lrisksn = lrisksp[:, 0], lrisksn[:, 0]
risks[a == 1] = lrisksp[a == 1]
risks[a == 0] = lrisksn[a == 0]
pci = lifelines.utils.concordance_index(t[a == 1], -risks[a == 1],
e[a == 1])
nci = lifelines.utils.concordance_index(t[a == 0], -risks[a == 0],
e[a == 0])
return 0.5 * (nci + pci)
if loss == 'pll':
loss = partial_ll_loss(model, x, t, e, a, lambd)
return float(loss)
def train(model,
xt,
tt,
et,
at,
xv,
tv,
ev,
av,
groups,
lambd=1,
epochs=200,
patience=2,
vloss='pll'):
"""The function used to train the Coupled Deep CPH VAE.
Trains the model and corresponding breslow's estimator given some training and
validation examples for a fixed number of epochs and learning rate.
Args:
model:
instance of CoupledDeepCPH class.
xt:
a numpy array of input features (Training Data).
tt:
a numpy vector of event times (Training Data).
et:
a numpy vector of event indicators (1 if event occured, 0 otherwise)
(Training Data).
at:
a numpy vector of the protected group membership (Training Data).
xv:
a numpy array of input features (Validation Data).
tv:
a numpy vector of event times (Validation Data).
ev:
a numpy vector of event indicators (1 if event occured, 0 otherwise)
(Validation Data).
av:
a numpy vector of the protected group membership (Validation Data).
groups:
List of the demographics to adjust for.
lambd:
float Strength of the VAE loss term.
epochs:
int Number of Training epochs to run.
patience:
number of training epochs to wait before stopping optimization.
vloss:
validation metric to optimize for. One of "pll" or "concordance".
Returns:
a trained survival analysis model and a breslow estimator.
"""
prot, nprot = groups[0], groups[1]
optimizer = tf.keras.optimizers.Adam(lr=0.001)
valc = 0
patience_ = 0
# Convert A to a binary Indicator!
at_ = at.copy()
at_[at_ == prot] = 1
at_[at_ == nprot] = 0
av_ = av.copy()
av_[av_ == prot] = 1
av_[av_ == nprot] = 0
for epoch in range(epochs):
train_step(model, xt, tt, et, at_, optimizer, lambd=lambd, seed=epoch)
valcn = test_step(model, xv, tv, ev, av_, loss=vloss, lambd=lambd)
if epoch % 1 == 0:
print(patience_, epoch, valcn)
if valcn < valc:
patience_ += 1
if patience_ >= patience:
return (model, train_breslow(model, xt, tt, et, at, xv, tv, ev, av,
groups))
valc = valcn
return (model, train_breslow(model, xt, tt, et, at, xv, tv, ev, av, groups))