forked from thushv89/SurvivalNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RiskLayer.py
57 lines (51 loc) · 1.99 KB
/
RiskLayer.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
__author__ = 'Song'
import numpy
import theano
import theano.tensor as T
import theano.tensor.extra_ops as Te
class RiskLayer(object):
def __init__(self, input, n_in, n_out, rng):
# rng = numpy.random.RandomState(111111)
# initialize with 0 the weights W as a matrix of shape (n_in, n_out)
self.W = theano.shared(
value = numpy.asarray(
rng.uniform(
low=-numpy.sqrt(6. / (n_in + n_out)),
high=numpy.sqrt(6. / (n_in + n_out)),
size=(n_in, n_out)
),
# rng.normal(size=(n_in, n_out)),
dtype=theano.config.floatX
),
name='W',
borrow=True
)
# initialize the baises b as a vector of n_out 0s
# self.b = theano.shared(
# value=numpy.zeros(n_out, dtype=theano.config.floatX),
# name='b',
# borrow=True
# )
self.input = input
self.output = T.dot(self.input, self.W ).flatten()
self.params = [self.W ]
def cost(self, observed, at_risk):
prediction = self.output
exp = T.exp(prediction)[::-1]
partial_sum = Te.cumsum(exp)[::-1] + 1 # get the reversed partial cumulative sum
log_at_risk = T.log(partial_sum[at_risk])
diff = prediction - log_at_risk
cost = T.sum(T.dot(observed, diff))
return cost
def gradient(self, observed, at_risk):
prediction = self.output
risk = T.exp(prediction)
product = self.input * (risk * T.ones((1, self.input.shape[0])))
numerator = Te.cumsum(product[::-1])[::-1][at_risk]
denominator = Te.cumsum(risk[::-1])[::-1][at_risk] * T.ones((1, self.input.shape[0]))
numerator = numerator.flatten()
denominator = denominator.flatten()
gradient = T.dot(observed, self.input - (numerator / denominator))
return gradient
def reset_weight(self, params):
self.W.set_value(params)