Thread: *ANSWER* q8/9 View Single Post
#4
 apbarraza Junior Member Join Date: Jan 2013 Posts: 4 Re: * answer * q8/9

I think I fixed the problem. I was confused and generated random points for each epoch and what was really required was a permutation over the original training points.
This is my fix:
Code:
```function [N, theta, Eout] = trainLogisticRegression(eta, mfunc, bfunc, numPointsPerEpoch)
% Initialize some useful values
N=0;
theta = zeros(3, 1);
theta_last =  theta + 1;

%Generate training points
[X, y]  = getRandomPoints(numPointsPerEpoch, mfunc, bfunc);
% Add intercept term
X = [ones(numPointsPerEpoch, 1) X];

while ((abs(theta-theta_last)>0.01)==1), %Iterate until convergence
N = N +1;
theta_last = theta;
%Permutation of training points
perm = randperm(numPointsPerEpoch);
%Gradient Descent
for i = 1:numPointsPerEpoch
e = y(perm(i)).*X(perm(i), :)./(1+exp(y(perm(i))*(theta'*X(perm(i),:)')));
%Adjusting parameters given gradient
theta = theta + eta*e';
end;
end

%New set of points to calculate the error
[X, y]  = getRandomPoints(numPointsPerEpoch, mfunc, bfunc);
% Add intercept term
X = [ones(numPointsPerEpoch, 1) X];

%Error measure
Eout1 = 1/numPointsPerEpoch*(sum(log( 1 + exp(-1*y.*(theta'*X')'))));
Eout2= 1/numPointsPerEpoch*sum((h(theta,X) - 1/2*(1+y)).^2);

Eout = [Eout1 Eout2];

end```
I am getting an average N of 37.100.
Eout1 is given me an average of 0.3 which is not the required answer
Eout2 is given me an average of 0.09 which is close to the final answer

I´m wondering if there is something still wrong with what I´m doing...
{"ft_lang_label":"__label__en","ft_lang_prob":0.7394842,"math_prob":0.99618226,"size":1440,"snap":"2021-04-2021-17","text_gpt3_token_len":441,"char_repetition_ratio":0.14972144,"word_repetition_ratio":0.12935324,"special_character_ratio":0.29930556,"punctuation_ratio":0.18466899,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99952257,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-14T02:53:05Z\",\"WARC-Record-ID\":\"<urn:uuid:1603919a-5765-4ff7-9bdb-e3f00bc8e42c>\",\"Content-Length\":\"14528\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f5b2fc55-6515-4ccd-a033-28344ec53969>\",\"WARC-Concurrent-To\":\"<urn:uuid:89fbb6cf-ee60-458d-9fe0-e2b6e7775af6>\",\"WARC-IP-Address\":\"131.215.134.70\",\"WARC-Target-URI\":\"http://book.caltech.edu/bookforum/showpost.php?s=02f5f611137a682f1529ef7e9edb0951&p=10760&postcount=4\",\"WARC-Payload-Digest\":\"sha1:DJSKIZH2WUPGL3ESCIEBJDFEC2KHFWPM\",\"WARC-Block-Digest\":\"sha1:EIFJ6GNGCHPBF6ZOVBCW2G4Q5524RRBH\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038076454.41_warc_CC-MAIN-20210414004149-20210414034149-00095.warc.gz\"}"}