-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeepsurv.R
60 lines (48 loc) · 1.83 KB
/
deepsurv.R
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
library(tensorflow)
library(keras)
library(survival)
# BiocManager::install("survcomp")
neg_log_likelihd <- function(y_true, y_pred) {
event <- y_true[, 1]
time <- y_true[, 2]
mask <- k_cast(time <= k_reshape(time, shape = c(-1, 1)), dtype = "float32")
log_loss <- k_log(k_sum(mask * k_exp(y_pred), axis = 1))
neg_log_loss <- -k_sum(event * (y_pred - log_loss))
return(neg_log_loss / k_sum(event))
}
build_deepsurv <- function(num_input,
num_layer,
num_nodes,
string_activation,
num_l2,
num_dropout,
num_lr,
lr_decay) {
input_layer <- layer_input(shape = c(num_input),
dtype = "float32")
hidden_layers <- input_layer
for (i in 1:num_layer) {
hidden_layers <- hidden_layers %>%
layer_dense(units = num_nodes,
activation = string_activation,
# kernel_regularizer = regularizer_l2(num_l2),
kernel_initializer = "glorot_uniform") %>%
layer_dropout(rate = num_dropout)
}
hidden_layers <- hidden_layers %>%
layer_dense(units = 1,
activation = "linear",
kernel_regularizer = regularizer_l2(num_l2)) %>%
layer_activity_regularization(l2 = num_l2)
model <- keras_model(input_layer, hidden_layers)
model %>% compile(
loss = neg_log_likelihd,
optimizer = optimizer_nadam(learning_rate = num_lr,
decay = lr_decay,
clipnorm=1.)
)
}
c_index <- function(LP_pred, Y, E){
c_index <- survcomp::concordance.index(LP_pred, Y, E)$c.index
return(c_index)
}