Docsity

Homework with Solutions - Computer Vision | CAP 5415, Assignments of Computer Science

Toggle sidebar
bg1
Subhabrata Bhattacharya CAP 5415 HW PS02
1. Problem 1: The log-loss function is defined as :
L=
N
X
i=1
log(1 + e−li<xi,θ> ) (1)
We are required to compute Lfor all data-points provided in our training dataset. The matlab code to calculate
the log-loss function is attached.
1function L = c a l c u l a t e l o g l o s s ( p ts , l ab e l s , t he t a )
2% CALCULATE LOG LOSS −F u nc ti o n t o c a l c u l a t e th e l o g −l i k e l i h o o d o f th e d at a
3% s d e s cr i b e d by pt s , l a b e l s and t he t a . pt s a r e t he d i f f e r e n t va l u es o f x
4% a nd y fro m t h e tr a in in g s a m p l e s . l a b e l s de n o te th e d i r e c t io n wh e re t h e
5% da ta w oul d b e c l a s s i f i e d i n t o .
6 L = 0;
7 [N m] = si z e ( pt s ) ; %Number o f i t em s in th e t r a i n i n g s e t
8for i c n t = 1 : N
9 L = L + lo g (1 + e xp (−l a b e l s ( i cn t )
*
( p t s ( i c n t , : )
*
t he ta ) ) ) ;
10 en d
Listing 1: Matlab script for the Problem 1
We use the load command to populate the variables containing xiand li. These correspond to pts and labels.
We set different values for theta as inputs for the function and the outputs we get are approximately close to the
expected value.
theta = [0;0;1];
l = calculate_log_loss(pts,labels,theta);
l= 81.3262
theta = [1;2;2];
l = calculate_log_loss(pts,labels,theta);
l = 36.4575
2. Problem 2: We have,
f(x, y) =(x−2y)2+ (y−60)2(2)
∂f (x, y)
∂x =2(x−2y) [Taking partial derivatives wrt x]
∂f (x, y)
∂y =−4(x−2y) + 2(y−60) [Taking partial derivatives wrt y]
This could also be represented by matrix equations as follows:
∇x=2−2 0
x
y
1
and∇y=−4 10 −120
x
y
1
3. Problem 3:The lines of code are enclosed as follows:
1function x l o g = g r a d d e s c ( x0 )
2% GRADDESC −
3global x l o g g l o g
4 s t o p fl a g = 0 ;
5 x=x0 ;
6 x l o g = [ ] ;
7 g l o g = [ ] ;
8
9 [ u , v ]= m es h gr id ( 1 : 2 5 6 , 1 : 2 5 6 ) ;
10 z = ( u−2
*
v ) . ˆ 2 + ( v −60 ) . ˆ 2 ;
11 n = 0 .1 6 ; %THIS I S THE STEP SIZE
1
pf3
pf4
pf5

Partial preview of the text

Download Homework with Solutions - Computer Vision | CAP 5415 and more Assignments Computer Science in PDF only on Docsity!

1. Problem 1: The log-loss function is defined as :

L =

∑^ N

i=

log(1 + e−li<xi,θ>) (1)

We are required to compute L for all data-points provided in our training dataset. The matlab code to calculate

the log-loss function is attached.

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

Listing 1: Matlab script for the Problem 1

We use the load command to populate the variables containing xi and li. These correspond to pts and labels.

We set different values for theta as inputs for the function and the outputs we get are approximately close to the

expected value.

theta = [0;0;1];

l = calculate_log_loss(pts,labels,theta);

l= 81.

theta = [1;2;2];

l = calculate_log_loss(pts,labels,theta);

l = 36.

2. Problem 2: We have,

f (x, y) =(x − 2 y)^2 + (y − 60)^2 (2)

∂f (x, y)

∂x

=2(x − 2 y) [Taking partial derivatives wrt x]

∂f (x, y)

∂y

= − 4(x − 2 y) + 2(y − 60) [Taking partial derivatives wrt y]

This could also be represented by matrix equations as follows:

∇x =

[

]

x

y

 and∇y =

[

]

x

y

3. Problem 3:The lines of code are enclosed as follows:

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

Listing 2: Matlab script for the Problem 3

The following set of figures visualize the output of our program and demonstrate how after every iteration, the

gradient descent function converges towards the solution.

4. Problem 4: In order to determine the gradient of L from (1), we are required to find its partial derivatives wrt θ.

∂L

∂L ∂θ 1 ∂L ∂θ 2 ∂L ∂θ 3

Partially differentiating wrt θ 1 , θ 2 , θ 3 , we get:

∂L

∑^ N

i=

1 + e−li<xi,^1 ,θ^1 >^

(−lixi, 1 )e−li<xi,^1 ,θ^1 >^ =

∑^ N

i=

−lixi, 1 e−li<xi,^1 ,θ^1 >

1 + e−li<xi,^1 ,θ^1 >

∂L

∑^ N

i=

1 + e−li<xi,^2 ,θ^2 >^

(−lixi, 2 )e−li<xi,^2 ,θ^2 >^ =

∑^ N

i=

−lixi, 2 e−li<xi,^2 ,θ^2 >

1 + e−li<xi,^2 ,θ^2 >

∂L

∑^ N

i=

1 + e−li<xi,^3 ,θ^3 >^

(−lixi, 3 )e−li<xi,^3 ,θ^3 >^ =

∑^ N

i=

−lixi, 3 e−li<xi,^3 ,θ^3 >

1 + e−li<xi,^3 ,θ^3 >

5. Problem 5: The following code is used to calculate the gradient of log loss function as specified in Problem 1. A

vector corresponding to the number of elements in the vector θ is returned.

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 ) ;

Following are the results for using line parameters θ = [1, 1 , 1]:

>> theta = [1;1;1];

>> x = calculate_gradient_log_loss(pts,labels,theta)

x =

The complete code with the block to update the line parameters to calculate the value of the log-loss function for

the desired number of iterations:

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

Listing 4: Matlab script visualizing logistic regression

The results depict that after every iteration, we converge to a value close to 1. 6 e1 as stated in the problem.

logistic_regression(pts, labels)

L:2.041555e+

L:1.231060e+

L:7.420995e+

L:5.357203e+

L:4.309031e+

L:3.671882e+

L:1.552061e+

L:1.550827e+

The corresponding visualization for the above program :

(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

Figure 2: Output captured in various steps of execution of the Logistic Regression script