| 
									
										
										
										
											2021-08-12 02:02:22 -04:00
										 |  |  | from flaml.tune.space import unflatten_hierarchical | 
					
						
							| 
									
										
										
										
											2020-12-14 23:10:03 -08:00
										 |  |  | from flaml import AutoML | 
					
						
							| 
									
										
										
										
											2021-10-08 16:09:43 -07:00
										 |  |  | from sklearn.datasets import fetch_california_housing | 
					
						
							| 
									
										
										
										
											2020-12-14 23:10:03 -08:00
										 |  |  | import os | 
					
						
							|  |  |  | import unittest | 
					
						
							|  |  |  | import logging | 
					
						
							|  |  |  | import tempfile | 
					
						
							|  |  |  | import io | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TestLogging(unittest.TestCase): | 
					
						
							|  |  |  |     def test_logging_level(self): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         from flaml import logger, logger_formatter | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with tempfile.TemporaryDirectory() as d: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             training_log = os.path.join(d, "training.log") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # Configure logging for the FLAML logger | 
					
						
							|  |  |  |             # and add a handler that outputs to a buffer. | 
					
						
							|  |  |  |             logger.setLevel(logging.INFO) | 
					
						
							|  |  |  |             buf = io.StringIO() | 
					
						
							|  |  |  |             ch = logging.StreamHandler(buf) | 
					
						
							|  |  |  |             ch.setFormatter(logger_formatter) | 
					
						
							|  |  |  |             logger.addHandler(ch) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # Run a simple job. | 
					
						
							| 
									
										
										
										
											2021-03-16 22:13:35 -07:00
										 |  |  |             automl = AutoML() | 
					
						
							| 
									
										
										
										
											2020-12-14 23:10:03 -08:00
										 |  |  |             automl_settings = { | 
					
						
							| 
									
										
										
										
											2021-02-05 21:41:14 -08:00
										 |  |  |                 "time_budget": 1, | 
					
						
							| 
									
										
										
										
											2021-10-08 16:09:43 -07:00
										 |  |  |                 "metric": "rmse", | 
					
						
							|  |  |  |                 "task": "regression", | 
					
						
							| 
									
										
										
										
											2020-12-14 23:10:03 -08:00
										 |  |  |                 "log_file_name": training_log, | 
					
						
							|  |  |  |                 "log_training_metric": True, | 
					
						
							| 
									
										
										
										
											2021-02-05 21:41:14 -08:00
										 |  |  |                 "n_jobs": 1, | 
					
						
							| 
									
										
										
										
											2021-03-16 22:13:35 -07:00
										 |  |  |                 "model_history": True, | 
					
						
							| 
									
										
										
										
											2021-08-26 13:45:13 -07:00
										 |  |  |                 "keep_search_state": True, | 
					
						
							| 
									
										
										
										
											2021-07-20 17:00:44 -07:00
										 |  |  |                 "learner_selector": "roundrobin", | 
					
						
							| 
									
										
										
										
											2020-12-14 23:10:03 -08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-10-08 16:09:43 -07:00
										 |  |  |             X_train, y_train = fetch_california_housing(return_X_y=True) | 
					
						
							| 
									
										
										
										
											2020-12-15 08:10:43 -08:00
										 |  |  |             n = len(y_train) >> 1 | 
					
						
							| 
									
										
										
										
											2021-09-04 01:42:21 -07:00
										 |  |  |             print(automl.model, automl.classes_, automl.predict(X_train)) | 
					
						
							| 
									
										
										
										
											2021-10-08 16:09:43 -07:00
										 |  |  |             automl.fit( | 
					
						
							|  |  |  |                 X_train=X_train[:n], | 
					
						
							|  |  |  |                 y_train=y_train[:n], | 
					
						
							|  |  |  |                 X_val=X_train[n:], | 
					
						
							|  |  |  |                 y_val=y_train[n:], | 
					
						
							|  |  |  |                 **automl_settings | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2021-08-02 19:10:26 -04:00
										 |  |  |             logger.info(automl.search_space) | 
					
						
							|  |  |  |             logger.info(automl.low_cost_partial_config) | 
					
						
							| 
									
										
										
										
											2021-08-23 19:36:51 -04:00
										 |  |  |             logger.info(automl.points_to_evaluate) | 
					
						
							| 
									
										
										
										
											2021-08-12 02:02:22 -04:00
										 |  |  |             logger.info(automl.cat_hp_cost) | 
					
						
							| 
									
										
										
										
											2021-08-02 19:10:26 -04:00
										 |  |  |             import optuna as ot | 
					
						
							| 
									
										
										
										
											2021-10-08 16:09:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-02 19:10:26 -04:00
										 |  |  |             study = ot.create_study() | 
					
						
							| 
									
										
										
										
											2021-08-12 02:02:22 -04:00
										 |  |  |             from flaml.tune.space import define_by_run_func, add_cost_to_space | 
					
						
							| 
									
										
										
										
											2021-10-08 16:09:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 02:02:22 -04:00
										 |  |  |             sample = define_by_run_func(study.ask(), automl.search_space) | 
					
						
							|  |  |  |             logger.info(sample) | 
					
						
							|  |  |  |             logger.info(unflatten_hierarchical(sample, automl.search_space)) | 
					
						
							|  |  |  |             add_cost_to_space( | 
					
						
							| 
									
										
										
										
											2021-10-08 16:09:43 -07:00
										 |  |  |                 automl.search_space, automl.low_cost_partial_config, automl.cat_hp_cost | 
					
						
							| 
									
										
										
										
											2021-08-12 02:02:22 -04:00
										 |  |  |             ) | 
					
						
							|  |  |  |             logger.info(automl.search_space["ml"].categories) | 
					
						
							| 
									
										
										
										
											2021-10-15 21:36:42 -07:00
										 |  |  |             if automl.best_config: | 
					
						
							|  |  |  |                 config = automl.best_config.copy() | 
					
						
							|  |  |  |                 config["learner"] = automl.best_estimator | 
					
						
							|  |  |  |                 automl.trainable({"ml": config}) | 
					
						
							| 
									
										
										
										
											2021-09-01 16:25:04 -07:00
										 |  |  |             from flaml import tune, BlendSearch | 
					
						
							| 
									
										
										
										
											2021-08-23 19:36:51 -04:00
										 |  |  |             from flaml.automl import size | 
					
						
							|  |  |  |             from functools import partial | 
					
						
							| 
									
										
										
										
											2021-10-08 16:09:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-15 21:36:42 -07:00
										 |  |  |             low_cost_partial_config = automl.low_cost_partial_config | 
					
						
							| 
									
										
										
										
											2021-09-01 16:25:04 -07:00
										 |  |  |             search_alg = BlendSearch( | 
					
						
							| 
									
										
										
										
											2021-10-08 16:09:43 -07:00
										 |  |  |                 metric="val_loss", | 
					
						
							|  |  |  |                 mode="min", | 
					
						
							| 
									
										
										
										
											2021-08-12 02:02:22 -04:00
										 |  |  |                 space=automl.search_space, | 
					
						
							| 
									
										
										
										
											2021-10-15 21:36:42 -07:00
										 |  |  |                 low_cost_partial_config=low_cost_partial_config, | 
					
						
							| 
									
										
										
										
											2021-08-23 19:36:51 -04:00
										 |  |  |                 points_to_evaluate=automl.points_to_evaluate, | 
					
						
							| 
									
										
										
										
											2021-08-12 02:02:22 -04:00
										 |  |  |                 cat_hp_cost=automl.cat_hp_cost, | 
					
						
							| 
									
										
										
										
											2021-12-04 21:52:20 -05:00
										 |  |  |                 resource_attr=automl.resource_attr, | 
					
						
							| 
									
										
										
										
											2021-08-12 02:02:22 -04:00
										 |  |  |                 min_resource=automl.min_resource, | 
					
						
							|  |  |  |                 max_resource=automl.max_resource, | 
					
						
							| 
									
										
										
										
											2021-10-08 16:09:43 -07:00
										 |  |  |                 config_constraints=[ | 
					
						
							|  |  |  |                     (partial(size, automl._state), "<=", automl._mem_thres) | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |                 metric_constraints=automl.metric_constraints, | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2021-08-12 02:02:22 -04:00
										 |  |  |             analysis = tune.run( | 
					
						
							| 
									
										
										
										
											2021-10-08 16:09:43 -07:00
										 |  |  |                 automl.trainable, | 
					
						
							|  |  |  |                 search_alg=search_alg,  # verbose=2, | 
					
						
							|  |  |  |                 time_budget_s=1, | 
					
						
							|  |  |  |                 num_samples=-1, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |             print(min(trial.last_result["val_loss"] for trial in analysis.trials)) | 
					
						
							|  |  |  |             config = analysis.trials[-1].last_result["config"]["ml"] | 
					
						
							|  |  |  |             automl._state._train_with_config(config["learner"], config) | 
					
						
							| 
									
										
										
										
											2021-10-15 21:36:42 -07:00
										 |  |  |             for _ in range(3): | 
					
						
							|  |  |  |                 print( | 
					
						
							|  |  |  |                     search_alg._ls.complete_config( | 
					
						
							|  |  |  |                         low_cost_partial_config, | 
					
						
							|  |  |  |                         search_alg._ls_bound_min, | 
					
						
							|  |  |  |                         search_alg._ls_bound_max, | 
					
						
							|  |  |  |                     ) | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2020-12-14 23:10:03 -08:00
										 |  |  |             # Check if the log buffer is populated. | 
					
						
							|  |  |  |             self.assertTrue(len(buf.getvalue()) > 0) | 
					
						
							| 
									
										
										
										
											2021-03-16 22:13:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         import pickle | 
					
						
							| 
									
										
										
										
											2021-10-08 16:09:43 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         with open("automl.pkl", "wb") as f: | 
					
						
							| 
									
										
										
										
											2021-03-16 22:13:35 -07:00
										 |  |  |             pickle.dump(automl, f, pickle.HIGHEST_PROTOCOL) | 
					
						
							| 
									
										
										
										
											2021-04-08 09:29:55 -07:00
										 |  |  |         print(automl.__version__) | 
					
						
							| 
									
										
										
										
											2021-11-03 19:08:23 -07:00
										 |  |  |         pred1 = automl.predict(X_train) | 
					
						
							|  |  |  |         with open("automl.pkl", "rb") as f: | 
					
						
							|  |  |  |             automl = pickle.load(f) | 
					
						
							|  |  |  |         pred2 = automl.predict(X_train) | 
					
						
							|  |  |  |         delta = pred1 - pred2 | 
					
						
							|  |  |  |         assert max(delta) == 0 and min(delta) == 0 | 
					
						
							| 
									
										
										
										
											2021-12-04 18:27:38 +00:00
										 |  |  |         automl.save_best_config("test/housing.json") |