1+ """
2+ #dp : 1, 중복 , 최적 부분 -> memorize
3+ goal :최대 이익
4+ input : time , price , 0
5+
6+ #1. 역순으로 N ~ 1 (퇴사일 N+1)
7+ for i in range(N , 0 , -1)
8+ #2-1 상담 가능 여부 (1)
9+ if i + T_i <= N 이면 -> 가능
10+ if not -> false (0)
11+
12+ # 2-2 최대 효율
13+ 리스트 dp :
14+ dp[i] = 현재 price + 바로 담 상담 price VS 내일 상담 price
15+ dp[i] = max(P[i] + dp[i+T_i] , P[i+1])
16+ """
17+
18+
19+
20+ import sys
21+ n = int (sys .stdin .readline ())
22+ schedules = [[]]
23+ for i in range (n ) :
24+ t , p = map (int , sys .stdin .readline ().split ())
25+ schedules .append ([t ,p ])
26+
27+ check = 0
28+
29+ dp = [0 for _ in range (n + 2 )]
30+
31+ for d in range (n ,0 , - 1 ) :
32+ t_i , p_i = schedules [d ]
33+ #1. 상담 가능여부
34+ if d + t_i > n + 1 : # 상담 불가능
35+ dp [d ] = dp [d + 1 ]
36+ else : #상담 가능
37+ dp [d ] = max (p_i + dp [d + t_i ] , dp [d + 1 ] )
38+
39+ print (dp [1 ])
40+
41+
42+ def train_epoch_boosting (self , model , previous_model = None ):
43+ '''
44+ 부스팅을 적용한 학습 (1 에폭)
45+ '''
46+ model .train ()
47+ total_loss = 0.0
48+ acc_cum = 0
49+ progress_bar = tqdm (self .train_loader , desc = 'Training with Boosting' )
50+ print (len (self .models ), 'Exists' if previous_model is not None else 'None' ) # 나중에 지울 부분
51+ for i ,(images , targets ) in enumerate (progress_bar ):
52+ images , targets = images .to (self .device ), targets .to (self .device )
53+ self .optimizer .zero_grad ()
54+
55+ # 현재 모델 예측
56+ print (f"Image.shape: { images .shape } " )
57+ print (f"targets.shape: { targets .shape } " )
58+ outputs = model (images )
59+ acc_cum += self .accuracy (outputs , targets )
60+ # 틀린 예측에 대한 가중치 적용wh
61+ if previous_model is not None : # 이전 모델이 존재하는 경우. 즉, Base 모델이 아닌 경우에 대해서만 Penalty 계산 후, 적용
62+ previous_model .eval ()
63+ with torch .no_grad ():
64+ prev_outputs = previous_model (images )
65+ print ('이전 모델의 정확도:' ,self .accuracy (prev_outputs , targets ))
66+ print ('현재 모델의 정확도:' , self .accuracy (outputs , targets ))
67+ # penalty_weights = self.boost_weights(prev_outputs, targets)
68+ loss = self .loss_fn (outputs , targets ) # * penalty_weights.to(self.device)
69+ # loss = loss.mean()
70+ else :
71+ loss = self .loss_fn (outputs , targets )
72+
73+
74+ loss .backward ()
75+ self .optimizer .step ()
76+ self .scheduler .step ()
77+ total_loss += loss .item ()
78+ progress_bar .set_postfix (loss = loss .item (), acc = acc_cum / (i + 1 ))
0 commit comments