 LFD Book Forum Evaluating a gradient function with vectors
 User Name Remember Me? Password
 Register FAQ Calendar Mark Forums Read

 Thread Tools Display Modes
#1
 kurts Invited Guest Join Date: Apr 2012 Location: Portland, OR Posts: 70 Evaluating a gradient function with vectors

Let's say we are using SGD with a gradient function
-(yn*xn)/(1 + e^(yn*w*xn)) where xn and w are 3-element vectors.

When I evaluate this function, can I evaluate it 3 times, once for each corresponding x[i] and w[i], and thus get a 3-element gradient vector, then update w from that vector?
Something like:

Code:
double gradient(double yn, double xn, double wn) {
double num = -1.0*yn*xn;
double denom = 1 + exp(yn*wn*xn);
return num / denom;
}

double g0 = gradient(yn, 1.0, w0);
double g1 = gradient(yn, x1n, w1);
double g2 = gradient(yn, x2n, w2);
w0 = w0 - eta*g0;
w1 = w1 - eta*g1;
w2 = w2 - eta*g2;
I'm not entirely confident that this is correct.
#2
 holland Junior Member Join Date: Apr 2012 Posts: 3 Re: Evaluating a gradient function with vectors

I think your approach is wrong for the denominator. My understanding is that w*x is a inner product that makes a scalar, so the only vector part of the gradient is the yn*xn on the top of the fraction.

(In particular, all 3 w and x terms would be used in the exponential in the denominator for each of the 3 values in the gradient. But your numerator would use the 3 different x's for the 3 different terms.)
#3
 kkkkk Invited Guest Join Date: Mar 2012 Posts: 71 Re: Evaluating a gradient function with vectors

Basically you have to calculate the partial derivative of the error function with respect to each dimension of w vector.
Then update each dimension of w using its partial derivative and the original w vector, and also the -eta.

This will help:
http://www.youtube.com/watch?v=U7HQ_...hannel&list=UL
#4
 kurts Invited Guest Join Date: Apr 2012 Location: Portland, OR Posts: 70 Re: Evaluating a gradient function with vectors

Holland, I think you are right. I thought the same thing, and I ended up trying it with the whole dot product in the denominator, and I am getting a much better final set of w values.

Instead of just plugging in individual values in the exponential, I go ahead and calculate the whole dot product and use the same denominator for each element of the gradient.

Code:
double altgradient(double yn, double xn, double wtx) {
double num = -1.0*yn*xn;
double denom = 1 + exp(yn*wtx);
return num / denom;
}

double calcWTX(double x1, double x2, double w0, double w1, double w2) {
double sum = w0 + w1*x1 + w2*x2;
return sum;
}

double wtx = calcWTX(x1n, x2n, w0, w1, w2);
double g0 = altgradient(yn, 1.0, wtx);
double g1 = altgradient(yn, x1n, wtx);
double g2 = altgradient(yn, x2n, wtx);
This is giving me a much better approximation of f. However, it does increase the number of steps by quite a bit.

Thanks!
#5
 sakumar Member Join Date: Apr 2012 Posts: 40 Re: Evaluating a gradient function with vectors

I realized that the numerator term (y_n*x_n) is a size 3 x n matrix that doesn't change for a particular data-set. So I evaluate it before I start the logistic regression loop. Inside the loop, the denominator has to be evaluated because w is changing each time.

 Thread Tools Show Printable Version Email this Page Display Modes Linear Mode Switch to Hybrid Mode Switch to Threaded Mode Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home General     General Discussion of Machine Learning     Free Additional Material         Dynamic e-Chapters         Dynamic e-Appendices Course Discussions     Online LFD course         General comments on the course         Homework 1         Homework 2         Homework 3         Homework 4         Homework 5         Homework 6         Homework 7         Homework 8         The Final         Create New Homework Problems Book Feedback - Learning From Data     General comments on the book     Chapter 1 - The Learning Problem     Chapter 2 - Training versus Testing     Chapter 3 - The Linear Model     Chapter 4 - Overfitting     Chapter 5 - Three Learning Principles     e-Chapter 6 - Similarity Based Methods     e-Chapter 7 - Neural Networks     e-Chapter 8 - Support Vector Machines     e-Chapter 9 - Learning Aides     Appendix and Notation     e-Appendices

All times are GMT -7. The time now is 06:27 PM.

 Contact Us - LFD Book - Top

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd. The contents of this forum are to be used ONLY by readers of the Learning From Data book by Yaser S. Abu-Mostafa, Malik Magdon-Ismail, and Hsuan-Tien Lin, and participants in the Learning From Data MOOC by Yaser S. Abu-Mostafa. No part of these contents is to be communicated or made accessible to ANY other person or entity.