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