-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes on minimal errors on plotting
- Loading branch information
Luis Roberto Mercado Diaz
committed
Apr 2, 2024
1 parent
3c61965
commit 4f589f1
Showing
6 changed files
with
114 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Wed Feb 7 15:34:31 2024 | ||
@author: lrm22005 | ||
""" | ||
import os | ||
import tqdm | ||
import torch | ||
from utils.data_loader import preprocess_data, split_uids, update_train_loader_with_uncertain_samples | ||
from models.ss_gp_model import MultitaskGPModel, train_gp_model | ||
from utils_gp.ss_evaluation import stochastic_evaluation, evaluate_model_on_all_data | ||
from active_learning.ss_active_learning import stochastic_uncertainty_sampling, run_minibatch_kmeans, stochastic_compare_kmeans_gp_predictions, label_samples | ||
from utils.visualization import plot_comparative_results, plot_training_performance, plot_results | ||
|
||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
|
||
def main(): | ||
n_classes = 4 | ||
batch_size = 1024 | ||
clinical_trial_train, clinical_trial_test, clinical_trial_unlabeled = split_uids() | ||
data_format = 'pt' | ||
|
||
train_loader, val_loader, test_loader = preprocess_data(data_format, clinical_trial_train, clinical_trial_test, clinical_trial_unlabeled, batch_size) | ||
|
||
# Initialize result storage | ||
results = { | ||
'train_loss': [], | ||
'validation_metrics': {'precision': [], 'recall': [], 'f1': [], 'auc_roc': []}, | ||
'active_learning': {'validation_metrics': []}, # Store validation metrics for each active learning iteration | ||
'test_metrics': None | ||
} | ||
|
||
# Initial model training | ||
model, likelihood, training_metrics = train_gp_model(train_loader, val_loader, num_iterations=50, n_classes=n_classes, patience=10, checkpoint_path='model_checkpoint_full.pt') | ||
|
||
# Save initial training metrics | ||
results['train_loss'].extend(training_metrics['train_loss']) | ||
for metric in ['precision', 'recall', 'f1_score']: | ||
results['validation_metrics'][metric].extend(training_metrics[metric]) | ||
|
||
active_learning_iterations = 10 | ||
for iteration in tqdm.tqdm(range(active_learning_iterations), desc='Active Learning', unit='iteration'): | ||
uncertain_sample_indices = stochastic_uncertainty_sampling(model, likelihood, val_loader, n_samples=batch_size, device=device) | ||
train_loader = update_train_loader_with_uncertain_samples(train_loader, uncertain_sample_indices, batch_size) | ||
|
||
# Re-train the model with updated training data | ||
model, likelihood, val_metrics = train_gp_model(train_loader, val_loader, num_iterations=10, n_classes=n_classes, patience=10, checkpoint_path='model_checkpoint_last.pt') | ||
|
||
# Store validation metrics for each active learning iteration | ||
results['active_learning']['validation_metrics'].append(val_metrics) | ||
|
||
# Final evaluations | ||
test_metrics = evaluate_model_on_all_data(model, likelihood, test_loader, device, n_classes) | ||
results['test_metrics'] = test_metrics | ||
|
||
# Visualization of results | ||
plot_training_performance(results['train_loss'], results['validation_metrics']) | ||
plot_results(results['test_metrics']) # Adjust this function to handle the structure of test_metrics | ||
|
||
print("Final Test Metrics:", results['test_metrics']) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+1.71 KB
(110%)
BML_project/utils_gp/__pycache__/data_loader.cpython-311.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters