
#1




*ANSWER* HW5 Q8/9  Help with R code
Hi, can anybody send me their R code for Q 8/9 or look at mine (appended below)? My gradient descent never runs for more than 1 epoch. I can't figure out what I'm doing wrong, I've pored over my code for so long I'm feeling crosseyed
# Learning from Data HW5 (items 8 and 9) # Logistic Regression with Stochastic Gradient Descent nIter = 100; nIn = 100; nOut = 10000; eta = 0.01; eOut = rep(NA, nIter); nEpochs = rep(NA, nIter); set.seed(123); for (i in (1:nIter)) { # Initialization of f(x) and of matching Insample nEpoch = nEpoch +1; }} # E(Eout) avgEOut = mean(eOut); # E(NEpoch) avgNEpoch = mean(nEpochs); Last edited by catherine; 05072013 at 11:55 PM. Reason: code formatting 
#2




Re: *ANSWER* HW5 Q8/9  Help with R code
Hi Catherine,
It would help if you can wrap your code up in the code tags. The tags can be found with the other formatting options when posting a message on the forum. This will make it easier for us to read and remove the sad face, although I thought it was appropriate. 
#3




Re: *ANSWER* HW5 Q8/9  Help with R code
Sorry, I hadn't noticed the advanced editing option. First time I'm posting something to the forum.

#4




Re: *ANSWER* HW5 Q8/9  Help with R code
Welcome aboard.
__________________
Where everyone thinks alike, no one thinks very much 
#5




Re: *ANSWER* HW5 Q8/9  Help with R code
I think the following bit of code has a bug. You are updating w and w0 every time through the loop and then comparing the two w's at the end. I believe you should be comparing the value of w from before the loop to the value of w at the end of the loop.
So the first time you have w=[0,0,0] then you loop through all points updating the weight. Then after that you compare w=[0,0,0] with weight you have at the end of the loop. Then you use the updated weight as the initial weight for the next time through the loop. Code:
for (j in permuted.ind) { w0 = w; v = (YIn[j] * XIn[j,]) / (1 + exp(YIn[j] * (XIn[j,] %*% w0))); w = w0  eta * v; } # Stop gradient descent as soon as delta(w, w0) < 0.01 if (sqrt(sum((w  w0)^2)) < 0.01) break() 
#6




Re: *ANSWER* HW5 Q8/9  Help with R code
Good spotting! Thank you so much arcticblue! I had simply misread the definition of the exit condition

Thread Tools  
Display Modes  

