[987eec]: / survival4D / nn / tf / models.py

Download this file

146 lines (112 with data), 5.7 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 keras
from typing import Tuple
from keras import backend as K
from keras.models import Model
from keras.layers import Input, BatchNormalization, Activation
from keras.layers.core import Dense, Dropout
from keras.optimizers import Adam
from keras.regularizers import l1
def baseline_autoencoder(
input_shape: Tuple, dropout: float, num_ae_units1: int, num_ae_units2: int, l1_reg_lambda_exp: float,
) -> Model:
"""Baseline autoencoder as published in https://www.nature.com/articles/s42256-019-0019-2"""
inputvec = Input(shape=(input_shape,))
x = Dropout(dropout, input_shape=(input_shape,))(inputvec)
x = Dense(units=int(num_ae_units1), activation='relu', activity_regularizer=l1(10**l1_reg_lambda_exp))(x)
encoded = Dense(units=int(num_ae_units2), activation='relu', name='encoded')(x)
risk_pred = Dense(units=1, activation='linear', name='predicted_risk')(encoded)
z = Dense(units=int(num_ae_units1), activation='relu')(encoded)
decoded = Dense(units=input_shape, activation='linear', name='decoded')(z)
model = Model(inputs=inputvec, outputs=[decoded, risk_pred])
return model
def baseline_bn_autoencoder(
input_shape: Tuple, dropout: float, num_ae_units1: int, num_ae_units2: int, l1_reg_lambda_exp: float,
) -> Model:
"""Add batch normalization to each layer before relu activation, based on baseline_autoencoder."""
inputvec = Input(shape=(input_shape,))
x = Dropout(dropout, input_shape=(input_shape,))(inputvec)
x = Dense(units=int(num_ae_units1), activation=None, activity_regularizer=l1(10**l1_reg_lambda_exp))(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Dense(units=int(num_ae_units2), activation=None)(x)
x = BatchNormalization()(x)
encoded = Activation("relu", name='encoded')(x)
risk_pred = Dense(units=1, activation='linear', name='predicted_risk')(encoded)
x = Dense(units=int(num_ae_units1), activation=None)(encoded)
x = BatchNormalization()(x)
z = Activation("relu")(x)
decoded = Dense(units=input_shape, activation='linear', name='decoded')(z)
model = Model(inputs=inputvec, outputs=[decoded, risk_pred])
return model
def model3_bn_autoencoder(
input_shape: Tuple, dropout: float, num_ae_units1: int, num_ae_units2: int, num_risk_units: int,
l1_reg_lambda_exp: float,
) -> Model:
"""
Add one more relu layer between encoded and risk_pred, based on baseline_bn_autoencoder.
Model 3 architecture: https://arxiv.org/pdf/1910.02951v1.pdf
"""
inputvec = Input(shape=(input_shape,))
x = Dropout(dropout, input_shape=(input_shape,))(inputvec)
x = Dense(units=int(num_ae_units1), activation=None, activity_regularizer=l1(10**l1_reg_lambda_exp))(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Dense(units=int(num_ae_units2), activation=None)(x)
x = BatchNormalization()(x)
encoded = Activation("relu", name='encoded')(x)
x = Dense(units=num_risk_units, activation=None)(encoded)
x = BatchNormalization()(x)
x = Activation("relu", name='encoded')(x)
risk_pred = Dense(units=1, activation='linear', name='predicted_risk')(x)
x = Dense(units=int(num_ae_units1), activation=None)(encoded)
x = BatchNormalization()(x)
z = Activation("relu")(x)
decoded = Dense(units=input_shape, activation='linear', name='decoded')(z)
model = Model(inputs=inputvec, outputs=[decoded, risk_pred])
return model
def deep_model3_bn_autoencoder(
input_shape: Tuple, dropout: float, num_ae_units1: int, num_ae_units2: int, num_ae_units3: int,
num_risk_units: int, l1_reg_lambda_exp: float,
) -> Model:
"""
Add one more relu layer in autoencoder, based on model3_bn_autoencoder.
Model 3 architecture: https://arxiv.org/pdf/1910.02951v1.pdf
"""
inputvec = Input(shape=(input_shape,))
x = Dropout(dropout, input_shape=(input_shape,))(inputvec)
x = Dense(units=int(num_ae_units1), activation=None, activity_regularizer=l1(10**l1_reg_lambda_exp))(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Dense(units=int(num_ae_units2), activation=None)(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Dense(units=int(num_ae_units3), activation=None)(x)
x = BatchNormalization()(x)
encoded = Activation("relu", name='encoded')(x)
x = Dense(units=num_risk_units, activation=None)(encoded)
x = BatchNormalization()(x)
x = Activation("relu", name='encoded')(x)
risk_pred = Dense(units=1, activation='linear', name='predicted_risk')(x)
x = Dense(units=int(num_ae_units2), activation=None)(encoded)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Dense(units=int(num_ae_units1), activation=None)(x)
x = BatchNormalization()(x)
z = Activation("relu")(x)
decoded = Dense(units=input_shape, activation='linear', name='decoded')(z)
model = Model(inputs=inputvec, outputs=[decoded, risk_pred])
return model
def model_factory(model_name: str, **kwargs):
# Before defining network architecture, clear current computation graph (if one exists)
K.clear_session()
if model_name == "baseline_autoencoder":
model = baseline_autoencoder(**kwargs)
elif model_name == "baseline_bn_autoencoder":
model = baseline_bn_autoencoder(**kwargs)
elif model_name == "model3_bn_autoencoder":
model = model3_bn_autoencoder(**kwargs)
elif model_name == "deep_model3_bn_autoencoder":
model = deep_model3_bn_autoencoder(**kwargs)
else:
raise ValueError("Model name {} has not been implemented.".format(model_name))
return model