-
Notifications
You must be signed in to change notification settings - Fork 2k
Using Parallel.For with LinearCoordinateDescent #396
Description
Hey there !
I understand that the LinearCoordinateDescent is not multithreaded and that's allright. I just want to understand why it is not working when called in a parallel environment.
When I'm using a parallel for to call "learn" on multiple instance of LinearCoordinateDescent I got a broken result compared to the same code called in a normal for loop. As if they were sharing variables.
example:
`
Parallel.For(0, 3, i =>
{
SupportVectorMachine mSvm;
LinearCoordinateDescent teacher = new LinearCoordinateDescent();
float WeigthNeg = 0.001f + i * 0.01f;
teacher.NegativeWeight = WeigthNeg;
lock (LearnLock)
mSvm = teacher.Learn(m_AllInput.Get(SampleTrain), ExpectedReponse.Get(SampleTrain));
lock (WriteLock)
{
Console.WriteLine("----------------------");
for (int x = 0; x < mSvm.SupportVectors[0].Length; x++)
Console.Write(mSvm.SupportVectors[0][x] + " , ");
}
});`
the output for a normal loop will be
-0.000826141768830679 , 0.0204699472678477 , 0.00102020381118296 , 0.00815680738251794 , 0 , -0.0313196904731213 , -0.00979969575451725 , -0.00619859000567448 , -0.182436451148049 , -0.169746783327167 , 0.0275845584795227 , -0.0106118382671151 , -0.00673461141893794 , -0.0716769962697753 , 0.0710968294057133 , -0.0547658229961046 , 0.575086763398627 , 0.0113209075151587 , 0.0800350225955227 , 0.521745164392834 , 0.05914920191347 , 0.0856669171200027 , 0.00885692863523255 , 0 , 0.202694197301532 , 0.214768659939216 , 0.00424783125796799 , -3.34316682284646E-05 , 0 , 0 , 0.00805334842159865 , -0.000406151757135115 , 0.00164673660219298 , -0.0351052896698053 , 0.0805058988245328 , -0.0781740379614528 , 0 , 0 , 0.00439108306169607 , 0 , 0 , 0 , 0 , 0 , 0 , 0.00351226473083791 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
-0.0346900386609239 , 0.0982236516984847 , 0 , 0.00375906773700697 , -0.00169247524856784 , -0.0422966498398981 , -0.0566273700377846 , -0.0624919871874013 , -0.524011340998584 , -0.544364462657513 , 0.0974668887971933 , -0.0993871044688287 , 0.0236184615809086 , -0.130593582063367 , 0.109480210839173 , -0.298891121870278 , 0.0021793430607151 , 0.00754978069591852 , 0.420525925445541 , 0 , 0.0140190693669248 , 0.385971501688919 , 0.0061115375457926 , 0 , 0.0544431514945646 , 0.111497110890586 , 0.0316171698615259 , -0.0171131452688447 , -0.0140677719852532 , 0 , 0.0741404019116678 , -0.0205981279763819 , 0.00331231553669871 , 0 , -0.0382578212103394 , -0.0181199512839947 , 0 , 0.0643693040847381 , 0.00759113442544499 , -0.00425454849062484 , 0 , 0 , 0 , 0 , 0.0821408671626303 , 0.0742342884625242 , 0.0224983527373667 , -0.00486644084687394 , 0 , 0 , 0 , 0 , 0.00266590639320067 , -0.00183332543167103 , 0 , 0 , 0 , 0 , 0 , 0 , 0.0233652221024686 , 0.0463451063717259 , 0.0577730926769039 , 0 , 0 , 0 , 0 , 0 , 0.0573093324201102 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0.00368554631494915 , 0.00679229094394894 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0.00210631655029401 , 0 , 0 , 0 , 0 , 0 , 0 , 0.0245928951997821 , 0.0280577660960887 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0.0107909584171157 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0.0266835987277734 , 0 , 0 , 0 , 0 , 0 ,
-0.0624498266977226 , 0.137097982980772 , -0.0607374740765661 , -0.00140713708827692 , 0.00552945290218392 , -0.0458669576431541 , -0.123175071172695 , -0.0800279494967896 , -0.425151973583967 , -0.501487175281888 , 0.0655841262068003 , -0.133144500976644 , 0.0611418847614961 , -0.109646725645036 , 0.122748618902023 , -0.295133471997353 , 0.0361634176576678 , 0.0467163321661245 , 0.103726855701531 , 0.00457130765464022 , 0.0804241151778741 , 0.0520067663442601 , 0.0225885217405756 , 0 , 0.0311956260278756 , 0.0934840111777248 , 0.0606254132728445 , -0.0136235261394431 , -0.0285850485202835 , 0 , 0.0695213106708914 , 0.0192005353532549 , 0.0486734363143292 , 0.0315977555120663 , -0.0834742297791078 , 0.0149424508587336 , 0.004044651308224 , 0.0713862674321995 , 0.00985276006351719 , -0.00443375284412176 , 0 , 0 , 0 , 0 , 0.110128213313692 , 0.0886720212087911 , 0.0392516955910503 , -0.0136028936625109 , 0 , 0 , 0 , 0 , 0.0226399445687031 , -0.0481770619860295 , 0 , 0 , 0 , 0 , 0 , 0 , 0.0549592450628401 , 0.0508520839649303 , 0.0833827553408674 , 0 , 0 , 0 , 0 , 0 , 0.0863461986255835 , 0.00429226922306294 , 0 , -0.0013261234901181 , 0 , 0 , 0 , 0 , 0.0317826242916549 , 0.0122208418124134 , 0.00801602166317494 , 0 , 0 , 0 , 0 , 0 , 0.00907777600563064 , 0 , 0 , 0.0128176364936291 , 0 , 0 , 0 , 0 , 0.0566937417008235 , 0.0780785799996673 , 0 , 0.0070623829579756 , 0 , 0 , 0 , 0 , 0 , 0 , 0.0406866715145646 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0.0707406937782872 , 0 , 0 , 0 , 0 , 0 ,
The output for a Parallel_for will be .
0 , -0.0364053404706865 , 0 , -0.0118284559009336 , -0.236977076541997 , 0.0101785368728012 , 0.054175066340882 , -0.112139307417236 , -0.230609580796169 , -0.204445486052906 , 0.00827309616408402 , -0.00253708175303826 , -0.00310190434486514 , -0.205020577470764 , 0.00260513252957839 , -0.0486718190082557 , -0.131894460367258 , -0.14548733291231 , 0.562547673448042 , 0 , 0 , 0.481501403675388 , 0.542195402765221 , 0 , 0.540893993500823 , 0.508449364954348 , 0.239102420673373 , 0 , 0 , 0 , 0 , -0.0223791034472427 , -0.0469054236893163 , -0.0989017843443595 , -0.0209303398579702 , -0.0193279369560885 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0.00775902262757202 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0.206331932824648 , 0.23140979224326 , 0.505989422570519 , 0 , 0 , 0.0277308808964755 , 1.04041356077525 , 0 , 0.617408050202274 , 0.715702209843209 , 0.400147909996907 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0.111480003436048 , 0 , 0 , 0 , 0 , 0 , 1.27189798560755 , 0 , 0.662333153767896 , 0.836195425181694 , 0.2349779587077 , 0.070441597883365 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
I would have expected the SupportVectors to look the same in the parallel version than in the normal loop version, but now there are significantly different.
I've look in the src code, but could not figured why.. Does that make sens to you ?
Thanks for everything, your doing an amazing job.
Best regards
JP