def test_TrainDDQN(tmp_path): """Tests imbDRL.agents.ddqn.TrainDDQN.""" tmp_models = str( tmp_path / "test_model" ) # No support for pathLib https://github.com/tensorflow/tensorflow/issues/37357 tmp_logs = str(tmp_path / "test_log") model = TrainDDQN(10, 10, 0.001, 0.0, 0.1, 5, model_path=tmp_models, log_dir=tmp_logs) assert model.model_path == tmp_models assert not model.compiled NOW = datetime.now().strftime("%Y%m%d") # yyyymmdd model = TrainDDQN(10, 10, 0.001, 0.0, 0.1, 5) assert "./models/" + NOW in model.model_path # yyyymmdd in yyyymmdd_hhmmss
def test_compile_model(tmp_path): """Tests imbDRL.agents.ddqn.TrainDDQN.compile_model.""" tmp_models = str( tmp_path / "test_model" ) # No support for pathLib https://github.com/tensorflow/tensorflow/issues/37357 tmp_logs = str(tmp_path / "test_log") model = TrainDDQN(10, 10, 0.001, 0.0, 0.1, 5, model_path=tmp_models, log_dir=tmp_logs) assert not model.compiled model.compile_model( np.random.rand(4, 12).astype(np.float32), np.random.choice(2, size=4).astype(np.int32), [Dense(4), Dense(2)]) assert model.compiled
) # Normalization should happen after splitting train and test sets X_train, X_test, y_train, y_test = train_test_split(df.to_numpy(), y, stratify=y, test_size=0.2) X_train, y_train, X_test, y_test, X_val, y_val = get_train_test_val( X_train, y_train, X_test, y_test, min_class, maj_class, val_frac=0.2) model = TrainDDQN(episodes, warmup_steps, learning_rate, gamma, min_epsilon, decay_episodes, target_update_period=target_update_period, target_update_tau=target_update_tau, batch_size=batch_size, collect_steps_per_episode=collect_steps_per_episode, memory_length=memory_length, collect_every=collect_every, n_step_update=n_step_update) model.compile_model(X_train, y_train, layers) model.q_net.summary() model.train(X_val, y_val, "F1") stats = model.evaluate(X_test, y_test, X_train, y_train) print(rounded_dict(stats)) # {'Gmean': 0.824172, 'F1': 0.781395, 'Precision': 0.730435, 'Recall': 0.84, 'TP': 84, 'TN': 131, 'FP': 31, 'FN': 16}
from imbDRL.metrics import (classification_metrics, network_predictions, plot_confusion_matrix, plot_pr_curve, plot_roc_curve) from imbDRL.utils import rounded_dict os.environ[ "CUDA_VISIBLE_DEVICES"] = "-1" # CPU is faster than GPU on structured data min_class = [1] # Minority classes, same setup as in original paper maj_class = [0] # Majority classes fp_model = "./models/20210118_132311.pkl" X_train, y_train, X_test, y_test = load_csv("./data/credit0.csv", "./data/credit1.csv", "Class", ["Time"], normalization=True) network = TrainDDQN.load_network(fp_model) y_pred_train = network_predictions(network, X_train) y_pred_test = network_predictions(network, X_test) stats = classification_metrics(y_train, y_pred_train) print(f"Train: {rounded_dict(stats)}") stats = classification_metrics(y_test, y_pred_test) print(f"Test: {rounded_dict(stats)}") plot_pr_curve(network, X_test, y_test, X_train, y_train) plot_roc_curve(network, X_test, y_test, X_train, y_train) plot_confusion_matrix(stats.get("TP"), stats.get("FN"), stats.get("FP"), stats.get("TN"))
def test_train(tmp_path): """Tests imbDRL.agents.ddqn.TrainDDQN.train.""" tmp_models = str( tmp_path / "test_model" ) # No support for pathLib https://github.com/tensorflow/tensorflow/issues/37357 tmp_logs = str(tmp_path / "test_log") model = TrainDDQN(10, 10, 0.001, 0.0, 0.1, 5, model_path=tmp_models, log_dir=tmp_logs, val_every=2, memory_length=30) with pytest.raises(Exception) as exc: model.train() assert "must be compiled" in str(exc.value) model.compile_model( np.random.rand(4, 10).astype(np.float32), np.random.choice(2, size=4).astype(np.int32), [Dense(4), Dense(2)]) model.train( np.random.rand(4, 10).astype(np.float32), np.random.choice(2, size=4).astype(np.int32)) assert model.replay_buffer.num_frames().numpy( ) >= 10 + 10 # 10 for warmup + 1 for each episode assert model.global_episode == 10 model = TrainDDQN(10, 10, 0.001, 0.0, 0.1, 5, model_path=tmp_models, log_dir=tmp_logs, val_every=2) with pytest.raises(Exception) as exc: model.train() assert "must be compiled" in str(exc.value) model.compile_model( np.random.rand(4, 10).astype(np.float32), np.random.choice(2, size=4).astype(np.int32), [Dense(4), Dense(2)]) model.train( np.random.rand(4, 10).astype(np.float32), np.random.choice(2, size=4).astype(np.int32)) assert model.replay_buffer.num_frames( ) >= 10 # 10 in total since no memory length is defined assert model.global_episode == 10