fminunc can't solve problem
Show older comments
I'm trying to build a softmax classifier and I can't seem to compute the weight vectors using fminunc.
W_0 = zeros(784,10);
O = @(W,j) softmax(X*W(:, j));
minus_log = @(W,j) -log(O(W,j));
H = @(W,j) sum((minus_log(W,j) .* Y(:,j))');
J = @(W,j) 1/n * sum(H(W,j));
weights = zeros(784, 10);
for j=1:10
wj = fminunc(@(W)J(W,j), zeros(784,1));
weights(:,j) = wj;
end
X is n by 784 and Y is n by 10. fminunc ends with the 'Index exceeds matrix dimensions.' error.
10 Comments
Walter Roberson
on 31 May 2021
n and X are undefined. n seems to be a scalar, but I do not know what size X is supposed to be.
Stefan Nastase
on 31 May 2021
John D'Errico
on 31 May 2021
So it is not that fminunc cannot solve the problem, but that you wrote the code improperly to use fminunc.
Before you throw something into an optimizer, first, verify that your objective function works! Test the objective. When you pass in a vector of size 784x1, does it return a scalar value? Or does your code fail with an error?
Stefan Nastase
on 31 May 2021
Walter Roberson
on 31 May 2021
What size is X ?
Stefan Nastase
on 31 May 2021
Walter Roberson
on 31 May 2021
How long does the code run before reporting the problem? What is j when the problem is encounted?
I am running the code with X = rand(n, 784); Y = rand(n, 10); and it is processing through... fminunc with 784 variables should be expected to be slow.
Stefan Nastase
on 31 May 2021
Edited: Stefan Nastase
on 31 May 2021
Walter Roberson
on 31 May 2021
Would it be possible for you to attach the X and Y in a .mat file? Perhaps the data makes a difference.
Also, which MATLAB release are you using?
Stefan Nastase
on 31 May 2021
Edited: Stefan Nastase
on 31 May 2021
Answers (1)
Walter Roberson
on 1 Jun 2021
Look at your code:
wj = fminunc(@(W)J(W,j), zeros(784,1));
The size of W that will be passed in will be 784 x 1.
J = @(W,j) 1/n * sum(H(W,j));
J will take that and pass it to H
H = @(W,j) sum((minus_log(W,j) .* Y(:,j))');
H will take it and pass it to minus_log
minus_log = @(W,j) -log(O(W,j));
minus_log will take it and pass it to O
O = @(W,j) softmax(X*W(:, j));
O will take it and try to index it at column j. But W only has one column.
15 Comments
Walter Roberson
on 1 Jun 2021
n = 60000;
M = 784;
maxj = 10;
load Y
X = rand(n, M);
W_0 = zeros(M, maxj);
O = @(W) softmax(X*W);
minus_log = @(W) -log(O(W));
H = @(W,j) sum((minus_log(W) .* Y(:,j)), 2);
J = @(W,j) 1/n * sum(H(W,j));
weights = zeros(M, maxj);
options = optimoptions('fminunc','Algorithm','quasi-newton');
for j=1:maxj
wj = fminunc(@(W)J(W,j), zeros(M,1), options);
weights(:,j) = wj;
end
Stefan Nastase
on 1 Jun 2021
Stefan Nastase
on 1 Jun 2021
Stefan Nastase
on 1 Jun 2021
Walter Roberson
on 1 Jun 2021
Please confirm your release is R2016a (as you indicated before) and not R2015a.
60000x10 matrix .* 60000x1 matrix is valid starting from R2015b.
Walter Roberson
on 1 Jun 2021
e is not defined for your e(j) .
On my system, without the e(j), in R2016b, the code is not exactly fast, but it does run to completion; progress can be traced by the occasional message similar to
Local minimum found.
Optimization completed because the size of the gradient is less than
the default value of the optimality tolerance.
<stopping criteria details>
which happens once for each value of j.
I am still using random X. Perhaps you could put your X into Google Drive or Dropbox and post the link to it?
Stefan Nastase
on 1 Jun 2021
Walter Roberson
on 1 Jun 2021
e = @(k) [zeros(k-1, 1); 1; zeros(10-k, 1)];
e is going to give back a 10 x 1 vector. You use * with e(j) which would be something suitable for selecting a single column from a something by 10 matrix. But W is 748 x 1 not 784 x 10.
Stefan Nastase
on 1 Jun 2021
Walter Roberson
on 1 Jun 2021
wj = fminunc(@(W)J(W,j), zeros(M,1), options);
so W will be 784 x 1. It will be passed into J
J = @(W,j) 1/n * sum(H(W,j));
and is still 784 x 1. It will be passed into H
H = @(W,j) sum((minus_log(W,j) .* Y(:,j)), 2);
and will still be 784 x 1. It will be passed into minus_log
minus_log = @(W,j) -log(O(W)) *e(j);
and will still be 784 x 1. It will be passed to O
O = @(W) softmax(X*W);
X is 60000 x 784, and you * it with W that is 784 x 1, giving you a 60000 x 1 result. That 60000 x 1 will be passed to softmax() which will return a 60000 x 1 array (the same size).
That 60000 x 1 will be returned from O, and will be * by e(j)
e = @(k) [zeros(k-1, 1); 1; zeros(10-k, 1)];
e(j) returns a 10 x 1 vector.
So minus_log will be a 60000 x 1 * a 10 x 1 . That is an error.
I would suggest that as the e(j) is obviously intended to select one column of a matrix that only has one column, that you could just leave out the e(j)
Walter Roberson
on 2 Jun 2021
Note: without the e(j), each j iteration is taking about 28 minutes on my system, for a total of about 4 2/3 hours run time.
Ah, I just noticed that it is running into a limit on the number of function evaluations. I will need to adjust the options for that.
Stefan Nastase
on 2 Jun 2021
Walter Roberson
on 2 Jun 2021
I increased the maximum iterations and maximum function evaluations to 1e6, and did one iteration. Literally just as I started typing this message, it finished the one iteration, after 14197 seconds -- just short of four hours (for one iteration)
weights(:,1) = [
-0.0668483829985867
0.234891384281569
0.298686318147781
-0.0523631198489966
-1.08005053813696
0.793050950584555
0.514221850430549
-0.168705108487602
0.313261917874737
0.116592410083641
-0.279697140879508
0.111499545551635
0.203215501241068
-0.398301183369412
-0.16219591277022
-0.316500418417299
0.438417768932266
-0.0255281530123204
0.0470246246994743
0.000292011151606748
0.409991659541117
-0.398405622789701
0.386183389242775
-0.936249620599338
0.107212641912081
2.80331175755588
-0.598032335665387
0.0208344166230423
0.229584701064718
0.177035458005252
2.50008106282662
0.915383140772701
-0.36804364106054
-1.05299042673607
-0.427894753492936
-0.0753782086622511
-0.0117915253545344
-0.245612595552175
-0.183942687019895
0.105225368559856
-0.159633574832249
-0.125925738770361
0.210755718455888
-0.0434649487445839
0.130910587368287
0.212064646024289
0.144198578369216
0.125509442357776
0.281936273464211
0.146681184438067
0.160694105992158
-0.081172880846275
-1.07567838121628
1.20964964715707
-1.34624495673536
0.984671178325176
0.289945020835393
-1.35932775794754
-2.39722845074613
1.03887397353972
0.680018479395765
-0.0580912779325948
-0.274518025621697
0.0387894825610407
-0.0641063316700847
-0.0997377535208911
0.0852174092060219
-0.125076959474835
-0.0421119500726402
0.165333744287713
-0.191939355030694
0.187636091624642
-0.0149148174741897
0.0569570172129179
-0.153957815015795
-0.038127801313112
0.154342308112507
0.087370373914997
0.0397102199320467
0.173961597657859
0.93619970138595
0.252030753133772
-0.815812473821209
0.764190730917176
-0.928800957082218
0.189437273123309
-0.0695628958705444
-1.11152931264951
0.0603826286894126
-0.110024935902611
-0.00602338493373507
-0.130477178099237
0.0397749699768286
0.140270734066406
-0.246923939238697
-0.16089904427387
0.0907151267257231
-0.177123622650632
-0.0770763011158528
-0.0910900190839041
-0.0228697210297662
0.0244583243137087
0.103304138017293
-0.0156361033431784
-0.229164428295675
0.429501301995306
-0.00801184848959154
0.0487183628993435
-0.130720193942551
-0.311435710141892
1.42257983673688
-0.492656616640154
-0.414891186724638
-0.309919494899468
1.39437183229742
0.156583681606896
-0.17873376420513
-0.0829866461905462
0.0328334185420057
-0.287537589407016
0.0153925030728468
0.343070410081882
0.0686471069953281
-0.153942984552881
0.16306471753292
0.0889856271583311
-0.235967299727531
0.00653271217686761
-0.336404234058829
-0.218585561931543
-0.0310410582925096
0.0898678405943337
0.163928142158287
-0.0400848359937233
0.249163225221489
0.140429873512664
0.566899706981303
0.670476443630937
-1.3799503253616
0.418532523475777
1.54313373739715
-0.860749303414678
-0.019931647262259
-0.130988426266387
0.354757967789141
0.0227109298285494
0.204762754332458
-0.153074171803519
-0.224636340368964
0.0123940335552903
-0.246554272125823
-0.105382301912623
0.127103746684295
-0.030328999420032
0.172456516550008
-0.038956687463275
-0.180341548043974
0.15334725706344
-0.0779739319847514
0.0278246024713355
0.0328820960373484
0.0611835292307367
0.143665795465287
0.0992159874429165
-0.010227701794117
-0.120050009387854
-0.911877510810651
2.04769106234289
0.00942935954205871
0.117706340666731
0.157444947137298
0.0809524463523642
-0.116860057940275
0.139828630040863
0.123122470291149
-0.351207639424635
0.299150880636075
-0.0955755555317044
0.0586652261281485
0.00795543053753331
-0.154307134724171
0.098189372430675
-0.339338192459895
0.299505143692537
0.00915539086492278
-0.171982280770756
0.0217299132251512
0.3428207830741
-0.14160010685041
-0.0358427678056345
0.208561477280084
-0.106231691272428
0.00040001537744281
0.0176157827562228
0.335865768207723
-0.645577717525273
0.744575727168932
0.256206406749462
0.220126750095851
-0.260340961992756
0.153173234766982
0.155367595883477
0.0327759801027137
0.110925597823675
0.102706066785775
0.00481933891941187
-0.00929827933710481
0.0715086424580359
0.116965960444917
0.0699190307658461
-0.226212309247131
-0.157569766240872
-0.00371254580851792
0.0732668359353629
-0.0197489542251061
0.0636500477615321
0.121093766374959
-0.156656693670455
0.166694246620757
0.0920613809740539
0.0886715742678555
0.00172377118842567
0.0103982957584913
-0.255446572966247
0.21420137903633
0.152621435612036
0.374895338445025
0.0446067931218969
0.167932083614233
0.036871724116622
0.250958483077087
0.0151358581514006
0.271911633450653
0.0378128501840153
0.0919250972712414
-0.101862342572983
0.0238508333028314
-0.152697141686958
0.0932524687651834
0.0591258911585739
-0.0709524505779743
0.0336828483071373
0.0621535734313207
0.202681263702455
-0.181993623223297
-0.0700930893988497
-0.0259659204931684
-0.240758622050618
0.015601233669221
-0.0237621038585974
-0.167226729670491
-0.228328544825946
0.212261466775465
-0.177369106745686
0.26657457249146
0.142325000342355
-0.00948426338367521
0.106102657954979
0.0437899115850532
0.125299403227092
0.0310921849587257
0.0852752538487357
0.163084096865219
-0.0965339617933082
0.0172208923905283
0.0976648534423438
0.0922632939717038
-0.0656659025902603
-0.0189661047796749
-0.0328489427710121
0.236579938803712
-0.0233770386399082
-0.10808388797829
0.16938713114614
0.126754070606892
-0.186493682700957
-0.0563357246608788
-0.0795153932790677
-0.0881620352141961
-0.195387542077861
-2.86544941781073
0.418246046506183
0.159552372248693
-0.264565860941512
-0.0337939119921872
0.143466813771518
-0.0118708288453276
0.0337231392791524
0.160423332943231
0.347186794760368
0.0381520978508014
-0.0878980558083452
0.0663580992442074
-0.243693149307717
0.164261535101097
-0.147740211473259
0.100380539285263
0.00658333086103933
0.179337808976494
-0.0229802298481981
0.000955256046230511
0.0796324991868171
-0.00638135547505448
0.0957847894856657
-0.0261377364328142
-0.212981119391511
-0.192190698020642
-0.36766801687194
1.05200447437717
-0.651838998287155
0.467871036643268
0.0459686951562332
-0.117484533806437
0.18937292555121
0.0598368835190807
0.0742923125775919
0.0270962656287223
0.134637397012478
-0.128791267608455
-0.0607295117513172
-0.182444809592044
0.146153581232332
0.155597264860918
-0.122611450215726
0.00477325115406099
-0.266063941797057
0.287791858277772
-0.075450868541649
-0.117723420372877
0.00466649233666182
-0.0278094668690351
0.100875407456193
0.0844786680799759
-0.12472484522831
-0.29245919192678
-1.0824348354808
1.1341000949197
0.100428201217654
0.499799402892109
-0.187330607924579
0.388394804101794
-0.098229090501625
-0.0551612489425452
0.0751491258733939
-0.186591173702527
0.332766576160506
0.0798243901328246
-0.00157004558532188
0.0283292648353769
-0.171750372951684
0.102768527822294
-0.240425371689628
0.124205164479758
0.146166074517061
0.001359715619882
0.0141499333667685
-0.107548331947006
-0.131961478608931
-0.116910549950282
0.0800914463373853
0.0400368894397201
0.0260274563826365
-0.362551970199787
-0.381828319120501
-1.37470381676963
0.43858984283078
-0.168068935514922
0.935002421553776
-0.368592426999955
0.182398716653391
0.0176854471095589
-0.0566321234872561
0.0892819677440207
0.0240128879745946
-0.421610784422644
-0.0602455710535193
-0.0984503016733933
-0.0808946865212363
0.0538912349727716
-0.175161036599998
0.152345866702723
-0.0720445642931288
0.33046022420275
0.0547550500173078
-0.119058421528727
-0.0097223437840855
-0.308736147028945
0.0483535717226667
-0.246902911310302
0.00675970676346051
-0.144942057893944
-0.540815285576477
-0.257342145701131
0.245542324820725
0.377405895328053
-0.256170030953572
0.490838780777291
-0.672806163146844
0.0175061880856685
-0.0505571249892091
-0.0151057052630127
0.183710477623859
0.127680934148404
-0.0152891497908235
0.19565544998821
0.19017096694179
0.150680383466093
-0.0150214452462304
-0.280666188298285
0.0314898570707079
-0.45458119620743
-0.0810022267405334
-0.257554018323808
-0.00396568865286147
0.144083536431039
-0.179810823618876
0.219363136234947
0.0529715501915122
-0.60938688014718
-0.0367811054125018
-0.605422317972006
-0.355005928455547
0.404149417599026
0.107351384094607
-0.414679975671933
0.512504284521307
0.196560998168567
-0.125351231114625
-0.00307186866629005
-0.179857336066351
0.190097636202332
-0.139934125769046
-0.18720437494206
-0.151164058615875
0.222807104172616
0.0543932612035033
0.154979226715711
-0.0424471736543276
-0.0848441117992094
-0.0401884635148347
0.0498611484265494
-0.205881903124607
-0.848457043670014
0.333751272119962
-0.683708024835908
0.311990146730987
0.0554899212701849
-0.642741886614039
-0.105177308351371
-0.0587674139756567
0.437118710653153
-0.386523491665235
0.726488772479946
-0.374370945483352
-0.13880455185715
0.0732088095195116
0.0714492662580706
-0.0375803215208379
-0.0990382103683072
0.0126928921312817
-0.000404361336804712
0.178614668440637
0.26065382292758
-0.199102118898851
-0.189103863041764
0.115395032978979
-0.213403525784635
-0.173088562029799
-0.173076755318284
0.266343462555896
0.258748824742209
-0.759857121163383
0.0320400902930271
-0.647890500862348
-0.391849831435577
0.44619872334126
-3.15348379298187
-0.0655007684656429
0.0313090099607602
-0.199977517987355
-0.186694107637869
0.279189046654003
-0.0241315076418009
-0.0236364149747765
-0.454951355257368
0.0284037776109162
-0.0990534831104064
-0.173829021935585
-0.0273432785481552
0.105843525518526
0.0273072148712745
0.155932394686241
-0.125115292390798
0.219612050029111
-0.131579092779185
-0.0891159168368341
0.0182348060728129
-0.366756801737635
0.51265275677783
0.256611149812891
-0.157106841217337
0.561120653183035
-0.867116460044481
-3.70255883142287
2.07245297116642
-1.54287274068555
-1.03209528339741
0.766062390065415
0.148587843904602
-0.123353614704091
0.0444657311234921
-0.0452725843687985
0.0962019937665279
-0.15417040443963
-0.0544651510428806
0.107387333890485
-0.292251044938472
0.250316711664638
-0.100398045409047
-0.26482182765218
0.0610561514016658
0.13522660795575
-0.108523581767731
0.00827316087636856
-0.153670118880011
-0.236529232411093
-0.055820152583479
-0.614192558752927
0.716048831171076
-0.892157354411876
0.652081301692573
-0.797851066648727
-0.789507502071255
0.00084544814908195
1.2940294360603
-1.26133968824821
0.690142873493948
0.2800024744151
-0.0623836841881628
-0.217620057761252
0.0412408445860952
0.17812203976304
0.310881381525398
-0.173510612629792
0.0783096563308116
0.161684817686823
-0.134620090695962
-0.0954160940057858
-0.16860221891996
-0.231855775676926
0.0239395352261974
0.0597890924684974
-0.0152808020692084
0.0657962358576424
-0.862505697499507
-0.0177174968094178
-0.300536878250797
0.791012411690198
-1.5147954095005
-0.581696489744224
0.493574641460894
-0.0546467053096841
-2.18136063751402
1.66998994788704
-0.480413652160427
-0.0613349603101528
-0.405576001789889
0.0253197413123529
0.225233270678046
-0.067838652588243
-0.268512587867707
-0.01794294604425
0.00528511718800813
-0.218876330848532
0.126373934729652
0.0219285828125403
-0.00096320401866012
-0.199355633926784
0.0231080022308489
0.265657647999871
0.180562371746151
-0.175401851042386
0.488705654462178
-0.037127001527798
-0.550156336032442
-0.0100453874890877
0.739298151376683
0.380638429870775
-3.06953817784059
-0.703884908364317
1.23280533297725
0.148570136662072
0.293528158483064
-0.34077585681301
-0.110605490238985
0.184896463156921
0.452236616647337
0.0726925979436372
-0.0336479001127872
-0.285456818602387
0.0438000752339321
0.132618924736713
0.00577703642812724
-0.129597637143352
-0.165910931518773
0.104333584340262
-0.0787485202025336
0.0999796244606303
-0.149490637080095
0.0116326459291074
-0.396722319086251
-0.648551708749326
0.46311280149945
0.237751216719083
0.0465058775150628
-0.615649276726202
-0.690116208334144
0.836688960733744
-0.869178563526874
0.370301444759158
-0.0305052269032978
-0.56593852072208
-0.0845240640128812
0.0732360637158833
0.0701409717394928
-0.0819972453883752
-0.417027266155228
0.152808065494095
-0.11672238434816
-0.0928450377685273
0.18343892948574
-0.0411971440528911
-0.200650143721023
-0.148327670579799
0.156141828536477
0.0923622148221883
0.0347782108120429
0.197070678395664
0.148590493230024
-0.233001608237466
-0.266512024311859
0.49120500659264
-0.822053830734351
1.27329400800604
0.14752514490586
-0.829968008894125
0.89759541830878
-0.0910103493634261
-1.0041781658068
0.216066668062257
-0.162303454214253
0.233326770110743
0.00775585447896311
-0.107885490123651
-0.0311096344006142
-0.15496175977239
0.175220608567733
-0.035685555218975
0.143352511942739
0.0433987598554698
-0.177251855135406
0.0241875323322989
0.208219310775114
0.158671140397118
0.0623187728025287
0.107538760526239
-0.232617763747613
-0.105115843044647
-0.12064769112812
-1.08874749921715
3.07209647888178
-0.00250539279299298
0.624388724731798
-0.534644202500649
-0.233944948492952
0.171079881423874
-0.784562404549569
-0.140079932440851
0.511516044572315
-0.250320614702193
0.493588331119691
0.240331591101167
-0.0387450725195493
-0.164623181698948
0.0832219931835995
-0.198677234628845
-0.197222959940636
0.0937005580907931
0.000455897320323082
-0.0464628626274127
-0.0844612368664031
-0.23237110533064
0.264187776480481
0.00424210868608308
0.0436664750937751
1.1700909476696
-0.879955000776877
0.262129244040949
-1.60550421150606
-1.74867009409
1.28267572874183
-0.00498359825514929
2.04390540518976
-1.0420236334125
-0.648609191434189
0.0597807231928927
-0.0183216238885864
-0.12384263581434
-0.318447578964504
-0.0581024905629387
0.108900165281168
0.156023727476858
-0.140349848903701
-0.158771658982124
-0.154766574431699
0.199603268554562
0.160656674036033
-0.404431067868471
0.161208625421488
0.205645172265187
0.0587083878205585
-0.149617489856916
-0.0191096374640681
-1.61759485180819
0.916361874247461
-1.04971109459909
2.08672396314013
0.368966773180474
1.6595268815914
1.08477296237332
-1.79181852377715
-0.57768419575805
0.117643406013592
-0.200996837304624
-0.234152053294192
0.0881915729738342
0.300463563593711
0.100196374278043
-0.118250488345355
-0.300224438155998
0.00156777961236278
-0.120702931973925
0.578652808186123
-0.247415955099847
0.111153306538136
0.146528230648954
-0.179379122263176
0.400095114000571
0.11566252005656
-0.0771621391578934
0.0620175593949429
0.871918840874586
-0.211439589185968
0.798199055045526
-2.06879723430428
1.44302569525713
0.0143615087014836
0.925936439736003
-0.0271011772064832
-2.27544762423451
1.14161159870368
-0.483675830378049
0.173228033167905
0.286927301254259
-0.0874075575306242
0.347227232901907
-0.311563978885482
-0.237006724305047
-0.0465695989205739
0.147390848987703
0.311711191350079
0.311371021199254
-0.211781798229626
-0.290273151923143
-0.151936641759267
0.318325310694123
0.0173996756227081
0.172237581015073
0.0172943203072413
-1.66705779009168
-0.434737071869909
1.5019038200084
0.193888857384968
1.86156203830858];
Stefan Nastase
on 2 Jun 2021
Walter Roberson
on 2 Jun 2021
I tried running with fminsearch() . I killed it off after about 7 hours... I could see from the output that descent was pretty slow.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!