|
| 1 | +# Setup |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +# import gzip |
| 6 | + |
| 7 | +# import math |
| 8 | +# import matplotlib |
| 9 | +# matplotlib.use('Agg') |
| 10 | + |
| 11 | +# import matplotlib.pyplot as plt |
| 12 | + |
| 13 | +from tensorflow.keras import backend as K |
| 14 | +import tensorflow.keras.optimizers as optimizers |
| 15 | +from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger, ReduceLROnPlateau, EarlyStopping |
| 16 | + |
| 17 | +file_path = os.path.dirname(os.path.realpath(__file__)) |
| 18 | +lib_path = os.path.abspath(os.path.join(file_path, '..', '..', 'common')) |
| 19 | +sys.path.append(lib_path) |
| 20 | + |
| 21 | +import candle |
| 22 | +import smiles_transformer as st |
| 23 | + |
| 24 | +import tensorflow.config.experimental |
| 25 | +gpus = tensorflow.config.experimental.list_physical_devices('GPU') |
| 26 | +try: |
| 27 | + for gpu in gpus: |
| 28 | + print("setting memory growth") |
| 29 | + tensorflow.config.experimental.set_memory_growth(gpu, True) |
| 30 | +except RuntimeError as e: |
| 31 | + print(e) |
| 32 | + |
| 33 | + |
| 34 | +def initialize_parameters(default_model='regress_default_model.txt'): |
| 35 | + |
| 36 | + # Build benchmark object |
| 37 | + sctBmk = st.BenchmarkST(st.file_path, default_model, 'keras', |
| 38 | + prog='p1b1_baseline', |
| 39 | + desc='Multi-task (DNN) for data extraction from clinical reports - Pilot 3 Benchmark 1') |
| 40 | + |
| 41 | + # Initialize parameters |
| 42 | + gParameters = candle.finalize_parameters(sctBmk) |
| 43 | + |
| 44 | + return gParameters |
| 45 | + |
| 46 | + |
| 47 | +# Train and Evaluate |
| 48 | + |
| 49 | +def run(params): |
| 50 | + |
| 51 | + x_train, y_train, x_val, y_val = st.load_data(params) |
| 52 | + |
| 53 | + model = st.transformer_model(params) |
| 54 | + |
| 55 | + optimizer = optimizers.deserialize({'class_name': params['optimizer'], 'config': {}}) |
| 56 | + |
| 57 | + # I don't know why we set base_lr. It doesn't appear to be used. |
| 58 | + if 'base_lr' in params and params['base_lr'] > 0: |
| 59 | + base_lr = params['base_lr'] |
| 60 | + else: |
| 61 | + base_lr = K.get_value(optimizer.lr) |
| 62 | + |
| 63 | + if 'learning_rate' in params and params['learning_rate'] > 0: |
| 64 | + K.set_value(optimizer.lr, params['learning_rate']) |
| 65 | + print('Done setting optimizer {} learning rate to {}'.format( |
| 66 | + params['optimizer'],params['learning_rate'])) |
| 67 | + |
| 68 | + model.compile(loss='mean_squared_error', |
| 69 | + optimizer=optimizer, |
| 70 | + metrics=['mae', st.r2]) |
| 71 | + |
| 72 | + # set up a bunch of callbacks to do work during model training.. |
| 73 | + |
| 74 | + checkpointer = ModelCheckpoint(filepath='smile_regress.autosave.model.h5', verbose=1, save_weights_only=True, save_best_only=True) |
| 75 | + csv_logger = CSVLogger('smile_regress.training.log') |
| 76 | + reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.75, patience=20, verbose=1, mode='auto', epsilon=0.0001, cooldown=3, min_lr=0.000000001) |
| 77 | + early_stop = EarlyStopping(monitor='val_loss', patience=100, verbose=1, mode='auto') |
| 78 | + |
| 79 | + history = model.fit(x_train, y_train, |
| 80 | + batch_size=params['batch_size'], |
| 81 | + epochs=params['epochs'], |
| 82 | + verbose=1, |
| 83 | + validation_data=(x_val, y_val), |
| 84 | + callbacks=[checkpointer, csv_logger, reduce_lr, early_stop]) |
| 85 | + |
| 86 | + model.load_weights('smile_regress.autosave.model.h5') |
| 87 | + |
| 88 | + return history |
| 89 | + |
| 90 | +def main(): |
| 91 | + params = initialize_parameters() |
| 92 | + run(params) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + main() |
| 97 | + if K.backend() == 'tensorflow': |
| 98 | + K.clear_session() |
0 commit comments