def run_experiment_and_save( model, model_name, results_dir, models_dir, args, X_train, X_test, y_train, y_test, ): optimizers = set_optimizer(model, args) steps_per_epoch = (X_train.shape[0] // args.batch_size) + 1 lr_schedulers = set_lr_scheduler(optimizers, steps_per_epoch, args) early_stopping = EarlyStopping( monitor=args.monitor, min_delta=args.early_stop_delta, patience=args.early_stop_patience, ) model_checkpoint = ModelCheckpoint( filepath=str(models_dir / "best_model"), monitor=args.monitor, save_best_only=True, max_save=1, ) trainer = Trainer( model, objective="regression", optimizers=optimizers, lr_schedulers=lr_schedulers, reducelronplateau_criterion=args.monitor.split("_")[-1], callbacks=[ early_stopping, model_checkpoint, LRHistory(n_epochs=args.n_epochs) ], ) start = time() trainer.fit( X_train={ "X_tab": X_train, "target": y_train }, X_val={ "X_tab": X_test, "target": y_test }, n_epochs=args.n_epochs, batch_size=args.batch_size, validation_freq=args.eval_every, ) runtime = time() - start y_pred = trainer.predict(X_tab=X_test) rmse = np.sqrt(mean_squared_error(y_test, y_pred)) r2 = r2_score(y_test, y_pred) print(f"rmse with the best model: {rmse}") if args.save_results: suffix = str(datetime.now()).replace(" ", "_").split(".")[:-1][0] filename = "_".join(["fb_comments", model_name, "best", suffix]) + ".p" results_d = {} results_d["args"] = args results_d["rmse"] = rmse results_d["r2"] = r2 results_d["early_stopping"] = early_stopping results_d["trainer_history"] = trainer.history results_d["trainer_lr_history"] = trainer.lr_history results_d["runtime"] = runtime with open(results_dir / filename, "wb") as f: pickle.dump(results_d, f)
blocks_dims=blocks_dims, blocks_dropout=args.blocks_dropout, mlp_hidden_dims=mlp_hidden_dims, mlp_activation=args.mlp_activation, mlp_dropout=args.mlp_dropout, mlp_batchnorm=args.mlp_batchnorm, mlp_batchnorm_last=args.mlp_batchnorm_last, mlp_linear_first=args.mlp_linear_first, embed_dropout=args.embed_dropout, continuous_cols=prepare_tab.continuous_cols, batchnorm_cont=args.batchnorm_cont, concat_cont_first=args.concat_cont_first, ) model = WideDeep(deeptabular=deeptabular) optimizers = set_optimizer(model, args) steps_per_epoch = (X_train.shape[0] // args.batch_size) + 1 lr_schedulers = set_lr_scheduler(optimizers, steps_per_epoch, args) early_stopping = EarlyStopping( monitor=args.monitor, min_delta=args.early_stop_delta, patience=args.early_stop_patience, ) trainer = Trainer( model, objective="regression", optimizers=optimizers, lr_schedulers=lr_schedulers, reducelronplateau_criterion=args.monitor.split("_")[-1],
def run_experiment_and_save( model, model_name, results_dir, models_dir, args, X_train, X_test, y_train, y_test, fl_exp_indx: int = 0, ): try: if args.focal_loss: alpha, gamma = load_focal_loss_params(results_dir, fl_exp_indx) focal_loss = True else: alpha = 0.25 gamma = 2 focal_loss = False except AttributeError: alpha = 0.25 gamma = 2 focal_loss = False optimizers = set_optimizer(model, args) steps_per_epoch = (X_train.shape[0] // args.batch_size) + 1 lr_schedulers = set_lr_scheduler(optimizers, steps_per_epoch, args) early_stopping = EarlyStopping( monitor=args.monitor, min_delta=args.early_stop_delta, patience=args.early_stop_patience, ) model_checkpoint = ModelCheckpoint( filepath=str(models_dir / "best_model"), monitor=args.monitor, save_best_only=True, max_save=1, ) trainer = Trainer( model, objective="binary_focal_loss" if focal_loss else "binary", optimizers=optimizers, lr_schedulers=lr_schedulers, reducelronplateau_criterion=args.monitor.split("_")[-1], callbacks=[early_stopping, model_checkpoint, LRHistory(n_epochs=args.n_epochs)], metrics=[Accuracy, F1Score], alpha=alpha, gamma=gamma, ) start = time() trainer.fit( X_train={"X_tab": X_train, "target": y_train}, X_val={"X_tab": X_test, "target": y_test}, n_epochs=args.n_epochs, batch_size=args.batch_size, validation_freq=args.eval_every, ) runtime = time() - start y_pred = trainer.predict(X_tab=X_test) acc = accuracy_score(y_test, y_pred) auc = roc_auc_score(y_test, y_pred) f1 = f1_score(y_test, y_pred) print(f"Accuracy: {acc}. F1: {f1}. ROC_AUC: {auc}") print(confusion_matrix(y_test, y_pred)) if args.save_results: suffix = str(datetime.now()).replace(" ", "_").split(".")[:-1][0] filename = "_".join(["bankm", model_name, "best", suffix]) + ".p" results_d = {} results_d["args"] = args results_d["acc"] = acc results_d["auc"] = auc results_d["f1"] = f1 results_d["early_stopping"] = early_stopping results_d["trainer_history"] = trainer.history results_d["trainer_lr_history"] = trainer.lr_history results_d["runtime"] = runtime with open(results_dir / filename, "wb") as f: pickle.dump(results_d, f)