def train(args): """Train model""" train_filename = f"{args.input_dir}/{ext_utils.TRAIN}_{ext_utils.DATA_FILENAME}" train_raw = list(np.load(train_filename).values()) val_filename = f"{args.input_dir}/{ext_utils.VAL}_{ext_utils.DATA_FILENAME}" val_raw = list(np.load(val_filename).values()) # Prepare training model = build_model(input_dim=train_raw[0].shape[2]) path = os.path.join( args.output_dir, 'keras_states/' + model.final_name + '.epoch{epoch}.test{val_loss}.state') metrics_callback = keras_utils.InHospitalMortalityMetrics( train_data=train_raw, val_data=val_raw, target_repl=False, batch_size=BATCH_SIZE, verbose=VERBOSE) # make sure save directory exists dirname = os.path.dirname(path) if not os.path.exists(dirname): os.makedirs(dirname) saver = ModelCheckpoint(path, verbose=1, period=SAVE_EVERY) keras_logs = os.path.join(args.output_dir, 'keras_logs') if not os.path.exists(keras_logs): os.makedirs(keras_logs) csv_logger = CSVLogger(os.path.join(keras_logs, model.final_name + '.csv'), append=True, separator=';') print("==> training") model.fit(x=train_raw[0], y=train_raw[1], validation_data=val_raw, epochs=args.epochs, initial_epoch=0, callbacks=[metrics_callback, saver, csv_logger], shuffle=True, verbose=2, batch_size=8) return model
data[1][1] = np.expand_dims(data[1][1], axis=-1) # (B, T, 1) return data train_raw = extend_labels(train_raw) val_raw = extend_labels(val_raw) if args.mode == 'train': # Prepare training path = os.path.join( args.output_dir, 'keras_states/' + model.final_name + '.epoch{epoch}.test{val_loss}.state') metrics_callback = keras_utils.InHospitalMortalityMetrics( train_data=train_raw, val_data=val_raw, target_repl=(args.target_repl_coef > 0), batch_size=args.batch_size, verbose=args.verbose) # make sure save directory exists dirname = os.path.dirname(path) if not os.path.exists(dirname): os.makedirs(dirname) saver = ModelCheckpoint(path, verbose=1, period=args.save_every) keras_logs = os.path.join(args.output_dir, 'keras_logs') if not os.path.exists(keras_logs): os.makedirs(keras_logs) csv_logger = CSVLogger(os.path.join(keras_logs, model.final_name + '.csv'), append=True, separator=';')
train_raw = extend_labels(train_raw) val_raw = extend_labels(val_raw) if args.mode == 'train': # Prepare training path = os.path.join( args.output_dir, 'keras_states/' + model.final_name + '.epoch{epoch}.test{val_loss}.state') metrics_callback = keras_utils.InHospitalMortalityMetrics( train_data=train_raw, val_data=val_raw, target_repl=(args.target_repl_coef > 0), delta=1e-7, batch_size=args.batch_size, verbose=args.verbose, dp=args.dp, noise_multiplier=args.noise_multiplier) # make sure save directory exists dirname = os.path.dirname(path) if not os.path.exists(dirname): os.makedirs(dirname) saver = ModelCheckpoint(path, verbose=1, period=args.save_every) keras_logs = os.path.join(args.output_dir, 'keras_logs') if not os.path.exists(keras_logs): os.makedirs(keras_logs) csv_logger = CSVLogger(os.path.join(keras_logs, model.final_name + '.csv'), append=True,