Repository: keras-team/autokeras

Issue:
Add logging to search.py
Current code prints the searched models to stdout. Instead redirect them to a log file that can be referred by user at a later point


Discussion:

Resolving patch:
diff --git a/autokeras/grid_search.py b/autokeras/grid_search.py
new file mode 100644
diff --git a/autokeras/search.py b/autokeras/search.py
--- a/autokeras/search.py
+++ b/autokeras/search.py
@@ -4,7 +4,10 @@
 import time
 import torch
 import torch.multiprocessing as mp
+import logging
 
+
+from datetime import datetime
 from autokeras.bayesian import BayesianOptimizer
 from autokeras.constant import Constant
 from autokeras.nn.model_trainer import ModelTrainer
@@ -84,6 +87,8 @@ def __init__(self, n_output_node, input_shape, path, metric, loss, generators, v
         if t_min is None:
             t_min = Constant.T_MIN
         self.bo = BayesianOptimizer(self, t_min, metric)
+        logging.basicConfig(filename=os.path.join(self.path,datetime.now().strftime('run_%d_%m_%Y : _%H_%M.log')),
+                            format='%(asctime)s - %(filename)s - %(message)s', level=logging.DEBUG)
 
     def load_model_by_id(self, model_id):
         return pickle_from_file(os.path.join(self.path, str(model_id) + '.graph'))
@@ -167,6 +172,7 @@ def search(self, train_data, test_data, timeout=60 * 60 * 24):
             test_data: An instance of Dataloader.
             timeout: An integer, time limit in seconds.
         """
+
         start_time = time.time()
         torch.cuda.empty_cache()
         if not self.history:
diff --git a/autokeras/utils.py b/autokeras/utils.py
--- a/autokeras/utils.py
+++ b/autokeras/utils.py
@@ -4,6 +4,7 @@
 import sys
 import tempfile
 import zipfile
+import logging
 
 import warnings
 import imageio
@@ -17,6 +18,7 @@
 from scipy.ndimage import zoom
 
 
+
 class NoImprovementError(Exception):
     def __init__(self, message):
         self.message = message
@@ -137,22 +139,21 @@ def download_file_with_extract(file_link, file_path, extract_path):
 def verbose_print(new_father_id, new_graph, new_model_id):
     """Print information about the operation performed on father model to obtain current model and father's id."""
     cell_size = [24, 49]
-    print('New Model Id', new_model_id)
+
+    logging.info('New Model Id - '+str(new_model_id))
     header = ['Father Model ID', 'Added Operation']
     line = '|'.join(str(x).center(cell_size[i]) for i, x in enumerate(header))
-    print('\n' + '+' + '-' * len(line) + '+')
-    print('|' + line + '|')
-    print('+' + '-' * len(line) + '+')
+    logging.info('\n' + '+' + '-' * len(line) + '+')
+    logging.info('|' + line + '|')
+    logging.info('+' + '-' * len(line) + '+')
     for i in range(len(new_graph.operation_history)):
         if i == len(new_graph.operation_history) // 2:
-            r = [new_father_id, ' '.join(str(item) for item in new_graph.operation_history[i])]
+            r = [str(new_father_id), ' '.join(str(item) for item in new_graph.operation_history[i])]
         else:
             r = [' ', ' '.join(str(item) for item in new_graph.operation_history[i])]
         line = '|'.join(str(x).center(cell_size[i]) for i, x in enumerate(r))
-        print('|' + line + '|')
-    print('+' + '-' * len(line) + '+')
-
-
+        logging.info('|' + line + '|')
+    logging.info('+' + '-' * len(line) + '+')
 
 def validate_xy(x_train, y_train):
     """Validate `x_train`'s type and the shape of `x_train`, `y_train`."""


Test patch: