Upload 2 files
Browse files
scripts/hpo_result_analyzer_universal.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ํฌํธ ๋ฐ ์๋ ์ค์น
|
| 2 |
+
import os
|
| 3 |
+
import subprocess
|
| 4 |
+
import sys
|
| 5 |
+
import logging
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import argparse
|
| 8 |
+
|
| 9 |
+
def install_package(package_name):
|
| 10 |
+
"""์ง์ ๋ ํจํค์ง๊ฐ ์์ผ๋ฉด pip๋ฅผ ์ฌ์ฉํ์ฌ ์ค์นํฉ๋๋ค."""
|
| 11 |
+
try:
|
| 12 |
+
__import__(package_name)
|
| 13 |
+
except ImportError:
|
| 14 |
+
logging.info(f"{package_name} ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฐพ์ ์ ์์ด ์ค์นํฉ๋๋ค...")
|
| 15 |
+
try:
|
| 16 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", package_name, "--quiet"])
|
| 17 |
+
logging.info(f"{package_name} ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น ์ฑ๊ณต.")
|
| 18 |
+
except subprocess.CalledProcessError as e:
|
| 19 |
+
logging.error(f"์ค๋ฅ: {package_name} ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น ์คํจ.")
|
| 20 |
+
sys.exit(f"{package_name} ์ค์น ์คํจ๋ก ์คํฌ๋ฆฝํธ๋ฅผ ์ข
๋ฃํฉ๋๋ค.")
|
| 21 |
+
|
| 22 |
+
# --- ํจํค์ง ์ค์น ---
|
| 23 |
+
required_packages = ["optuna", "pandas", "plotly"]
|
| 24 |
+
for pkg in required_packages:
|
| 25 |
+
install_package(pkg)
|
| 26 |
+
|
| 27 |
+
import optuna
|
| 28 |
+
|
| 29 |
+
# --- 1. ๊ฒฝ๋ก ๋ฐ ๋ก๊น
์ค์ ---
|
| 30 |
+
PROJECT_BASE_DIR = "/home/work/baro/sillok25060103"
|
| 31 |
+
LOG_DIR = os.path.join(PROJECT_BASE_DIR, "logs")
|
| 32 |
+
os.makedirs(LOG_DIR, exist_ok=True)
|
| 33 |
+
LOG_FILE_PATH = os.path.join(LOG_DIR, "hpo_universal_analysis.log")
|
| 34 |
+
|
| 35 |
+
logging.basicConfig(
|
| 36 |
+
level=logging.INFO,
|
| 37 |
+
format="%(asctime)s [%(levelname)s] %(message)s",
|
| 38 |
+
handlers=[
|
| 39 |
+
logging.FileHandler(LOG_FILE_PATH, mode='w', encoding='utf-8'),
|
| 40 |
+
logging.StreamHandler(sys.stdout)
|
| 41 |
+
]
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
def analyze_hpo_results(db_name, study_name, output_dir, file_prefix):
|
| 45 |
+
"""
|
| 46 |
+
์ง์ ๋ Optuna DB์ Study๋ฅผ ๋ถ์ํ๊ณ ์๊ฐํ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํฉ๋๋ค.
|
| 47 |
+
"""
|
| 48 |
+
logging.info("="*60)
|
| 49 |
+
logging.info(f"'{study_name}' ์ฐ๊ตฌ ๋ถ์ ์์")
|
| 50 |
+
logging.info(f"DB ํ์ผ: {db_name}")
|
| 51 |
+
logging.info("="*60)
|
| 52 |
+
|
| 53 |
+
db_path = os.path.join(PROJECT_BASE_DIR, "optuna_db", db_name)
|
| 54 |
+
storage_name = f"sqlite:///{db_path}"
|
| 55 |
+
|
| 56 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 57 |
+
logging.info(f"๋ถ์ ๊ฒฐ๊ณผ๋ฌผ(๊ทธ๋ํ) ์ ์ฅ ๊ฒฝ๋ก: {output_dir}")
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
study = optuna.load_study(study_name=study_name, storage=storage_name)
|
| 61 |
+
logging.info(f"์ฐ๊ตฌ๋ฅผ ์ฑ๊ณต์ ์ผ๋ก ๋ถ๋ฌ์์ต๋๋ค. (์ด Trial: {len(study.trials)})")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
logging.error(f"์ฐ๊ตฌ๋ฅผ ๋ถ๋ฌ์ค๋ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 64 |
+
return
|
| 65 |
+
|
| 66 |
+
# --- ๊ฒฐ๊ณผ ์์ฝ ํ
์ด๋ธ ์ถ๋ ฅ ---
|
| 67 |
+
logging.info("\n--- ์์ 5๊ฐ Trial ์์ฝ (eval_loss ๊ธฐ์ค) ---")
|
| 68 |
+
|
| 69 |
+
# *** ์์ ๋ ๋ถ๋ถ: DataFrame์ ์๋์ผ๋ก ์์ฑํ์ฌ ์์ ์ฑ ํ๋ณด ***
|
| 70 |
+
completed_trials = [t for t in study.trials if t.state == optuna.trial.TrialState.COMPLETE]
|
| 71 |
+
if not completed_trials:
|
| 72 |
+
logging.warning("์๋ฃ๋ Trial์ด ์์ต๋๋ค. ๋ถ์์ ์ข
๋ฃํฉ๋๋ค.")
|
| 73 |
+
return
|
| 74 |
+
|
| 75 |
+
records = []
|
| 76 |
+
for t in completed_trials:
|
| 77 |
+
# 3๋จ๊ณ HPO์ฒ๋ผ user_attrs์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ์ฅํ์ ๊ฒฝ์ฐ์ ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ ๋ชจ๋ ์ฒ๋ฆฌ
|
| 78 |
+
params = t.user_attrs.get('predefined_params', t.params)
|
| 79 |
+
record = {
|
| 80 |
+
'number': t.number,
|
| 81 |
+
'eval_loss': t.value,
|
| 82 |
+
'duration': t.duration,
|
| 83 |
+
'params': params
|
| 84 |
+
}
|
| 85 |
+
records.append(record)
|
| 86 |
+
|
| 87 |
+
df_results = pd.DataFrame(records).sort_values(by='eval_loss', ascending=True)
|
| 88 |
+
|
| 89 |
+
with pd.option_context('display.max_rows', 5, 'display.max_columns', None, 'display.width', 120, 'display.max_colwidth', None):
|
| 90 |
+
print(df_results.head())
|
| 91 |
+
|
| 92 |
+
# --- ์๊ฐํ ๊ทธ๋ํ ์์ฑ ๋ฐ ์ ์ฅ ---
|
| 93 |
+
logging.info("\n--- ๊ฒฐ๊ณผ ์๊ฐํ ๊ทธ๋ํ ์์ฑ ์ค ---")
|
| 94 |
+
try:
|
| 95 |
+
fig_importance = optuna.visualization.plot_param_importances(study)
|
| 96 |
+
importance_path = os.path.join(output_dir, f"{file_prefix}param_importances.html")
|
| 97 |
+
fig_importance.write_html(importance_path)
|
| 98 |
+
logging.info(f"1. ํ๋ผ๋ฏธํฐ ์ค์๋ ๊ทธ๋ํ ์ ์ฅ ์๋ฃ: {importance_path}")
|
| 99 |
+
|
| 100 |
+
fig_history = optuna.visualization.plot_optimization_history(study)
|
| 101 |
+
history_path = os.path.join(output_dir, f"{file_prefix}optimization_history.html")
|
| 102 |
+
fig_history.write_html(history_path)
|
| 103 |
+
logging.info(f"2. ์ต์ ํ ๊ณผ์ ๊ทธ๋ํ ์ ์ฅ ์๋ฃ: {history_path}")
|
| 104 |
+
|
| 105 |
+
fig_slice = optuna.visualization.plot_slice(study)
|
| 106 |
+
slice_path = os.path.join(output_dir, f"{file_prefix}slice_plot.html")
|
| 107 |
+
fig_slice.write_html(slice_path)
|
| 108 |
+
logging.info(f"3. ๊ฐ๋ณ ํ๋ผ๋ฏธํฐ ๊ด๊ณ๋ ๊ทธ๋ํ ์ ์ฅ ์๋ฃ: {slice_path}")
|
| 109 |
+
except Exception as e:
|
| 110 |
+
logging.error(f"์๊ฐํ ๊ทธ๋ํ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {e}")
|
| 111 |
+
|
| 112 |
+
logging.info(f"\n'{study_name}' ์ฐ๊ตฌ ๋ถ์ ์๋ฃ.\n")
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
if __name__ == '__main__':
|
| 116 |
+
# ํฐ๋ฏธ๋์์ ์คํํ ๋ ์ธ์(argument)๋ฅผ ๋ฐ์ ์ฒ๋ฆฌํฉ๋๋ค.
|
| 117 |
+
parser = argparse.ArgumentParser(description="Optuna HPO ๊ฒฐ๊ณผ๋ฅผ ๋ถ์ํ๊ณ ์๊ฐํํฉ๋๋ค.")
|
| 118 |
+
parser.add_argument("--db_name", type=str, required=True, help="๋ถ์ํ Optuna DB ํ์ผ๋ช
(optuna_db ํด๋ ๋ด ์์น)")
|
| 119 |
+
parser.add_argument("--study_name", type=str, required=True, help="๋ถ์ํ Study์ ์ด๋ฆ")
|
| 120 |
+
parser.add_argument("--output_dir", type=str, default=os.path.join(PROJECT_BASE_DIR, "hpo_analysis_results"), help="์๊ฐํ ๊ฒฐ๊ณผ๋ฌผ์ ์ ์ฅํ ๋๋ ํ ๋ฆฌ")
|
| 121 |
+
parser.add_argument("--file_prefix", type=str, default="analysis_", help="์์ฑ๋ HTML ํ์ผ๋ช
์ ์ ๋์ฌ (์: 'stage2_')")
|
| 122 |
+
|
| 123 |
+
args = parser.parse_args()
|
| 124 |
+
|
| 125 |
+
analyze_hpo_results(
|
| 126 |
+
db_name=args.db_name,
|
| 127 |
+
study_name=args.study_name,
|
| 128 |
+
output_dir=args.output_dir,
|
| 129 |
+
file_prefix=args.file_prefix
|
| 130 |
+
)
|