-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathensemble_trainer.py
More file actions
1032 lines (843 loc) · 44.7 KB
/
ensemble_trainer.py
File metadata and controls
1032 lines (843 loc) · 44.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch
import numpy as np
import pandas as pd
import json
import time
import logging
import os
import psutil
from datetime import datetime, timedelta
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
from model import create_model, EnsembleModel
from train import train_model
from data_preprocessor import normalize_data, create_dataset, prepare_data, split_train_test, analyze_feature_importance
from utils import cache_result, optimize_memory_usage
from parallel_utils import optimize_gpu_tensor_ops
from tqdm import tqdm
import pickle
import matplotlib.pyplot as plt
import sys
from dotenv import load_dotenv
from price_constraint_validator import FuelPriceConstraintValidator
# .env 파일 로드
load_dotenv()
# 환경 변수 값 읽기
EPOCHS = int(os.getenv("EPOCHS", "1000"))
IMPORTANCE_EPOCHS = int(os.getenv("IMPORTANCE_EPOCHS", "500"))
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "32"))
PATIENCE = int(os.getenv("PATIENCE", "30"))
ENSEMBLE_SIZE = int(os.getenv("ENSEMBLE_SIZE", "5"))
MIN_DELTA = float(os.getenv("MIN_DELTA", "0.0001"))
LOOK_BACK = int(os.getenv("LOOK_BACK", "7"))
MAX_DAILY_CHANGE_PCT = float(os.getenv("MAX_DAILY_CHANGE_PCT", "1.0"))
MAX_WEEKLY_CHANGE_PCT = float(os.getenv("MAX_WEEKLY_CHANGE_PCT", "3.5"))
SMOOTHING_FACTOR = float(os.getenv("SMOOTHING_FACTOR", "0.6"))
class OilPriceEnsembleTrainer:
"""유가 예측을 위한 앙상블 모델 트레이너"""
def __init__(self, features_df, target_cols, look_back=None, test_size=0.2,
ensemble_size=None, use_gpu=True, cache_dir='model_cache'):
"""
초기화
Args:
features_df: 특성 데이터프레임
target_cols: 타겟 컬럼 리스트
look_back: 시계열 윈도우 크기
test_size: 테스트 데이터 비율
ensemble_size: 앙상블에 포함할 모델 수 (None이면 환경변수 사용)
use_gpu: GPU 사용 여부
cache_dir: 모델 캐시 디렉토리
"""
self.features_df = features_df
self.target_cols = target_cols
self.look_back = look_back if look_back is not None else LOOK_BACK
self.test_size = test_size
# 환경 변수의 기본값 사용 (None이면)
self.ensemble_size = ensemble_size if ensemble_size is not None else ENSEMBLE_SIZE
self.use_gpu = use_gpu
# 모델 캐시 디렉토리 생성
self.cache_dir = cache_dir
os.makedirs(self.cache_dir, exist_ok=True)
# 최적 디바이스 구성
self.device = torch.device("cuda" if torch.cuda.is_available() and use_gpu else "cpu")
self.use_mixed_precision = torch.cuda.is_available() and use_gpu
# 시스템 리소스에 맞게 최적화 (E5-2683v4 CPU 고려)
cpu_count = os.cpu_count() or 16
self.num_workers = min(cpu_count - 2, 32) # CPU 코어 여유분 남김
# 메모리 최적화 (128GB RAM 고려)
optimize_memory_usage()
# 로깅 설정
self.setup_logger()
self.logger.info(f"시스템 리소스: CPU {cpu_count}코어, GPU {torch.cuda.device_count()}개, 사용 워커: {self.num_workers}개")
self.logger.info(f"앙상블 모델 크기: {self.ensemble_size}, 시계열 윈도우: {self.look_back}일, 인내심: {PATIENCE}")
# 초기 데이터 준비
self.prepare_data()
# 모델 구성 - 다양한 앙상블 구성
self.model_configs = [
{
"model_type": "lstm",
"input_dim": self.X.shape[2],
"hidden_dim": 64,
"layer_dim": 2,
"output_dim": len(self.target_indices),
"dropout": 0.3
},
{
"model_type": "gru",
"input_dim": self.X.shape[2],
"hidden_dim": 64,
"layer_dim": 2,
"output_dim": len(self.target_indices),
"dropout": 0.3
},
{
"model_type": "cnn",
"input_dim": self.X.shape[2],
"hidden_dim": 64,
"output_dim": len(self.target_indices),
"sequence_length": self.look_back,
"dropout": 0.3
},
{
"model_type": "lstm", # 다양한 하이퍼파라미터로 추가 LSTM 모델
"input_dim": self.X.shape[2],
"hidden_dim": 128,
"layer_dim": 1,
"output_dim": len(self.target_indices),
"dropout": 0.2
},
{
"model_type": "gru", # 다른 구성의 GRU
"input_dim": self.X.shape[2],
"hidden_dim": 96,
"layer_dim": 3,
"output_dim": len(self.target_indices),
"dropout": 0.4
}
]
# 각 지역에 대한 앙상블 모델 저장
self.region_models = {}
# 타겟 스케일러 저장
self.target_scalers = {}
def setup_logger(self):
"""로깅 설정"""
self.logger = logging.getLogger("OilPriceEnsemble")
self.logger.setLevel(logging.INFO)
# 핸들러가 이미 있는지 확인
if not self.logger.handlers:
# 콘솔 핸들러
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# 파일 핸들러
log_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logs')
os.makedirs(log_dir, exist_ok=True)
fh = logging.FileHandler(f"{log_dir}/training_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log")
fh.setLevel(logging.INFO)
# 포맷 설정
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
fh.setFormatter(formatter)
self.logger.addHandler(ch)
self.logger.addHandler(fh)
def prepare_data(self):
"""데이터 전처리 및 준비"""
self.logger.info("데이터 전처리 시작...")
# 'date' 컬럼 제외 (날짜는 별도 처리)
if 'date' in self.features_df.columns:
self.dates = self.features_df['date'].copy()
features = self.features_df.drop(['date'], axis=1)
else:
self.dates = None
features = self.features_df.copy()
# 'area' 컬럼 처리
if 'area' in features.columns:
self.regions = features['area'].unique().tolist()
self.logger.info(f"발견된 지역: {self.regions}")
self.is_region_data = True
# area 컬럼을 제거
features = features.drop(['area'], axis=1)
else:
self.regions = ['National']
self.is_region_data = False
# 타겟 컬럼 식별
valid_targets = [col for col in self.target_cols if col in features.columns]
if not valid_targets:
raise ValueError("유효한 타겟 컬럼이 없습니다.")
self.target_indices = [features.columns.get_loc(col) for col in valid_targets]
self.feature_names = features.columns.tolist()
self.target_names = valid_targets
self.logger.info(f"타겟 변수: {self.target_names}")
self.logger.info(f"특성 수: {len(self.feature_names)}")
# 전체 데이터에 대한 정규화 및 시계열 데이터셋 생성
self.data_array = features.values.astype(float)
self.normalized_data, self.scaler = normalize_data(self.data_array)
# 시계열 데이터셋 생성
X, Y_full = create_dataset(self.normalized_data, self.look_back)
self.X, self.Y_full = prepare_data(X, Y_full)
# 타겟 변수만 선택
self.Y = self.Y_full[:, self.target_indices]
# 학습/테스트 분할
self.X_train, self.Y_train, self.X_test, self.Y_test = split_train_test(self.X, self.Y, self.test_size)
self.logger.info(f"데이터 분할 완료 - 학습 샘플: {len(self.X_train)}, 테스트 샘플: {len(self.X_test)}")
self.logger.info("데이터 전처리 완료")
@cache_result(expire_hours=48)
def analyze_variable_importance(self):
"""변수 중요도 분석"""
self.logger.info("변수 중요도 분석 시작...")
try:
# 기본 LSTM 모델 학습
input_dim = self.X_train.shape[2]
hidden_dim = 64
layer_dim = 2
output_dim = len(self.target_indices)
from model import LSTMModel
model = LSTMModel(input_dim, hidden_dim, layer_dim, output_dim)
# 모델을 적절한 디바이스로 이동
model = model.to(self.device)
# 학습 데이터를 디바이스로 이동
X_train_device = self.X_train.to(self.device)
Y_train_device = self.Y_train.to(self.device)
# .env에서 설정한 에포크 수 사용
self.logger.info(f"변수 중요도 분석을 위해 {IMPORTANCE_EPOCHS}번의 에포크로 모델 학습")
model, _ = train_model(model, X_train_device, Y_train_device,
epochs=IMPORTANCE_EPOCHS, batch_size=BATCH_SIZE, verbose=10)
# 중요: 분석 전에 CPU로 모델 이동
model = model.cpu()
# 중요: X_test도 CPU로 이동 후 numpy 변환
X_test_cpu = self.X_test.cpu().numpy() if torch.is_tensor(self.X_test) else self.X_test
# 특성 중요도 분석
importance_df = analyze_feature_importance(
model,
X_test_cpu,
self.feature_names,
self.target_names
)
# 상위 5개 특성 출력
self.logger.info(f"각 타겟에 대한 상위 5개 특성:")
for target in self.target_names:
top_features = importance_df[target].sort_values(ascending=False).head(5)
self.logger.info(f"{target}: {top_features.index.tolist()}")
return importance_df
except Exception as e:
self.logger.warning(f"특성 중요도 분석 중 오류 발생: {str(e)}. 대체 방법 사용")
import traceback
self.logger.warning(traceback.format_exc())
# 대체 방법: 모든 특성에 동일한 중요도 부여
return pd.DataFrame(
np.ones((len(self.feature_names), len(self.target_names))),
index=self.feature_names,
columns=self.target_names
)
def train_region_models(self, epochs=None, batch_size=None, patience=None, min_delta=None):
"""지역별 모델 학습"""
# 기본값이 None인 경우 .env 설정 사용
if epochs is None:
epochs = EPOCHS
if batch_size is None:
if self.use_gpu and torch.cuda.is_available():
batch_size = optimize_gpu_tensor_ops(mixed_precision=self.use_mixed_precision)
else:
# 128GB RAM 활용을 위한 CPU 배치 크기
total_ram = psutil.virtual_memory().total / (1024**3) # GB
batch_size = min(2048, int(total_ram / 16)) # RAM 크기에 맞춤
if patience is None:
patience = PATIENCE
if min_delta is None:
min_delta = MIN_DELTA
self.logger.info(f"학습 설정: 에포크 {epochs}, 배치 크기 {batch_size}, 인내심 {patience}, 최소개선율 {min_delta}")
# 단일 데이터셋 처리
if not self.is_region_data:
self.logger.info("지역 정보가 없어 전체 데이터로 단일 모델 학습 중...")
ensemble = self._train_models_for_region(self.X_train, self.Y_train, epochs, batch_size, patience, min_delta)
self.region_models['National'] = ensemble
self.target_scalers['National'] = self.scaler
return
# 지역별 모델 학습
self.logger.info(f"{len(self.regions)}개 지역에 대한 모델 학습 시작...")
for idx, region in enumerate(self.regions):
# 진행 상태 표시
progress = f"[{idx+1}/{len(self.regions)}]"
self.logger.info(f"{progress} 지역 {region} 모델 학습 시작...")
# 지역별 데이터 필터링
region_data = self.features_df[self.features_df['area'] == region].copy()
# 충분한 데이터가 있는지 확인
if len(region_data) < 30:
self.logger.warning(f"{progress} 지역 {region}의 데이터가 부족합니다. 건너뜁니다.")
continue
# 'date' 컬럼 제외
if 'date' in region_data.columns:
region_data = region_data.drop(['date'], axis=1)
# 'area' 컬럼 제외
if 'area' in region_data.columns:
region_data = region_data.drop(['area'], axis=1)
# 타겟 인덱스 조정
feature_names = region_data.columns.tolist()
target_indices = [feature_names.index(col) for col in self.target_names if col in feature_names]
# 데이터 정규화 및 시계열 데이터셋 생성
data_array = region_data.values.astype(float)
normalized_data, scaler = normalize_data(data_array)
X, Y_full = create_dataset(normalized_data, self.look_back)
X, Y_full = prepare_data(X, Y_full)
# 타겟 변수만 선택
Y = Y_full[:, target_indices]
# 학습/테스트 분할
X_train, Y_train, _, _ = split_train_test(X, Y, self.test_size)
# 모델 구성 조정
model_configs = []
for config in self.model_configs:
new_config = config.copy()
new_config['input_dim'] = X.shape[2]
new_config['output_dim'] = len(target_indices)
model_configs.append(new_config)
# 앙상블 모델 학습
self.logger.info(f"{progress} {region} 지역 앙상블 모델 학습 시작")
ensemble = self._train_models_for_region(X_train, Y_train, epochs, batch_size, patience, min_delta, model_configs)
self.logger.info(f"{progress} {region} 지역 앙상블 모델 학습 완료")
# 결과 저장
self.region_models[region] = ensemble
self.target_scalers[region] = scaler
# 메모리 정리
torch.cuda.empty_cache() if torch.cuda.is_available() else None
# 모델을 저장하여 OOM 방지
if idx % 5 == 0 and idx > 0:
self.save_models()
self.logger.info(f"{idx}번째 지역까지 모델 저장 완료")
self.logger.info(f"{len(self.region_models)}개 지역에 대한 모델 학습 완료")
def _train_models_for_region(self, X_train, Y_train, epochs=100, batch_size=32, patience=30, min_delta=0.0001, model_configs=None):
"""한 지역에 대한 앙상블 모델 학습"""
if model_configs is None:
model_configs = self.model_configs
models = []
weights = []
val_losses = []
# 검증 데이터 분할 (20%)
train_size = int(len(X_train) * 0.8)
X_train_data, X_val = X_train[:train_size], X_train[train_size:]
Y_train_data, Y_val = Y_train[:train_size], Y_train[train_size:]
# 안전한 배치 크기 설정 (GPU 메모리 과부하 방지)
if self.use_gpu and torch.cuda.is_available():
# GTX 1080 8GB에 더 안전한 배치 사이즈
batch_size = min(batch_size, 12)
self.logger.info(f"GPU 메모리 안전을 위해 배치 크기를 {batch_size}로 조정했습니다.")
# 다양한 모델 훈련 - 명시적으로 앙상블 크기를 제한
for i, config in enumerate(model_configs[:self.ensemble_size]):
model_type = config.get('model_type', 'lstm')
# 로그 메시지 수정: 현재 모델/총 앙상블 크기
self.logger.info(f"모델 {i+1}/{self.ensemble_size} ({model_type}) 훈련 중...")
try:
# 훈련 시작 전 GPU 메모리 정리
if torch.cuda.is_available():
torch.cuda.empty_cache()
# 모델 생성 및 디바이스 이동
model = create_model(**config)
model = model.to(self.device)
# 텐서 변환 및 디바이스 이동
if isinstance(X_train_data, torch.Tensor):
X_train_tensor = X_train_data.to(self.device)
else:
X_train_tensor = torch.FloatTensor(X_train_data).to(self.device)
if isinstance(Y_train_data, torch.Tensor):
Y_train_tensor = Y_train_data.to(self.device)
else:
Y_train_tensor = torch.FloatTensor(Y_train_data).to(self.device)
# 검증 데이터도 준비 (하지만 아직 디바이스로 이동하지 않음)
if isinstance(X_val, torch.Tensor):
X_val_cpu = X_val.cpu() # 일단 CPU에 보관
else:
X_val_cpu = torch.FloatTensor(X_val)
if isinstance(Y_val, torch.Tensor):
Y_val_cpu = Y_val.cpu() # 일단 CPU에 보관
else:
Y_val_cpu = torch.FloatTensor(Y_val)
# 모델 학습
model, history = train_model(
model,
X_train_tensor,
Y_train_tensor,
epochs=epochs,
batch_size=batch_size,
patience=patience,
min_delta=min_delta,
verbose=5
)
# 검증 손실 계산 - 메모리 관리를 위해 작은 배치로 처리
model.eval()
val_loss = 0.0
val_batch_size = 32 # 검증은 더 작은 배치로
n_batches = 0
with torch.no_grad():
for start_idx in range(0, len(X_val_cpu), val_batch_size):
end_idx = min(start_idx + val_batch_size, len(X_val_cpu))
# 현재 배치만 GPU로 이동
X_batch = X_val_cpu[start_idx:end_idx].to(self.device)
Y_batch = Y_val_cpu[start_idx:end_idx].to(self.device)
# 예측 및 손실 계산
outputs = model(X_batch)
batch_loss = torch.nn.MSELoss()(outputs, Y_batch).item()
# 누적
val_loss += batch_loss
n_batches += 1
# 배치 데이터 메모리 해제
del X_batch, Y_batch
torch.cuda.empty_cache() if torch.cuda.is_available() else None
# 평균 손실 계산
val_loss = val_loss / max(1, n_batches)
val_losses.append(val_loss)
# 검증 손실 기반 가중치 계산
weight = 1.0 / (val_loss + 1e-10) # 0으로 나누기 방지
weights.append(weight)
# CPU로 이동하여 메모리 절약
model = model.cpu()
models.append(model)
self.logger.info(f"모델 {i+1} 학습 완료: 최종 검증 손실 {val_loss:.6f}")
except Exception as e:
self.logger.error(f"모델 {model_type} 학습 중 오류 발생: {str(e)}")
import traceback
self.logger.error(traceback.format_exc())
# 오류 발생 시 다음 모델로 계속 진행
continue
finally:
# 메모리 정리
if torch.cuda.is_available():
torch.cuda.empty_cache()
# 학습된 모델이 없으면 오류 발생
if not models:
raise ValueError("모든 모델 학습에 실패했습니다.")
# 가중치 계산 방식 개선
if models:
# 지수 가중치 적용 대신 최근 실적 기반 가중치 계산
recent_weights = np.exp(-np.array(val_losses) * 2) # 지수 감쇠 강화
weights = recent_weights / recent_weights.sum() # 정규화
self.logger.info(f"모델 가중치: {weights.round(3)}")
# 앙상블 강화를 위한 부스팅 적용
ensemble = EnhancedEnsembleModel(models, weights, prediction_constraints={
'max_daily_change_pct': MAX_DAILY_CHANGE_PCT, # 환경 변수에서 설정
'max_weekly_change_pct': MAX_WEEKLY_CHANGE_PCT, # 환경 변수에서 설정
'smoothing_factor': SMOOTHING_FACTOR, # 환경 변수에서 설정
})
return ensemble
def predict_future(self, future_steps=7):
"""미래 예측 수행"""
self.logger.info(f"향후 {future_steps}일에 대한 예측 시작...")
# 마지막 날짜 확인
if self.dates is not None:
last_date = self.dates.iloc[-1]
future_dates = pd.date_range(start=last_date + timedelta(days=1), periods=future_steps)
else:
future_dates = [f"Day+{i+1}" for i in range(future_steps)]
predictions = {}
total_regions = len(self.region_models)
for idx, (region, model) in enumerate(self.region_models.items()):
# 진행 표시줄
progress_bar = f"[{idx+1}/{total_regions}]"
self.logger.info(f"{progress_bar} 지역 {region}에 대한 예측 수행 중...")
try:
# CPU에서 예측 수행
model = model.cpu()
# 마지막 시퀀스 데이터 가져오기
if self.is_region_data:
region_data = self.features_df[self.features_df['area'] == region].copy()
if 'date' in region_data.columns:
region_data = region_data.drop(['date'], axis=1)
if 'area' in region_data.columns:
region_data = region_data.drop(['area'], axis=1)
data_array = region_data.values.astype(float)
normalized_data, _ = normalize_data(data_array)
else:
normalized_data = self.normalized_data
# 마지막 시퀀스
last_sequence = normalized_data[-self.look_back:]
# 예측 수행
scaler = self.target_scalers[region]
current_sequence = last_sequence.copy()
region_predictions = []
# 단계별 예측
for step in range(future_steps):
# 현재 시퀀스로 예측
input_tensor = torch.FloatTensor(current_sequence).unsqueeze(0) # 배치 차원 추가
with torch.no_grad():
pred = model(input_tensor).detach().cpu().numpy()[0] # 배치 차원 제거
region_predictions.append(pred)
# 피드백 루프: 마지막 행 제거하고 예측값 추가
new_row = np.zeros((1, current_sequence.shape[1]))
if self.is_region_data:
target_indices = [region_data.columns.tolist().index(col) for col in self.target_names if col in region_data.columns]
for i, target_idx in enumerate(target_indices):
if i < len(pred):
new_row[0, target_idx] = pred[i]
else:
for i, target_idx in enumerate(self.target_indices):
if i < len(pred):
new_row[0, target_idx] = pred[i]
current_sequence = np.vstack([current_sequence[1:], new_row])
# 예측 결과를 원래 스케일로 역변환
original_scale_preds = []
for step_pred in region_predictions:
# 전체 피처 차원으로 확장
full_pred = np.zeros((1, scaler.n_features_in_))
if self.is_region_data:
target_indices = [region_data.columns.tolist().index(col) for col in self.target_names if col in region_data.columns]
for i, target_idx in enumerate(target_indices):
if i < len(step_pred):
full_pred[0, target_idx] = step_pred[i]
else:
for i, target_idx in enumerate(self.target_indices):
if i < len(step_pred):
full_pred[0, target_idx] = step_pred[i]
# 역변환
inverse_pred = scaler.inverse_transform(full_pred)[0]
# 타겟 값만 추출
if self.is_region_data:
target_indices = [region_data.columns.tolist().index(col) for col in self.target_names if col in region_data.columns]
original_scale_preds.append([inverse_pred[idx] for idx in target_indices])
else:
original_scale_preds.append([inverse_pred[idx] for idx in self.target_indices])
# 데이터프레임 생성
pred_df = pd.DataFrame(
original_scale_preds,
columns=self.target_names,
index=future_dates
)
# 예측 결과 후처리 및 합리성 확인
for col in pred_df.columns:
curr_vals = pred_df[col].values
for i in range(1, len(curr_vals)):
change_pct = abs((curr_vals[i] - curr_vals[i-1]) / curr_vals[i-1]) * 100
if change_pct > 10: # 10% 이상 급변
# 이전 값과 다음 값의 평균으로 보정
if i < len(curr_vals) - 1:
curr_vals[i] = (curr_vals[i-1] + curr_vals[i+1]) / 2
else:
curr_vals[i] = curr_vals[i-1] # 마지막 값은 이전 값으로
pred_df[col] = curr_vals
# 결과 저장
predictions[region] = pred_df
# 결과 요약 출력
summary_stats = pred_df.describe().loc[['mean', 'min', 'max']]
self.logger.info(f"{progress_bar} 지역 {region} 예측 완료")
# NumPy float64를 일반 float로 변환하여 로그 출력 - 소수점 1자리로 제한
mean_values = {k: round(float(v), 1) for k, v in dict(summary_stats.loc['mean']).items()}
self.logger.info(f"평균 가격: {mean_values}")
except Exception as e:
self.logger.error(f"지역 {region} 예측 중 오류 발생: {str(e)}")
import traceback
self.logger.error(traceback.format_exc())
# 예측 결과를 가져온 후 제약조건 적용하여 보정
validator = FuelPriceConstraintValidator(
max_daily_change_pct=MAX_DAILY_CHANGE_PCT,
max_weekly_change_pct=MAX_WEEKLY_CHANGE_PCT,
smoothing_factor=SMOOTHING_FACTOR,
price_relationships={
'premium_gasoline_to_gasoline': 200,
'gasoline_to_diesel': 120,
'diesel_to_kerosene': 50
}
)
# 각 지역별 예측 보정
for region, pred_df in predictions.items():
# 제약조건 적용하여 예측값 보정
adjusted_df = validator.validate_predictions(pred_df)
predictions[region] = adjusted_df
# 보정 후 통계 출력
summary_stats = adjusted_df.describe().loc[['mean', 'min', 'max']]
self.logger.info(f"[보정 후] {region} 지역 예측 통계:")
for stat, values in summary_stats.iterrows():
self.logger.info(f" - {stat}: {dict(values.round(1))}")
self.logger.info(f"총 {len(predictions)}개 지역에 대한 예측 완료")
return predictions
def save_models(self):
"""학습된 모델 저장"""
models_dir = os.path.join(self.cache_dir, 'trained_models')
os.makedirs(models_dir, exist_ok=True)
# 메타데이터 저장
metadata = {
'date_trained': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'ensemble_size': self.ensemble_size,
'look_back': self.look_back,
'target_names': self.target_names,
'regions': list(self.region_models.keys()),
'feature_names': self.feature_names
}
meta_path = os.path.join(models_dir, 'metadata.json')
with open(meta_path, 'w') as f:
json.dump(metadata, f, indent=2)
# 각 지역별 모델 저장
for region, model in self.region_models.items():
region_dir = os.path.join(models_dir, region.replace(' ', '_'))
os.makedirs(region_dir, exist_ok=True)
# 모델 저장
model_path = os.path.join(region_dir, 'ensemble_model.pkl')
with open(model_path, 'wb') as f:
pickle.dump(model, f)
# 스케일러 저장
scaler_path = os.path.join(region_dir, 'scaler.pkl')
with open(scaler_path, 'wb') as f:
pickle.dump(self.target_scalers[region], f)
self.logger.info(f"모든 모델이 {models_dir}에 저장되었습니다.")
def load_models(self):
"""저장된 모델 로드"""
models_dir = os.path.join(self.cache_dir, 'trained_models')
if not os.path.exists(models_dir):
self.logger.warning("저장된 모델을 찾을 수 없습니다.")
return False
# 모델 로드
loaded_regions = []
for region_dir in os.listdir(models_dir):
region_path = os.path.join(models_dir, region_dir)
if os.path.isdir(region_path) and not region_dir.startswith('.'):
region = region_dir.replace('_', ' ')
model_path = os.path.join(region_path, 'ensemble_model.pkl')
scaler_path = os.path.join(region_path, 'scaler.pkl')
if os.path.exists(model_path) and os.path.exists(scaler_path):
try:
with open(model_path, 'rb') as f:
self.region_models[region] = pickle.load(f)
with open(scaler_path, 'rb') as f:
self.target_scalers[region] = pickle.load(f)
loaded_regions.append(region)
except Exception as e:
self.logger.error(f"지역 {region} 모델 로드 중 오류: {str(e)}")
if loaded_regions:
self.logger.info(f"{len(loaded_regions)}개 지역의 모델을 로드했습니다: {', '.join(loaded_regions[:5])}..." if len(loaded_regions) > 5 else loaded_regions)
return True
else:
self.logger.warning("로드할 모델이 없습니다.")
return False
def plot_predictions(self, predictions, save_path=None):
"""예측 결과를 시각화"""
# 경고 메시지 숨기기
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="matplotlib")
# 기본 matplotlib 설정만 사용
import matplotlib as mpl
mpl.rcParams['axes.unicode_minus'] = False
for region, pred_df in predictions.items():
fig, ax = plt.subplots(figsize=(12, 6))
for col in pred_df.columns:
ax.plot(pred_df.index, pred_df[col], marker='o', linewidth=2, label=col)
# 영어로만 제목과 라벨 설정
ax.set_title(f"Future Oil Price Prediction - {region}", fontsize=15)
ax.set_xlabel("Date", fontsize=12)
ax.set_ylabel("Price (KRW)", fontsize=12)
ax.grid(True)
ax.legend()
plt.tight_layout()
if save_path:
region_save_path = os.path.join(save_path, f"{region.replace(' ', '_')}_prediction.png")
plt.savefig(region_save_path)
self.logger.info(f"{region} 예측 그래프 저장: {region_save_path}")
else:
plt.show()
plt.close()
def run_ensemble_prediction_pipeline(features_df, target_cols, look_back=3, future_steps=7,
ensemble_size=None, use_gpu=True, batch_size=None):
"""
앙상블 예측 파이프라인 실행
Args:
features_df: 특성 데이터프레임
target_cols: 타겟 컬럼 리스트
look_back: 시계열 윈도우 크기
future_steps: 예측할 미래 일 수
ensemble_size: 앙상블에 포함할 모델 수 (None이면 환경변수 사용)
use_gpu: GPU 사용 여부
batch_size: 학습 배치 크기 (None이면 자동 설정)
Returns:
지역별 예측 결과
"""
# 환경 변수에서 설정 가져오기 (None인 경우)
if ensemble_size is None:
ensemble_size = ENSEMBLE_SIZE
# 로거 설정
logger = logging.getLogger("EnsemblePipeline")
logger.setLevel(logging.INFO)
if not logger.handlers:
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
# 진행 상황 표시를 위한 상수 정의
TOTAL_STEPS = 5 # 전체 단계 수
current_step = 0
def show_progress(step_description, step=1):
nonlocal current_step
current_step += step
progress_bar = '=' * current_step + '>' + ' ' * (TOTAL_STEPS - current_step - 1)
logger.info(f"\n[{progress_bar}] {current_step}/{TOTAL_STEPS} {step_description}")
# 현재 시간 기록 (총 실행시간 계산용)
start_time = time.time()
# 앙상블 트레이너 초기화
trainer = OilPriceEnsembleTrainer(
features_df=features_df,
target_cols=target_cols,
look_back=look_back,
ensemble_size=ensemble_size,
use_gpu=use_gpu
)
show_progress("데이터 로드 및 전처리 완료")
# 저장된 모델이 있는지 확인하고 로드
if trainer.load_models():
logger.info("저장된 모델을 성공적으로 로드했습니다.")
show_progress("저장된 모델 로드", 3) # 3단계 건너뜀
else:
logger.info("저장된 모델을 찾을 수 없거나 로드하는데 실패했습니다. 새로 학습합니다.")
# 변수 중요도 분석
try:
importance_df = trainer.analyze_variable_importance()
logger.info("특성 중요도 분석 완료")
# 상위 5개 특성 출력
for target in target_cols:
if target in importance_df.columns:
top_features = importance_df[target].sort_values(ascending=False).head(5).index.tolist()
logger.info(f"{target}에 대한 상위 5개 특성: {', '.join(top_features)}")
except Exception as e:
logger.warning(f"특성 중요도 분석 중 오류 발생: {str(e)}. 모든 특성을 사용합니다.")
show_progress("특성 중요도 분석 완료")
# 지역별 모델 학습 시작 시간 기록
model_start_time = time.time()
logger.info("지역별 모델 학습 시작...")
# 지역별 모델 학습
trainer.train_region_models(epochs=EPOCHS, batch_size=batch_size, patience=PATIENCE)
# 학습 완료 시간 및 소요 시간 출력
model_end_time = time.time()
model_elapsed_time = model_end_time - model_start_time
hours, remainder = divmod(model_elapsed_time, 3600)
minutes, seconds = divmod(remainder, 60)
logger.info(f"모든 모델 학습 완료. 총 소요 시간: {int(hours)}시간 {int(minutes)}분 {seconds:.1f}초")
show_progress("모델 학습 완료")
# 학습된 모델 저장
logger.info("학습된 모델 저장 중...")
trainer.save_models()
logger.info("모델 저장 완료")
show_progress("모델 저장 완료")
# 미래 예측 수행
logger.info(f"향후 {future_steps}일에 대한 예측 시작...")
predictions = trainer.predict_future(future_steps=future_steps)
logger.info("예측 완료")
# 예측 결과 시각화 (기본적으로 비활성화)
try:
# 결과 저장 디렉토리 생성
images_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'prediction_images')
os.makedirs(images_dir, exist_ok=True)
# 예측 결과 시각화 및 저장
trainer.plot_predictions(predictions, save_path=images_dir)
logger.info(f"예측 시각화 결과가 {images_dir}에 저장되었습니다.")
except Exception as e:
logger.warning(f"예측 시각화 중 오류 발생: {str(e)}")
show_progress("예측 완료")
# 총 실행 시간 출력
total_time = time.time() - start_time
hours, remainder = divmod(total_time, 3600)
minutes, seconds = divmod(remainder, 60)
logger.info(f"전체 예측 파이프라인 완료. 총 소요 시간: {int(hours)}시간 {int(minutes)}분 {seconds:.1f}초")
# 결과 요약 출력
print_prediction_summary(predictions, future_steps)
return predictions
def print_prediction_summary(predictions, future_steps):
"""예측 결과 요약을 깔끔하게 출력합니다."""
logger = logging.getLogger("EnsemblePipeline")
logger.info("\n" + "="*50)
logger.info(f"향후 {future_steps}일 예측 결과 요약")
logger.info("="*50)
# 지역별로 예측 요약
for region, pred_df in predictions.items():
logger.info(f"\n[지역: {region}]")
# 유가 종류별 평균 가격 및 변동폭 출력
for col in pred_df.columns:
# NumPy float64를 일반 float로 변환
avg_price = float(pred_df[col].mean())
min_price = float(pred_df[col].min())
max_price = float(pred_df[col].max())
change = float(pred_df[col].iloc[-1] - pred_df[col].iloc[0])
change_pct = float((change / pred_df[col].iloc[0]) * 100 if pred_df[col].iloc[0] != 0 else 0)
logger.info(f" {col}: 평균 {avg_price:.1f}원, 변동폭 {change:+.1f}원 ({change_pct:+.1f}%)")
# 날짜별 상세 예측 결과 출력
logger.info("\n [일자별 예측 결과]")
for date, row in pred_df.iterrows():
date_str = date.strftime('%Y-%m-%d') if hasattr(date, 'strftime') else str(date)
logger.info(f" {date_str}:")
for col in pred_df.columns:
price = float(row[col]) # NumPy float64를 일반 float로 변환
logger.info(f" - {col}: {price:.1f}원")
logger.info("\n" + "="*50)
# 직접 실행 시 간단한 테스트 수행
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
try:
from data_loader import load_data_from_mongo
# 데이터 로드
logger.info("데이터 로딩 중...")
df = load_data_from_mongo()
if df.empty:
logger.error("데이터를 로드할 수 없습니다.")
exit(1)
logger.info(f"데이터 로드 완료: {df.shape[0]}개 레코드")
# 타겟 컬럼 설정
target_cols = ["gasoline", "premiumGasoline", "diesel", "kerosene"]
# 예측 파이프라인 실행
predictions = run_ensemble_prediction_pipeline(
df,
target_cols,
look_back=3,
future_steps=7,
ensemble_size=3,
use_gpu=torch.cuda.is_available()
)
logger.info("예측 완료!")
except Exception as e:
logger.error(f"오류 발생: {str(e)}", exc_info=True)
# 예측 결과 제약 설정을 위한 향상된 앙상블 모델 클래스
class EnhancedEnsembleModel(EnsembleModel):
def __init__(self, models, weights=None, prediction_constraints=None):
super(EnhancedEnsembleModel, self).__init__(models, weights)
self.constraints = prediction_constraints or {}
# 예측 마지막 값 저장용
self.last_predictions = None
def forward(self, x):
"""가중 앙상블 예측 수행 및 제약 적용"""
# 기본 앙상블 예측
ensemble_pred = super().forward(x)
# 직전 예측값이 있고 제약이 설정된 경우 보정 적용
if self.last_predictions is not None and self.constraints:
# 급격한 변화 제한
if 'max_daily_change_pct' in self.constraints:
max_change = self.constraints['max_daily_change_pct'] / 100
for i in range(ensemble_pred.size(0)):
for j in range(ensemble_pred.size(1)):
prev_val = self.last_predictions[j]
curr_val = ensemble_pred[i, j].item()
# 변화율 계산 및 제한
if prev_val != 0:
change_pct = (curr_val - prev_val) / prev_val
if abs(change_pct) > max_change:
direction = 1 if change_pct > 0 else -1
ensemble_pred[i, j] = prev_val * (1 + direction * max_change)
# 현재 값을 다음 호출을 위해 저장
if ensemble_pred.size(0) == 1: # 단일 예측인 경우만 저장
self.last_predictions = ensemble_pred[0].detach().clone()
return ensemble_pred
def predict_sequence(self, initial_sequence, steps, feature_indices=None):