Skip to content

Commit 7d3bcfb

Browse files
authored
Merge pull request #658 from dice-group/refactor/trainers-nero-logging
Route NERO and NCES/CLIP trainer output through logging instead of print
2 parents 07e27e9 + 4f2336c commit 7d3bcfb

5 files changed

Lines changed: 54 additions & 56 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ date) and use that section as the basis for the GitHub release notes.
1212
## [Unreleased]
1313

1414
### Fixed
15+
- `NCESTrainer` and `CLIPTrainer` raised a bare `ValueError` for an unsupported optimizer; the
16+
message was a `print` placed after the `raise` and never ran. It is now the exception message.
1517
- `TripleStoreReasoner.instances(ce, direct=False)` only expanded subclasses for the root
1618
variable's type triple, so class expressions with fillers (e.g.
1719
`Grandfather ⊓ ∃hasChild.Grandparent`) missed individuals typed only by a subclass of the
@@ -33,6 +35,9 @@ date) and use that section as the basis for the GitHub release notes.
3335
training, grid-search parameters, classification report) through `logging` instead of `print`.
3436
The duplicate verbose `print` after each "Error extracting ... features" warning is removed,
3537
as the same error was already logged.
38+
- `NERO` (`verbose` levels map to `info`/`debug`; the "queue exceeded max" notice is now an
39+
unconditional `warning`), `NCESTrainer` and `CLIPTrainer` report through `logging` instead of
40+
`print`; the decorative `#`/`*` banners are gone.
3641
- Re-enabled `tests/test_semantic_cache.py` (previously 100% commented out), exercising only the
3742
symbolic (HermiT) reasoner; the neural (EBR) path is left out as in-process KGE training
3843
alongside the JVM segfaulted CI (#515).

‎Improvements.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ none remain open as of this writing.
2929
`knowledge_base.py`, `semantic_caching.py`,
3030
`learners/tree_learner_refinement_inherit.py`,
3131
`learners/nces.py`, `learners/nces2.py`, `learners/tree_learner.py`,
32+
`learners/nero.py`, `nces_trainer.py`, `clip_trainer.py`,
3233
`learners/spell_kit/structures.py` (except `structure_to_dot`, whose
3334
`print()`s are genuine DOT-graph text output, not log messages), and
34-
`learners/drill.py`. `print(` still appears **~230 times** across the rest
35+
`learners/drill.py`. `print(` still appears **~155 calls** across the rest
3536
of `ontolearn/**/*.py`; remaining offenders include
36-
`learners/celoe.py` (11), `learners/sparql_query_learner.py` (4), and
37-
`data_struct.py` (10) — several of these overlap with the broad-except
37+
`lp_generator/helper_classes.py` (14), `binders.py` (13),
38+
`utils/static_funcs.py` (11), `learning_problem_generator.py` (10),
39+
`search.py` (9), `data_struct.py` (8) and `learners/celoe.py` (7, mostly
40+
the intentional `show_search_tree` display methods) — several of these overlap with the broad-except
3841
cleanup below and are best converted alongside that work. Routing the rest
3942
through the existing logger (with levels) would make output
4043
controllable/filterable instead of unconditionally printed.

‎ontolearn/clip_trainer.py‎

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
# SOFTWARE.
2323
# -----------------------------------------------------------------------------
2424

25+
import logging
2526
import numpy as np
2627
import copy
2728
import torch
@@ -33,6 +34,8 @@
3334
from sklearn.metrics import f1_score, accuracy_score
3435
import time
3536

37+
logger = logging.getLogger(__name__)
38+
3639

3740
class CLIPTrainer:
3841
"""CLIP trainer."""
@@ -58,30 +61,22 @@ def get_optimizer(self, length_predictor, optimizer='Adam'): # pragma: no cover
5861
elif optimizer == 'RMSprop':
5962
return torch.optim.RMSprop(length_predictor.parameters(), lr=self.learning_rate)
6063
else:
61-
raise ValueError
62-
print('Unsupported optimizer')
64+
raise ValueError(f"Unsupported optimizer: {optimizer}")
6365

6466
def show_num_learnable_params(self):
65-
print("*"*20+"Trainable model size"+"*"*20)
6667
size = sum([p.numel() for p in self.clip.length_predictor.parameters()])
67-
print("Length Predictor: ", size)
68-
print("*"*20+"Trainable model size"+"*"*20)
69-
print()
68+
logger.info(f"Trainable model size: length predictor: {size}")
7069

7170
def train(self, train_dataloader, save_model=True, optimizer='Adam', record_runtime=True):
7271
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7372
if isinstance(self.clip.length_predictor, list):
7473
self.clip.length_predictor = copy.deepcopy(self.clip.length_predictor[0])
7574
self.show_num_learnable_params()
7675
if device.type == "cpu":
77-
print("Training on CPU, it may take long...")
76+
logger.info("Training on CPU, it may take long...")
7877
else:
79-
print("GPU available !")
80-
print()
81-
print("#"*50)
82-
print()
83-
print("{} starts training... \n".format(self.clip.length_predictor.name))
84-
print("#"*50, "\n")
78+
logger.info("GPU available !")
79+
logger.info("{} starts training...".format(self.clip.length_predictor.name))
8580
length_predictor = copy.deepcopy(self.clip.length_predictor).train()
8681
desc = length_predictor.name
8782
if device.type == "cuda":
@@ -136,12 +131,11 @@ def train(self, train_dataloader, save_model=True, optimizer='Adam', record_runt
136131
json.dump(runtime_info, file, indent=3)
137132
results_dict = dict()
138133
if save_model:
139-
print("Top performance: loss: {:.4f}, f1: {:.2f}% ... "
140-
"acc: {:.2f}%".format(min(Train_loss), max(F1), max(Acc)), "weights saved based on Acc best score!")
134+
logger.info("Top performance: loss: {:.4f}, f1: {:.2f}% ... acc: {:.2f}% "
135+
"weights saved based on Acc best score!".format(min(Train_loss), max(F1), max(Acc)))
141136
else:
142-
print("Top performance: loss: {:.4f}, f1: {:.2f}% ... "
143-
"acc: {:.2f}%".format(min(Train_loss), max(F1), max(Acc)))
144-
print()
137+
logger.info("Top performance: loss: {:.4f}, f1: {:.2f}% ... "
138+
"acc: {:.2f}%".format(min(Train_loss), max(F1), max(Acc)))
145139
results_dict.update({"Train Max F1": max(F1), "Train Acc": max(Acc),
146140
"Train Min Loss": min(Train_loss)})
147141
if save_model: # pragma: no cover
@@ -152,7 +146,7 @@ def train(self, train_dataloader, save_model=True, optimizer='Adam', record_runt
152146
if not os.path.exists(self.storage_path+"/trained_models/"):
153147
os.mkdir(self.storage_path+"/trained_models/")
154148
torch.save(length_predictor.state_dict(), self.storage_path+"/trained_models/"+"trained_"+desc+".pt")
155-
print("{} saved".format(length_predictor.name))
149+
logger.info("{} saved".format(length_predictor.name))
156150
if not os.path.exists(self.storage_path+"/metrics/"):
157151
os.mkdir(self.storage_path+"/metrics/")
158152
with open(self.storage_path+"/metrics/"+"metrics_"+desc+".json", "w") as plot_file:

‎ontolearn/learners/nero.py‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import torch
3535
from owlapy import dl_to_owl_expression
3636

37+
import logging
3738
from owlapy.class_expression import OWLThing
3839
from owlapy.owl_individual import OWLNamedIndividual
3940
from owlapy.render import DLSyntaxObjectRenderer
@@ -44,6 +45,8 @@
4445
from ontolearn.refinement_operators import NERORefinement
4546
from ontolearn.utils.static_funcs import compute_f1_score
4647

48+
logger = logging.getLogger(__name__)
49+
4750
# =============================================================================
4851
# NERO Main Class
4952
# =============================================================================
@@ -118,8 +121,8 @@ def __init__(self,
118121
self._is_trained = False
119122

120123
if self.verbose > 0:
121-
print(f"NERO initialized with {self.neural_architecture} architecture")
122-
print(f"Device: {self.device}")
124+
logger.info(f"NERO initialized with {self.neural_architecture} architecture")
125+
logger.info(f"Device: {self.device}")
123126

124127
def _initialize_instance_mapping(self):
125128
"""Initialize mapping from individuals to indices."""
@@ -135,7 +138,7 @@ def _initialize_refinement_operator(self):
135138
"""Initialize the refinement operator."""
136139
if self.refinement_op is None:
137140
if self.verbose > 0:
138-
print("Initializing refinement operator...")
141+
logger.info("Initializing refinement operator...")
139142
self.refinement_op = NERORefinement(self.kb)
140143
self.expression.update(self.refinement_op.expressions)
141144

@@ -197,7 +200,7 @@ def train(self, learning_problems: List[Tuple[List[str], List[str]]]):
197200
learning_problems: List of (positive_examples, negative_examples) tuples
198201
"""
199202
if self.verbose > 0:
200-
print("Training NERO model...")
203+
logger.info("Training NERO model...")
201204

202205
start_time = time.time()
203206

@@ -276,22 +279,22 @@ def train(self, learning_problems: List[Tuple[List[str], List[str]]]):
276279
epoch_loss += loss.item()
277280

278281
if self.verbose > 0 and (epoch + 1) % 10 == 0:
279-
print(f"Epoch {epoch+1}/{self.num_epochs}, Loss: {epoch_loss:.4f}")
282+
logger.info(f"Epoch {epoch+1}/{self.num_epochs}, Loss: {epoch_loss:.4f}")
280283

281284
self.model.eval()
282285
self._is_trained = True
283286

284287
if self.verbose > 0:
285288
training_time = time.time() - start_time
286-
print(f"Training completed in {training_time:.2f} seconds")
289+
logger.info(f"Training completed in {training_time:.2f} seconds")
287290

288291
def search(self, pos: Set[str], neg: Set[str], top_k: int = 10, max_child_length: int = 10, max_queue_size: int = 10000) -> Dict:
289292
"""
290293
Perform reinforcement learning-based search for complex class expressions.
291294
Uses neural predictions to initialize and guide the search.
292295
"""
293296
if self.verbose > 0:
294-
print("Starting reinforcement learning search...")
297+
logger.info("Starting reinforcement learning search...")
295298

296299
start_time = time.time()
297300
self.search_tree = SearchTree()
@@ -346,8 +349,8 @@ def search(self, pos: Set[str], neg: Set[str], top_k: int = 10, max_child_length
346349
best_hypothesis = expr
347350

348351
if self.verbose > 0:
349-
print(f"Initial best hypothesis: {best_hypothesis.name} (Quality: {best_quality:.3f})")
350-
print(f"Initial queue size: {len(self.search_tree.gate)}")
352+
logger.info(f"Initial best hypothesis: {best_hypothesis.name} (Quality: {best_quality:.3f})")
353+
logger.info(f"Initial queue size: {len(self.search_tree.gate)}")
351354

352355
# Iterative refinement-based search
353356
iteration = 0
@@ -364,8 +367,7 @@ def search(self, pos: Set[str], neg: Set[str], top_k: int = 10, max_child_length
364367

365368
queue_size = self.search_tree.items_in_queue.qsize()
366369
if queue_size > max_queue_size:
367-
if self.verbose > 0:
368-
print(f"WARNING: Queue size ({queue_size}) exceeded max. Stopping search.")
370+
logger.warning(f"Queue size ({queue_size}) exceeded max. Stopping search.")
369371
break
370372

371373
try:
@@ -403,12 +405,12 @@ def search(self, pos: Set[str], neg: Set[str], top_k: int = 10, max_child_length
403405
best_quality = quality
404406
best_hypothesis = next_concept
405407
if self.verbose > 1:
406-
print(f"New best at iter {iteration}: {best_hypothesis.name} (Quality: {best_quality:.3f})")
408+
logger.debug(f"New best at iter {iteration}: {best_hypothesis.name} (Quality: {best_quality:.3f})")
407409

408410
# Early stopping if perfect solution found
409411
if quality >= 1.0:
410412
if self.verbose > 0:
411-
print(f"Perfect solution found at iteration {iteration}")
413+
logger.info(f"Perfect solution found at iteration {iteration}")
412414
runtime = time.time() - start_time
413415
return {
414416
'Prediction': best_hypothesis.name,
@@ -419,20 +421,20 @@ def search(self, pos: Set[str], neg: Set[str], top_k: int = 10, max_child_length
419421
except (AttributeError, KeyError, Exception) as e:
420422
# Skip concepts that cause errors during evaluation
421423
if self.verbose > 1:
422-
print(f"Skipping concept due to error: {e}")
424+
logger.debug(f"Skipping concept due to error: {e}")
423425
continue
424426

425427
if iteration % 10 == 0:
426428
time.sleep(0.001)
427429

428430
if self.verbose > 0 and iteration % 1000 == 0:
429431
elapsed = time.time() - start_time
430-
print(f"Iter {iteration}, Queue: {queue_size}, Best: {best_quality:.3f}, Time: {elapsed:.1f}s")
432+
logger.info(f"Iter {iteration}, Queue: {queue_size}, Best: {best_quality:.3f}, Time: {elapsed:.1f}s")
431433

432434
runtime = time.time() - start_time
433435
if self.verbose > 0:
434-
print(f"Search finished in {runtime:.2f}s after {iteration} iterations.")
435-
print(f"Best: {best_hypothesis.name} (Quality: {best_quality:.3f})")
436+
logger.info(f"Search finished in {runtime:.2f}s after {iteration} iterations.")
437+
logger.info(f"Best: {best_hypothesis.name} (Quality: {best_quality:.3f})")
436438

437439
return {
438440
'Prediction': best_hypothesis.name,

‎ontolearn/nces_trainer.py‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# -----------------------------------------------------------------------------
2424

2525
"""Trainer for NCES instances"""
26+
import logging
2627
import numpy as np
2728
import copy
2829
import torch
@@ -38,6 +39,8 @@
3839
from collections import defaultdict
3940
from ontolearn.data_struct import NCESDataset, ROCESDataset, TriplesDataset
4041

42+
logger = logging.getLogger(__name__)
43+
4144
def before_pad(arg):
4245
arg_temp = []
4346
for atm in arg:
@@ -111,8 +114,7 @@ def get_optimizer(self, model, emb_model=None, optimizer='Adam'): # pragma: no
111114
elif optimizer == 'RMSprop':
112115
return torch.optim.RMSprop(parameters, lr=self.learning_rate)
113116
else:
114-
raise ValueError
115-
print('Unsupported optimizer')
117+
raise ValueError(f"Unsupported optimizer: {optimizer}")
116118

117119
def get_data_idxs(self):
118120
data_idxs = [(self.synthesizer.triples_data.entity2idx.loc[t[0]].values[0],
@@ -135,10 +137,7 @@ def show_num_trainable_params(synthesizer):
135137
size_model = sum([p.numel() for p in synthesizer["model"].parameters()])
136138
if synthesizer["emb_model"]:
137139
size_emb_model = sum([p.numel() for p in synthesizer["emb_model"].parameters()])
138-
print("#"*30+"Trainable model size"+"#"*30)
139-
print("Synthesizer: ", size_model)
140-
print("Embedding model: ", size_emb_model)
141-
print("#"*30+"Trainable model size"+"#"*30)
140+
logger.info(f"Trainable model size: synthesizer: {size_model}, embedding model: {size_emb_model}")
142141

143142
def collate_batch(self, batch): # pragma: no cover
144143
pos_emb_list = []
@@ -203,19 +202,15 @@ def train(self, data, shuffle_examples=False, example_sizes=None,
203202
for model_name in self.synthesizer.model:
204203
self.show_num_trainable_params(self.synthesizer.model[model_name])
205204
if device.type == "cpu":
206-
print("Training on CPU, it may take long...")
205+
logger.info("Training on CPU, it may take long...")
207206
else:
208-
print("GPU available !")
209-
print()
210-
print("#"*80)
211-
print()
207+
logger.info("GPU available !")
212208
model = copy.deepcopy(self.synthesizer.model[model_name])
213209
if model["model"].name == "SetTransformer":
214210
start_message = "{}: {} ({} inducing points) starts training... \n".format(self.synthesizer.name, model["model"].name, model["model"].m)
215211
else:
216212
start_message = "{}: {} starts training... \n".format(self.synthesizer.name, model["model"].name)
217-
print(start_message)
218-
print("#"*80, "\n")
213+
logger.info(start_message.strip())
219214
desc = model["model"].name
220215
if model["model"].name != model_name:
221216
desc += "_" + model_name
@@ -303,9 +298,8 @@ def train(self, data, shuffle_examples=False, example_sizes=None,
303298
with open(self.storage_path+"/runtime/runtime"+"_"+desc+".json", "w") as file:
304299
json.dump(runtime_info, file, indent=3)
305300
results_dict = dict()
306-
print("Top performance: loss: {:.4f}, soft accuracy: {:.2f}% ... "
307-
"hard accuracy: {:.2f}%".format(min(Train_loss), max(Train_acc['soft']), max(Train_acc['hard'])))
308-
print()
301+
logger.info("Top performance: loss: {:.4f}, soft accuracy: {:.2f}% ... "
302+
"hard accuracy: {:.2f}%".format(min(Train_loss), max(Train_acc['soft']), max(Train_acc['hard'])))
309303
results_dict.update({"Train Max Soft Acc": max(Train_acc['soft']), "Train Max Hard Acc": max(Train_acc['hard']),
310304
"Train Min Loss": min(Train_loss)})
311305

@@ -345,7 +339,7 @@ def train(self, data, shuffle_examples=False, example_sizes=None,
345339
"num_relations": self.synthesizer.num_relations,
346340
"kernel_size": self.synthesizer.kernel_size,
347341
"num_of_output_channels": self.synthesizer.num_of_output_channels}, f)
348-
print("{} saved".format(model["model"].name))
342+
logger.info("{} saved".format(model["model"].name))
349343
if not os.path.exists(self.storage_path+"/metrics/"):
350344
os.mkdir(self.storage_path+"/metrics/")
351345
with open(self.storage_path+"/metrics/"+"metrics_"+desc+".json", "w") as plot_file:

0 commit comments

Comments
 (0)