



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




i=
1 f u n c t i o n L = c a l c u l a t e l o g l o s s ( pts , l a b e l s , t h e t a ) 2 % CALCULATE LOG LOSS − Function t o c a l c u l a t e t h e l o g −l i k e l i h o o d o f t h e data 3 % s d e s c r i b e d by pts , l a b e l s and t h e t a. p t s a r e t h e d i f f e r e n t v a l u e s o f x 4 % and y from t h e t r a i n i n g s a m p l e s. l a b e l s d e n o t e t h e d i r e c t i o n where t h e 5 % data would be c l a s s i f i e d i n t o. 6 L = 0 ; 7 [N m] = s i z e ( p t s ) ; %Number o f i t e m s i n t h e t r a i n i n g s e t 8 f o r i c n t = 1 : N 9 L = L + l o g ( 1 + exp(− l a b e l s ( i c n t ) * ( p t s ( i c n t , : ) * t h e t a ) ) ) ; 10 end
1 f u n c t i o n x l o g = g r a d d e s c ( x0 ) 2 % GRAD DESC − 3 g l o b a l x l o g g l o g 4 s t o p f l a g = 0 ; 5 x=x0 ; 6 x l o g = [ ] ; 7 g l o g = [ ] ; 8 9 [ u , v]= meshgrid ( 1 : 2 5 6 , 1 : 2 5 6 ) ; 10 z = ( u− 2 *v ). ˆ2 + ( v−60). ˆ 2 ; 11 n = 0. 1 6 ; %THIS IS THE STEP SIZE
12 c i t e r = 0 ; 13 w h i l e (¬ s t o p f l a g ) 14 f p r i n t f ( 1 , ' I t e r : %d\n ' , c i t e r ) ; 15 c i t e r=c i t e r +1; 16 %FILL IN CODE FOR COMPUTING THE GRADIENT 17 %g ( 1 , : ) s t o r e s g r a d i e n t a l o n g t h e h o r i z o n t a l d i r e c t i o n ( x ) and g ( 2 , : ) 18 %s t o r e s t h e g r a d i e n t a l o n g t h e v e r t i c a l d i r e c t i o n ( y ) 19 g ( 1 , : ) = 2 * ( x ( 1 ) − 2 * x ( 2 ) ) ; 20 g ( 2 , : ) = ( −4)( x ( 1 ) − 2 * x ( 2 ) ) + 2 * ( x ( 2 ) − 6 0 ) ; 21 22 x l o g = [ x l o g x ] ; 23 g l o g =[ g l o g −g n ] ; 24 %FILL IN CODE FOR UPDATING X 25 x = x + n( −1) g ; 26 27 i m a g e s c ( z ) ; 28 a x i s image 29 h o l d on 30 q u i v e r ( x l o g ( 1 , : ) ' , x l o g ( 2 , : ) ' , g l o g ( 1 , : ) ' , g l o g ( 2 , : ) ' , 0 , ' g ' ) ; 31 h o l d o f f ; 32 f = ( x (1) − 2 * x ( 2 ) ). ˆ2 + ( x (2) −60). ˆ 2 ; 33 f p r i n t f ( 1 , ' f :% e \n ' , f ) ; 34 i f ( c i t e r >500) 35 s t o p f l a g =1; 36 end 37 end
∂L ∂θ 1 ∂L ∂θ 2 ∂L ∂θ 3
i=
i=
i=
i=
i=
i=
1 f u n c t i o n g=c a l c u l a t e g r a d i e n t l o g l o s s ( pts , l a b e l s , t h e t a ) 2 % CALCULATE GRADIENT LOG LOSS − f o r each l i n e p a r a m e t e r s i n t h e t a , we would 3 % need t o c a l c u l a t e t h e g r a d i e n t. 4 % Number o f i t e m s i n t r a i n i n g d a t a s e t 5 N = l e n g t h ( p t s ) ; 6 % number o f l i n e p a r a m e t e r s 7 nt = l e n g t h ( t h e t a ) ; 8 % I n i t i a l i z i n g g r a d i e n t s f o r each o f t h e l i n e p a r a m e t e r s. 9 g = z e r o s ( nt , 1 ) ;
1 f u n c t i o n l o g i s t i c r e g r e s s i o n ( pts , l a b e l s ) 2 % LOGISTIC REGRESSION − 3 % 4 t h e t a 0 = [ 1 ; 0 ; 5 ] ; 5 t h e t a=t h e t a 0 ; 6 s t e p = 0. 0 1 2 5 ; 7 max num iter =50; 8 s t o p f l a g = 0 ; 9 c i t e r =1; 10 p o s i n d s = f i n d ( l a b e l s >0); 11 n e g i n d s = f i n d ( l a b e l s <0); 12 w h i l e (¬ s t o p f l a g ) 13 p l o t ( p t s ( p o s i n d s , 1 ) , p t s ( p o s i n d s , 2 ) , ' bx ' ) ; 14 h o l d on ; 15 p l o t ( p t s ( n e g i n d s , 1 ) , p t s ( n e g i n d s , 2 ) , ' r o ' ) ; 16 v=a x i s ; 17 l i n e p t 1 = ( t h e t a (3) − t h e t a ( 1 ) * v ( 1 ) ) / t h e t a ( 2 ) ; 18 l i n e p t 2 = ( t h e t a (3) − t h e t a ( 1 ) * v ( 2 ) ) / t h e t a ( 2 ) ; 19 p l o t ( [ v ( 1 ) v ( 2 ) ] , [ l i n e p t 1 l i n e p t 2 ] ) ; 20 h o l d o f f ; 21 a x i s ( v ) ; 22 t i t l e ( num2str ( c i t e r ) ) 23 drawnow 24 pause ( 0. 1 ) 25 g=c a l c u l a t e g r a d i e n t l o g l o s s ( pts , l a b e l s , t h e t a ) ; 26 L=c a l c u l a t e l o g l o s s ( pts , l a b e l s , t h e t a ) ; 27 f p r i n t f ( 1 , 'L:% e \n ' ,L ) ; 28 %INSERT CODE TO UDPATE THETA 29 t h e t a = t h e t a + s t e p ( −1) g ; 30 31 i f ( c i t e r ≥ max num iter ) 32 s t o p f l a g =1; 33 end 34 c i t e r=c i t e r +1; 35 end
(a) Initially (b) At iteration 1 (c) After 10 iterations
(d) 21 iterations (e) Result at 34 (f) 42 iterations
(g) Finally at 50th^ iteration iterations