How to reuse Jacobian and Inverse Hessian with SciPy minimize - scipy

I am currently using the minimize(BFGS) function from SciPy.optimize to calibrate my model. Once calibrated the parameters will be just slightly perturbed from time to time and I would like to reuse the Jacobian and IHessian from the previous calibration for my initial ste( a kind of initial guess for jac and hessian and then switch to regular calculation for these)
Any idea how to do it?

You will have to modify scipy slightly to do this. The Hessian approximation for BFGS is stored in the variable Hk, first defined in line 1308 of the function _minimize_bfgs() in the file _optimize.py here: https://github.com/scipy/scipy/blob/65d8361519e3f1166d81dceef9f1d6375b4ed808/scipy/optimize/_optimize.py#L1308
You will have to modify scipy's code return or store this matrix, and pass it to the function again as an argument next time.
Alternatively, you can use scipy's nonlinear root finding instead, because minimizing a function is the same as finding the point, x, where the gradient, g(x), of the function is zero. The caveat is that this is much less robust from an algorithm perspective. The root finder may converge to saddle points or maxima, etc.
The BFGS algorithm for optimization is very similar to Broyden's method for finding the roots of a nonlinear function. Broyden is the B in BFGS, and BFGS is basically a modified version Broydens root finding method applied to the equation g(x)=0.
From a code perspective, scipy provides better access to the internals of the Broyden's nonlinear root finder than it does to BFGS. The Broyden Jacobian matrix (which is the analog of the BFGS Hessian) is stored as an object of type "Jacobian" which can be re-used if you over-write the "setup()" method to do nothing, so it won't reset. For example:
import numpy as np
from scipy.optimize import nonlin, rosen, rosen_der
x0 = np.array([0.0, 0.0])
counter = [1]
def callback(x, Fx):
print('k=', counter[0], ' xk=', x, ' f(xk)=', Fx)
counter[0] += 1
print('first solve:')
jac = nonlin.BroydenFirst()
sol1 = nonlin.nonlin_solve(rosen_der, x0, jacobian=jac, callback=callback, line_search='wolfe')
print('')
print('Jacobian after first solve:')
print(jac.todense())
print('')
print('second solve:')
jac.setup = lambda x,y,z: None # Prevent Jacobian from resetting. Try commenting this line out!
counter = [1]
sol2 = nonlin.nonlin_solve(rosen_der, x0, jacobian=jac, callback=callback, line_search='wolfe')
print('')
print('Jacobian after second solve:')
print(jac.todense())
This yields the following output:
first solve:
k= 1 xk= [-0.5 0. ] f(xk)= [-53. -50.]
k= 2 xk= [-0.32283933 0.16713271] f(xk)= [ 5.47792447 12.58149553]
k= 3 xk= [-0.28061925 0.08236319] f(xk)= [-2.1553475 0.7232059]
k= 4 xk= [-0.21641127 0.02839649] f(xk)= [-4.02884252 -3.68746961]
k= 5 xk= [-0.18109429 0.02229409] f(xk)= [-3.12286088 -2.10021066]
k= 6 xk= [-0.19320298 0.02730297] f(xk)= [-3.16110526 -2.00488442]
k= 7 xk= [-0.1500595 0.01281555] f(xk)= [-2.88248831 -1.94046136]
k= 8 xk= [-0.14063472 0.00987684] f(xk)= [-2.83825517 -1.98025685]
k= 9 xk= [-0.10559036 0.00122285] f(xk)= [-2.63043649 -1.98529385]
k= 10 xk= [-0.07978769 -0.00399875] f(xk)= [-2.49036939 -2.07296403]
k= 11 xk= [-0.04033837 -0.00840453] f(xk)= [-2.24254189 -2.00634254]
k= 12 xk= [-0.01281811 -0.01000148] f(xk)= [-2.07775868 -2.03315754]
k= 13 xk= [ 0.04612425 -0.00598848] f(xk)= [-1.75801514 -1.62318494]
k= 14 xk= [ 0.03215952 -0.00653348] f(xk)= [-1.83833134 -1.51354257]
k= 15 xk= [0.08036797 0.0021266 ] f(xk)= [-1.6999891 -0.86648289]
k= 16 xk= [-0.04367326 -0.01098396] f(xk)= [-2.31254872 -2.57826183]
k= 17 xk= [-0.07820406 -0.00365169] f(xk)= [-2.46195331 -1.95351227]
k= 18 xk= [-0.02940764 -0.00337684] f(xk)= [-2.10871 -0.84832937]
k= 19 xk= [-0.0314977 -0.00316138] f(xk)= [-2.11532548 -0.83069659]
k= 20 xk= [-0.00053675 -0.00472907] f(xk)= [-2.00208887 -0.94587179]
k= 21 xk= [ 0.01086898 -0.00493684] f(xk)= [-1.95628507 -1.01099561]
k= 22 xk= [ 0.03866879 -0.00408101] f(xk)= [-1.83641115 -1.11525685]
k= 23 xk= [ 0.06232922 -0.00227391] f(xk)= [-1.72179137 -1.23176741]
k= 24 xk= [0.09084425 0.0019575 ] f(xk)= [-1.5895594 -1.2590346]
k= 25 xk= [0.11524109 0.00670698] f(xk)= [-1.46650132 -1.3147068 ]
k= 26 xk= [0.17879539 0.0246649 ] f(xk)= [-1.12012011 -1.46057767]
k= 27 xk= [0.17505925 0.02929594] f(xk)= [-1.5553635 -0.26996006]
k= 28 xk= [0.18893989 0.03342207] f(xk)= [-1.4500936 -0.45524167]
k= 29 xk= [0.22546628 0.04688035] f(xk)= [-1.19240743 -0.79093866]
k= 30 xk= [0.23877529 0.05259369] f(xk)= [-1.10029946 -0.88399008]
k= 31 xk= [0.25428437 0.06010509] f(xk)= [-1.02807924 -0.91109027]
k= 32 xk= [0.27116407 0.06871439] f(xk)= [-0.9353492 -0.96311185]
k= 33 xk= [0.28827306 0.07809215] f(xk)= [-0.84584567 -1.00184213]
k= 34 xk= [0.30453673 0.08760267] f(xk)= [-0.76480491 -1.0279903 ]
k= 35 xk= [0.32072413 0.09764762] f(xk)= [-0.68934851 -1.04326926]
k= 36 xk= [0.33639066 0.10790526] f(xk)= [-0.6203388 -1.05068295]
k= 37 xk= [0.35201809 0.11866338] f(xk)= [-0.55625337 -1.05067108]
k= 38 xk= [0.36724683 0.12964277] f(xk)= [-0.49759833 -1.04549305]
k= 39 xk= [0.38246707 0.14110658] f(xk)= [-0.44343805 -1.03489669]
k= 40 xk= [0.39728775 0.15273218] f(xk)= [-0.39410312 -1.02107526]
k= 41 xk= [0.41215838 0.16485939] f(xk)= [-0.34886931 -1.00302937]
k= 42 xk= [0.42650931 0.17699212] f(xk)= [-0.30793999 -0.9836144 ]
k= 43 xk= [0.44109504 0.18976381] f(xk)= [-0.27072811 -0.9602033 ]
k= 44 xk= [0.45477201 0.20212697] f(xk)= [-0.23719208 -0.93812269]
k= 45 xk= [0.4681153 0.21458789] f(xk)= [-0.21291526 -0.9088083 ]
k= 46 xk= [0.47908973 0.22505828] f(xk)= [-0.1854592 -0.89373795]
k= 47 xk= [0.50398499 0.2497432 ] f(xk)= [-0.13370942 -0.85153389]
k= 48 xk= [0.51439976 0.26044589] f(xk)= [-0.11498655 -0.83224564]
k= 49 xk= [0.53852539 0.28603837] f(xk)= [-0.06750659 -0.79424541]
k= 50 xk= [0.55785196 0.30748713] f(xk)= [-0.05606785 -0.74233693]
k= 51 xk= [0.56092826 0.31093463] f(xk)= [-0.0466508 -0.74117561]
k= 52 xk= [0.61261365 0.37369717] f(xk)= [-0.38311202 -0.31966368]
k= 53 xk= [0.52465657 0.27398909] f(xk)= [-0.68302112 -0.25508661]
k= 54 xk= [0.74386124 0.52529992] f(xk)= [ 7.82778407 -5.60592561]
k= 55 xk= [0.54293777 0.30014958] f(xk)= [-2.07995492 1.07363175]
k= 56 xk= [0.58511821 0.34990237] f(xk)= [-2.59425851 1.50781064]
k= 57 xk= [0.35868093 0.08830817] f(xk)= [ 4.50558888 -8.06876881]
k= 58 xk= [0.47983856 0.24586376] f(xk)= [-4.0381075 3.12374291]
k= 59 xk= [0.3775732 0.1412489] f(xk)= [-1.04660917 -0.26252449]
k= 60 xk= [0.36511619 0.13121294] f(xk)= [-0.96352471 -0.41937734]
k= 61 xk= [ 0.1114212 -0.05351408] f(xk)= [ 1.16118684 -13.18575168]
k= 62 xk= [0.25437135 0.02356053] f(xk)= [ 2.69511008 -8.22885014]
k= 63 xk= [0.16777908 0.02828527] f(xk)= [-1.67353225 0.0270904 ]
k= 64 xk= [0.21546576 0.04636992] f(xk)= [-1.56427885 -0.01111462]
k= 65 xk= [0.24867847 0.0590896 ] f(xk)= [-1.22895955 -0.55027587]
k= 66 xk= [0.26241736 0.06557769] f(xk)= [-1.13033017 -0.65703565]
k= 67 xk= [0.27594487 0.07265633] f(xk)= [-1.06297502 -0.69784814]
k= 68 xk= [0.29626637 0.08383401] f(xk)= [-0.94058127 -0.78794969]
k= 69 xk= [0.31324088 0.09384403] f(xk)= [-0.83777351 -0.85516414]
k= 70 xk= [0.3305509 0.10477546] f(xk)= [-0.74543613 -0.89768638]
k= 71 xk= [0.34571472 0.11489762] f(xk)= [-0.66954478 -0.9242097 ]
k= 72 xk= [0.36107381 0.12568167] f(xk)= [-0.60009902 -0.9385247 ]
k= 73 xk= [0.37526559 0.13609304] f(xk)= [-0.53928208 -0.94624546]
k= 74 xk= [0.38966873 0.14710916] f(xk)= [-0.48301011 -0.94651221]
k= 75 xk= [0.40322511 0.15787327] f(xk)= [-0.43270865 -0.94344462]
k= 76 xk= [0.41704718 0.16925377] f(xk)= [-0.3860966 -0.93491704]
k= 77 xk= [0.4300686 0.18033373] f(xk)= [-0.34418922 -0.92505424]
k= 78 xk= [0.44351101 0.1921499 ] f(xk)= [-0.30541382 -0.9104218 ]
k= 79 xk= [0.45596 0.20341705] f(xk)= [-0.27054816 -0.89649514]
k= 80 xk= [0.46923896 0.2158001 ] f(xk)= [-0.23845909 -0.87701904]
k= 81 xk= [0.48085841 0.22691624] f(xk)= [-0.2095579 -0.86171446]
k= 82 xk= [0.49450418 0.24035192] f(xk)= [-0.18369471 -0.83649135]
k= 83 xk= [0.50433095 0.25022809] f(xk)= [-0.15987461 -0.82432328]
k= 84 xk= [0.52923466 0.27606143] f(xk)= [-0.08884812 -0.80558078]
k= 85 xk= [0.54817807 0.29670844] f(xk)= [-0.07244057 -0.75815082]
k= 86 xk= [0.55196258 0.30088075] f(xk)= [-0.06107966 -0.7563875 ]
k= 87 xk= [0.58409242 0.3383646 ] f(xk)= [-0.17778262 -0.55987076]
k= 88 xk= [0.57415074 0.32741847] f(xk)= [-0.33941812 -0.44612013]
k= 89 xk= [0.5759845 0.32975938] f(xk)= [-0.38752971 -0.39975147]
k= 90 xk= [0.54936923 0.30264256] f(xk)= [-1.08497164 0.16720094]
k= 91 xk= [0.59588566 0.35541025] f(xk)= [-0.88701248 0.06610646]
k= 92 xk= [0.62557699 0.38945577] f(xk)= [-0.27571094 -0.37815896]
k= 93 xk= [0.62659512 0.39086428] f(xk)= [-0.30639535 -0.35143459]
k= 94 xk= [0.60750698 0.37014954] f(xk)= [-1.04859775 0.21696188]
k= 95 xk= [0.63679781 0.40648354] f(xk)= [-0.97401477 0.19441837]
k= 96 xk= [0.67603801 0.4552325 ] f(xk)= [-0.16255821 -0.35897816]
k= 97 xk= [0.67756175 0.457579 ] f(xk)= [-0.23538087 -0.30218326]
k= 98 xk= [0.66719264 0.44635928] f(xk)= [-0.98940486 0.24265117]
k= 99 xk= [0.68116519 0.46516345] f(xk)= [-0.95848113 0.23548731]
k= 100 xk= [0.72613353 0.52567098] f(xk)= [-0.08332021 -0.31978466]
k= 101 xk= [0.72763167 0.52819241] f(xk)= [-0.17934169 -0.25108513]
k= 102 xk= [0.72088263 0.52080286] f(xk)= [-0.88438909 0.22621877]
k= 103 xk= [0.72930705 0.53299447] f(xk)= [-0.86394012 0.22113746]
k= 104 xk= [0.77143759 0.59395855] f(xk)= [-0.09998023 -0.2314799 ]
k= 105 xk= [0.772421 0.59561435] f(xk)= [-0.14005778 -0.20396923]
k= 106 xk= [0.7641847 0.58495593] f(xk)= [-0.77048152 0.19553579]
k= 107 xk= [0.77477743 0.60123328] f(xk)= [-0.74585811 0.19064376]
k= 108 xk= [0.81159937 0.65777517] f(xk)= [-0.07866333 -0.1836731 ]
k= 109 xk= [0.81222326 0.65888019] f(xk)= [-0.10705352 -0.16528704]
k= 110 xk= [0.80623427 0.65086575] f(xk)= [-0.66231087 0.17040917]
k= 111 xk= [0.81361762 0.66280639] f(xk)= [-0.64378248 0.16655104]
k= 112 xk= [0.84654107 0.71601533] f(xk)= [-0.09817672 -0.12329062]
k= 113 xk= [0.85700521 0.73465556] f(xk)= [-0.35373609 0.03952514]
k= 114 xk= [0.83758317 0.70213926] f(xk)= [-0.52374073 0.1187387 ]
k= 115 xk= [0.90388038 0.81436146] f(xk)= [ 0.76163845 -0.52765703]
k= 116 xk= [0.86767736 0.75453523] f(xk)= [-0.84467781 0.33424436]
k= 117 xk= [0.90054052 0.81082101] f(xk)= [-0.1440905 -0.03044198]
k= 118 xk= [0.90932277 0.82694243] f(xk)= [-0.20846799 0.01490863]
k= 119 xk= [0.87018732 0.75667299] f(xk)= [-0.06714585 -0.1105966 ]
k= 120 xk= [0.82135459 0.67121157] f(xk)= [ 0.76362535 -0.68235826]
k= 121 xk= [0.86037593 0.74128628] f(xk)= [-0.63700442 0.20790696]
k= 122 xk= [0.83873931 0.70465215] f(xk)= [-0.71455427 0.23370366]
k= 123 xk= [1.03860171 1.04308697] f(xk)= [14.86961426 -7.12131062]
k= 124 xk= [0.84811163 0.7215493 ] f(xk)= [-1.06909728 0.45119093]
k= 125 xk= [0.86097865 0.74383034] f(xk)= [-1.15489858 0.50922045]
k= 126 xk= [0.66388223 0.40558191] f(xk)= [ 8.66399552 -7.03154158]
k= 127 xk= [0.83144109 0.69757602] f(xk)= [-2.42627201 1.25634529]
k= 128 xk= [0.78711785 0.62687425] f(xk)= [-2.73036387 1.46394824]
k= 129 xk= [1.21594817 1.31516318] f(xk)= [ 79.89010983 -32.67335542]
k= 130 xk= [0.80183352 0.64738549] f(xk)= [-1.82311642 0.88970056]
k= 131 xk= [0.81130929 0.66135898] f(xk)= [-1.39515741 0.62724291]
k= 132 xk= [0.84379242 0.71145948] f(xk)= [-0.13482093 -0.10523573]
k= 133 xk= [0.8488667 0.72108011] f(xk)= [-0.47388572 0.1010872 ]
k= 134 xk= [0.83939571 0.70529745] f(xk)= [-0.56036629 0.14245826]
k= 135 xk= [0.90713661 0.81941977] f(xk)= [ 1.07593817 -0.69541067]
k= 136 xk= [0.86501385 0.74991553] f(xk)= [-0.84661343 0.33331324]
k= 137 xk= [0.89854797 0.80733425] f(xk)= [-0.18342061 -0.01084162]
k= 138 xk= [0.91010896 0.82821754] f(xk)= [-0.15037671 -0.01615486]
k= 139 xk= [0.92061081 0.84725285] f(xk)= [-0.05883515 -0.05428094]
k= 140 xk= [0.93145142 0.8677041 ] f(xk)= [-0.17522727 0.02046812]
k= 141 xk= [0.90995165 0.82823094] f(xk)= [-0.25978644 0.04378789]
k= 142 xk= [0.9791429 0.95553256] f(xk)= [ 1.20698881 -0.63765106]
k= 143 xk= [0.92368943 0.85431748] f(xk)= [-0.56470048 0.22306163]
k= 144 xk= [0.9419506 0.8881915] f(xk)= [-0.46295033 0.18411344]
k= 145 xk= [0.9577381 0.91746347] f(xk)= [-0.16160146 0.04023942]
k= 146 xk= [0.96701862 0.93506488] f(xk)= [-0.04270618 -0.01202489]
k= 147 xk= [0.97141166 0.94372517] f(xk)= [-0.0900311 0.01691066]
k= 148 xk= [0.96094308 0.92354637] f(xk)= [-0.12991058 0.026951 ]
k= 149 xk= [0.99570993 0.99061839] f(xk)= [ 0.3179641 -0.16397559]
k= 150 xk= [0.97199417 0.94517136] f(xk)= [-0.21102069 0.07973764]
k= 151 xk= [0.98787082 0.97590466] f(xk)= [-0.03054315 0.00318098]
k= 152 xk= [0.99093975 0.98195051] f(xk)= [-0.01372648 -0.00221709]
k= 153 xk= [0.99398549 0.98801939] f(xk)= [-0.016895 0.00244771]
k= 154 xk= [0.95542339 0.91169344] f(xk)= [ 0.3466759 -0.22808167]
k= 155 xk= [0.99166933 0.98353597] f(xk)= [-0.06740292 0.02558392]
k= 156 xk= [0.9849434 0.97039446] f(xk)= [-0.14080314 0.05619102]
k= 157 xk= [0.99794854 0.99583192] f(xk)= [ 0.02359149 -0.01387567]
k= 158 xk= [0.99621505 0.99248275] f(xk)= [-0.02284649 0.00766731]
k= 159 xk= [0.99710996 0.99424387] f(xk)= [-0.0119997 0.00311882]
k= 160 xk= [0.99816951 0.99634977] f(xk)= [-0.00661416 0.0014793 ]
k= 161 xk= [0.99951767 0.99903483] f(xk)= [-0.00066891 -0.00014795]
k= 162 xk= [0.99970257 0.99940648] f(xk)= [-0.001095 0.00025015]
k= 163 xk= [0.99900072 0.99800724] f(xk)= [-0.00391391 0.00095864]
k= 164 xk= [0.99997795 0.99995574] f(xk)= [ 1.74913071e-05 -3.08012179e-05]
k= 165 xk= [0.99997761 0.99995513] f(xk)= [-8.70938135e-06 -1.80379225e-05]
k= 166 xk= [0.99997777 0.99995565] f(xk)= [-9.03605856e-05 2.29477018e-05]
k= 167 xk= [0.99997745 0.99995502] f(xk)= [-9.16328188e-05 2.32707922e-05]
k= 168 xk= [1. 1.] f(xk)= [ 2.02059708e-07 -1.00387831e-07]
Jacobian after first solve:
[[ 116.48889476 -56.35457991]
[-225.91025031 112.73024426]]
second solve:
k= 1 xk= [0.56260435 1.12745333] f(xk)= [-183.36781519 162.18593492]
k= 2 xk= [1. 1.] f(xk)= [ 9.83445546e-08 -5.00097297e-08]
Jacobian after second solve:
[[ 380.39702492 -133.25513499]
[-329.18184311 142.82269653]]
You can see it takes 168 iterations the first time, and only 2 iterations the second time when re-using the Jacobian from the first time.
I found that using the "Wolfe conditions" linesearch, and using "Broyden's first method" (rather than Broyden's second method) tends to make the solver more robust here.

I think it should be possible using a decorator. See below. Depending on the tolerance or whether the gradient function is being called the first time, the gradient is either recomputed or returned from cache.
import numpy as np
from scipy.optimize import minimize
aa = 3.14
#decorator
def smartgrad(func):
def wrapped(*args,**kwargs):
distance = abs(args[0]-wrapped.oldstate)
if ( wrapped.firstcall == True) or (distance > wrapped.tolerance):
print('recomputing...')
newgrad = func(*args,**kwargs)
wrapped.firstcall = False
wrapped.oldgrad = newgrad
wrapped.oldstate = args[0]
return newgrad
else:
print('returning from cache...')
return wrapped.oldgrad
wrapped.firstcall = True
wrapped.tolerance = 1E-1
wrapped.oldgrad = 0.0
wrapped.oldstate = 0.0
return wrapped
def ff(xx):
return (xx-aa)**2
#smartgrad
def grad(xx):
return 2*(xx-aa)
if __name__ =='__main__':
gg=grad(1.0); # gradient is recomputed
print(gg)
gg=grad(1.05) # gradient is returned from cache
print(gg)
gg=grad(1.11) # gradient is recomputed
print(gg)

Related

Write a table object into csv in matlab

I have a table object in Matlab with cells as shown in the snapshot:
The cells inside Land and Seamark are as below:
The class of the objects are as below:
>> class(FileData.gTruth.LabelData.Land)
ans =
'cell'
>> class(FileData.gTruth.LabelData.Land{1,1})
ans =
'double'
>> class(FileData.gTruth.LabelData)
ans =
'table'
I tried some syntax like writetable and csvwrite but i am not getting the right format of output. The reading of the Land and Seamark as shown in the figure gets jumbled(reading is columnwise and not row-wise).
I want my output to be in this order:
[1063 126 115 86] [1 169 158 147;1 104 165 66;728 105 276 43;950 113 971 40;1 107 810 23;227 133 48 15;618 131 107 20] [562 220 33 51;1736 167 26 28;532 130 18 15;393 129 23 14]
Code so far:
writetable(FileData.gTruth.LabelData,'labelled1.txt','Delimiter' , ';');
You can simply use reshape on the transpose of the two-dimensional matrices to build a new table:
Ship = [1063 126 115 86]
Land = {[1 169 158 147;1 104 165 66; 728 105 276 43; 950 113 971 40; 1 107 810 23; 227 133 48 15; 618 131 107 20]}
Seamark = {[562 220 33 51; 1736 167 26 28; 532 130 18 15; 393 129 23 14]}
t = table(Ship,Land,Seamark);
t2 = table(t.Ship,reshape(t.Land{:}.',1,[]),reshape(t.Seamark{:}.',1,[]))
writetable(t2,'mycsv.csv','WriteVariableNames',false)
The first and only row of mycsv.csv file is:
1063 126 115 86 1 169 158 147 1 104 165 66 728 105 276 43 950 113 971 40 1 107 810 23 227 133 48 15 618 131 107 20 562 220 33 51 1736 167 26 28 532 130 18 15 393 129 23 14
I used the WriteVariableNames,false Name-Value pair to indicate that the variable names are not to be included in the first row of the file.

Keras LSTM: modeling the layers

I'm new to Keras LSTM and I building a script for prediction based on time series.
I have the data set below which describe users' power consumption in a year
User mai/13 jun/13 jul/13 ago/13 set/13 out/13 nov/13 dez/13 jan/14 fev/14 mar/14 abr/14
x1 88 99 108 90 86 79 83 75 80 69 75 61
x2 0 143 163 127 174 199 177 140 100 100 130 119
x3 51 50 41 42 51 48 58 53 53 37 47 41
x4 181 211 166 105 128 123 125 98 114 58 4 0
x5 173 187 180 195 211 212 231 188 193 168 166 0
x6 343 321 293 272 400 397 467 392 409 306 408 353
x7 215 150 161 148 153 130 165 166 198 166 150 148
x8 100 140 132 119 121 125 148 135 144 123 124 16
x9 197 193 181 161 201 161 227 154 176 148 171 172
x10 356 371 347 347 363 377 423 373 395 338 337 302
For each user, my script split the data in windows with size n, i.e., if n=6, user x1 will have the following dataset
88 99 108 90 86 79
99 108 90 86 79 83
108 90 86 79 83 75
90 86 79 83 75 80
86 79 83 75 80 69
79 83 75 80 69 75
83 75 80 69 75 61
I want my net to learn from the patterns of each window, so when I'm going to test it, based on a new window, it predicts a value considering the previous values.
Considering these variables:
dataset_length => number of months
dataset_dim => number of users
sequence_length => window size
The net I'm thinking about is:
1st layer: LSTM with sequence_length units, input_shape(None, 1) and return_sequences set true
2nd layer: LSTM with sequence_length*2 units and return_sequences set false
3rd layer: Dense with 1 unit
Is there anything wrong on my net layout? I'm testing the net with a little bit more data and not getting a very good result (high loss). What can I change for a better prediction? Do I need more layers?
Please let me know if you need some piece of code or the test results.

changing the range / limits on a polar chart in octave / matlab

I'm using Octave 4.0 using Linux which is similar to Matlab
Is it possible to have a different range of numbers on a polar chart and have them show up along with the degrees also?
The normal polar plot goes from 0-359 degrees shown in black in image, I would like the range and tick values to be from 0 to 100 shown in red in the image is this possible? If so can two ranges be shown on a polar plot at the same time (0-359 and 0-100) almost like plotting 2 y axis using plotyy on the same plot?
See image below of polar plot
Here's the the numbers 0-359 and there corresponding numbers 0-100 matching up.
0 0
1 0.27855
2 0.5571
3 0.83565
4 1.11421
5 1.39276
6 1.67131
7 1.94986
8 2.22841
9 2.50696
10 2.78552
11 3.06407
12 3.34262
13 3.62117
14 3.89972
15 4.17827
16 4.45682
17 4.73538
18 5.01393
19 5.29248
20 5.57103
21 5.84958
22 6.12813
23 6.40669
24 6.68524
25 6.96379
26 7.24234
27 7.52089
28 7.79944
29 8.07799
30 8.35655
31 8.6351
32 8.91365
33 9.1922
34 9.47075
35 9.7493
36 10.0279
37 10.3064
38 10.585
39 10.8635
40 11.1421
41 11.4206
42 11.6992
43 11.9777
44 12.2563
45 12.5348
46 12.8134
47 13.0919
48 13.3705
49 13.649
50 13.9276
51 14.2061
52 14.4847
53 14.7632
54 15.0418
55 15.3203
56 15.5989
57 15.8774
58 16.156
59 16.4345
60 16.7131
61 16.9916
62 17.2702
63 17.5487
64 17.8273
65 18.1058
66 18.3844
67 18.663
68 18.9415
69 19.2201
70 19.4986
71 19.7772
72 20.0557
73 20.3343
74 20.6128
75 20.8914
76 21.1699
77 21.4485
78 21.727
79 22.0056
80 22.2841
81 22.5627
82 22.8412
83 23.1198
84 23.3983
85 23.6769
86 23.9554
87 24.234
88 24.5125
89 24.7911
90 25.0696
91 25.3482
92 25.6267
93 25.9053
94 26.1838
95 26.4624
96 26.7409
97 27.0195
98 27.2981
99 27.5766
100 27.8552
101 28.1337
102 28.4123
103 28.6908
104 28.9694
105 29.2479
106 29.5265
107 29.805
108 30.0836
109 30.3621
110 30.6407
111 30.9192
112 31.1978
113 31.4763
114 31.7549
115 32.0334
116 32.312
117 32.5905
118 32.8691
119 33.1476
120 33.4262
121 33.7047
122 33.9833
123 34.2618
124 34.5404
125 34.8189
126 35.0975
127 35.376
128 35.6546
129 35.9331
130 36.2117
131 36.4903
132 36.7688
133 37.0474
134 37.3259
135 37.6045
136 37.883
137 38.1616
138 38.4401
139 38.7187
140 38.9972
141 39.2758
142 39.5543
143 39.8329
144 40.1114
145 40.39
146 40.6685
147 40.9471
148 41.2256
149 41.5042
150 41.7827
151 42.0613
152 42.3398
153 42.6184
154 42.8969
155 43.1755
156 43.454
157 43.7326
158 44.0111
159 44.2897
160 44.5682
161 44.8468
162 45.1253
163 45.4039
164 45.6825
165 45.961
166 46.2396
167 46.5181
168 46.7967
169 47.0752
170 47.3538
171 47.6323
172 47.9109
173 48.1894
174 48.468
175 48.7465
176 49.0251
177 49.3036
178 49.5822
179 49.8607
180 50.1393
181 50.4178
182 50.6964
183 50.9749
184 51.2535
185 51.532
186 51.8106
187 52.0891
188 52.3677
189 52.6462
190 52.9248
191 53.2033
192 53.4819
193 53.7604
194 54.039
195 54.3175
196 54.5961
197 54.8747
198 55.1532
199 55.4318
200 55.7103
201 55.9889
202 56.2674
203 56.546
204 56.8245
205 57.1031
206 57.3816
207 57.6602
208 57.9387
209 58.2173
210 58.4958
211 58.7744
212 59.0529
213 59.3315
214 59.61
215 59.8886
216 60.1671
217 60.4457
218 60.7242
219 61.0028
220 61.2813
221 61.5599
222 61.8384
223 62.117
224 62.3955
225 62.6741
226 62.9526
227 63.2312
228 63.5097
229 63.7883
230 64.0669
231 64.3454
232 64.624
233 64.9025
234 65.1811
235 65.4596
236 65.7382
237 66.0167
238 66.2953
239 66.5738
240 66.8524
241 67.1309
242 67.4095
243 67.688
244 67.9666
245 68.2451
246 68.5237
247 68.8022
248 69.0808
249 69.3593
250 69.6379
251 69.9164
252 70.195
253 70.4735
254 70.7521
255 71.0306
256 71.3092
257 71.5877
258 71.8663
259 72.1448
260 72.4234
261 72.7019
262 72.9805
263 73.2591
264 73.5376
265 73.8162
266 74.0947
267 74.3733
268 74.6518
269 74.9304
270 75.2089
271 75.4875
272 75.766
273 76.0446
274 76.3231
275 76.6017
276 76.8802
277 77.1588
278 77.4373
279 77.7159
280 77.9944
281 78.273
282 78.5515
283 78.8301
284 79.1086
285 79.3872
286 79.6657
287 79.9443
288 80.2228
289 80.5014
290 80.7799
291 81.0585
292 81.337
293 81.6156
294 81.8942
295 82.1727
296 82.4513
297 82.7298
298 83.0084
299 83.2869
300 83.5655
301 83.844
302 84.1226
303 84.4011
304 84.6797
305 84.9582
306 85.2368
307 85.5153
308 85.7939
309 86.0724
310 86.351
311 86.6295
312 86.9081
313 87.1866
314 87.4652
315 87.7437
316 88.0223
317 88.3008
318 88.5794
319 88.8579
320 89.1365
321 89.415
322 89.6936
323 89.9721
324 90.2507
325 90.5292
326 90.8078
327 91.0864
328 91.3649
329 91.6435
330 91.922
331 92.2006
332 92.4791
333 92.7577
334 93.0362
335 93.3148
336 93.5933
337 93.8719
338 94.1504
339 94.429
340 94.7075
341 94.9861
342 95.2646
343 95.5432
344 95.8217
345 96.1003
346 96.3788
347 96.6574
348 96.9359
349 97.2145
350 97.493
351 97.7716
352 98.0501
353 98.3287
354 98.6072
355 98.8858
356 99.1643
357 99.4429
358 99.7214
359 100
Here's an image of the numbers 0-359 and there corresponding numbers 0-100 matching up.
Numbers matching up
The polar plot object in Octave adds the rtick and ttick properties to the parent axes which allows you to change the location of the ticks, however, there is unfortunately no tticklabel property that we can use to easily find and modify the theta tick marks.
Instead, we can plot your plot that you want the theta range to be 0 - 100 by first transforming your data to instead be 0 - 2*pi (as is expected by polar). Then after plotting both, we can use findall to locate all text objects, figure out which ones are the theta ticks, create a copy of them and modify one of the sets to appear to be 0 - 100.
% Your first plot is going to use the 0 - 2pi range for theta
theta1 = linspace(0, 2*pi, 1000);
rho1 = sin(theta1 * 5);
plot1 = polar(theta1, rho1);
hold on
% For your second plot, just transform your 0 - 100 range to be 0 - 360 instead
theta2 = 0:100;
rho2 = linspace(0, 1, numel(theta2));
modtheta2 = 2*pi * (theta2 ./ 100);
plot2 = polar(modtheta2, rho2);
% Now we need to modify all of the labels
% Find all of the original labels
labels = findall(gca, 'type', 'text');
% Figure out which ones are the radial labels. To do this we compute the distance
% from the center of the plot and find the most common distance
distances = cellfun(#(x)norm(x(1:2)), get(labels, 'Position'));
% Figure out the most common
[~, ~, b] = unique(round(distances * 100));
h = hist(b, 1:max(b));
labels = labels(b == find(h == max(h)));
% Make a copy of these labels (have to use arrayfun for 4.0.x compatibility)
blacklabels = arrayfun(#(L)copyobj(L, gca), labels);
% Shift these labels outward by 15%
arrayfun(#(x)set(x, 'Position', get(x, 'Position') * 1.15), blacklabels);
% Now set the other labels to red and change their values
set(labels, 'COlor', 'red')
for k = 1:numel(labels)
value = str2num(get(labels(k), 'String'))
% Convert the value to be between 0 and 100
newvalue = 100 * (value / 360);
set(labels(k), 'String', sprintf('%0.2f', newvalue))
end

execution of parforlop in matlab

I have sample code emulating my actual code. Where I have cell arrays outside the parfor loop. I have to perform computations on strings and numerical outputs will be stored in arrays which I can write to a csv file after each parfor loop. So I made dummy code. But I couldn't get it to execute. The error message is: "subscription mismatch at line 6".
ftemp=fopen('temp.csv','w');
march=cell(1,20);tc=0;
march={'ab' 'cd' 'ef' 'gh' 'ij' 'kl' 'mn' 'op' 'qr' 'st' 'uv' 'AB' 'CD' 'EF' 'GH' 'IJ' 'KL' 'MN' 'OP' 'QR'};
for i=1:10
matlabpool open 4;
parfor j=1:1:20
a(j,1)=randi(200,1,1);
b(j,2)=j+tc;
c(j,3)=march{1,j};
d(j,4)=(randi(200,1,1)/200);
end
fprintf(ftemp,'%d\t%d\t%s\t%f',a,b,c,d);
matlabpool close
clear a b c d;
tc=tc+20;
end
fclose(ftemp);
quit
The cause of the Error is that you are trying to assign a cell into an array in line 9.
I made some changes into your code, they are described in comments.
ftemp=fopen('temp.csv','w');
march=cell(1,20);tc=0;
march={'ab' 'cd' 'ef' 'gh' 'ij' 'kl' 'mn' 'op' 'qr' 'st' 'uv' 'AB' 'CD' 'EF' 'GH' 'IJ' 'KL' 'MN' 'OP' 'QR'};
for i=1:10
parpool local; % here in my version of Matlab 2015 there are no more "matlabpool" if it doesen't work change it back
parfor j=1:20
%i changed variable a(j,1) into a(j) and b(j,2) into b(j): may
%contain empty arrays , idem: for c and d
a(j)=randi(200,1,1);
b(j)=j+tc;
c{j}=march{1,j}; % changed c(j)= march{1,j}; : cause of error
d(j)=randi(200,1,1)/200;
end
fprintf(ftemp,'%d\t%d\t%c\t%d',a,b,char(c),d); % char(c) in order to convert cell array to array of strings
delete(gcp) % there isn't such thing "matlabpool close" , the right expression is "delete(gcp)"
clear a b c d;
tc=tc+20;
end
fclose(ftemp);
%quit : i remove because it closes matlab , please put it back if u really
%want to close matlab after operation
Note: if parpool doesent work for your version of matlab , please change it back to the expression you were using matlabpool open 4;
Well for the unexpected outputs and number of rows in the output , it's because the wrong use of fprintf . We need to print element by element , which means the fprintf has to be inside the parfor loop, so your code should look like this:
ftemp=fopen('temp.csv','w');
march=cell(1,20);tc=0;
march={'ab';'cd';'ef';'gh';'ij';'klm';'mn';'op';'qr';'st';'uv';'ABls';'CD';'E3F';'GH';'IJ';'dynaKL';'MN';'OP';'QR'};
matlabpool open 4;
for k=1:10
%parpool local; % here in my version of Matlab 2015 there are no more "matlabpool" if it doesen't work change it back
parfor j=1:20
%i changed variable a(j,1) into a(j) and b(j,2) into b(j): may
%contain empty arrays , idem: for c and d
a(j)=randi(200,1,1);
b(j)=j+tc;% indexing purposes to identify the order when parallel processing going on
if length(march{j})>2 % this kind of conditions and computations are there in my actual code
c{j}='skip';
else
c{j}=march{j};
end
%d(j)=randi(200,1,1)/200;
fprintf(ftemp,'%d\t%d\t%s\t\n',a(j),b(j),c{j}); % char(c) in order to convert cell array to array of strings
end
clear a b c;
tc=tc+20;
end
fclose(ftemp);
matlabpool close;
the output is:
9 1 ab
171 2 cd
7 3 ef
98 4 gh
102 5 ij
20 6 skip
53 7 mn
174 8 op
36 9 qr
30 10 st
130 11 uv
127 12 skip
133 13 CD
65 14 skip
118 15 GH
130 16 IJ
139 17 skip
168 18 MN
57 19 OP
25 20 QR
22 21 ab
39 22 cd
83 23 ef
22 24 gh
159 25 ij
40 26 skip
164 27 mn
78 28 op
194 29 qr
88 30 st
125 31 uv
1 32 skip
2 33 CD
112 34 skip
161 35 GH
170 36 IJ
55 37 skip
59 38 MN
18 39 OP
134 40 QR
80 41 ab
118 42 cd
108 43 ef
174 44 gh
97 45 ij
157 46 skip
85 47 mn
98 48 op
40 49 qr
11 50 st
171 51 uv
139 52 skip
90 53 CD
70 54 skip
173 55 GH
150 56 IJ
186 57 skip
155 58 MN
136 59 OP
96 60 QR
158 61 ab
118 62 cd
124 63 ef
127 64 gh
26 65 ij
124 66 skip
91 67 mn
186 68 op
63 69 qr
137 70 st
170 71 uv
98 72 skip
132 73 CD
80 74 skip
160 75 GH
20 76 IJ
156 77 skip
142 78 MN
110 79 OP
51 80 QR
18 81 ab
29 82 cd
40 83 ef
49 84 gh
102 85 ij
113 86 skip
96 87 mn
44 88 op
166 89 qr
90 90 st
21 91 uv
60 92 skip
44 93 CD
166 94 skip
103 95 GH
123 96 IJ
77 97 skip
163 98 MN
138 99 OP
111 100 QR
94 101 ab
133 102 cd
158 103 ef
13 104 gh
26 105 ij
117 106 skip
90 107 mn
58 108 op
156 109 qr
79 110 st
196 111 uv
168 112 skip
192 113 CD
160 114 skip
56 115 GH
129 116 IJ
191 117 skip
157 118 MN
170 119 OP
30 120 QR
137 121 ab
128 122 cd
12 123 ef
38 124 gh
38 125 ij
122 126 skip
97 127 mn
129 128 op
142 129 qr
154 130 st
99 131 uv
85 132 skip
129 133 CD
111 134 skip
108 135 GH
78 136 IJ
102 137 skip
48 138 MN
100 139 OP
89 140 QR
114 141 ab
12 142 cd
184 143 ef
145 144 gh
5 145 ij
48 146 skip
92 147 mn
64 148 op
87 149 qr
32 150 st
136 151 uv
103 152 skip
90 153 CD
73 154 skip
28 155 GH
191 156 IJ
63 157 skip
120 158 MN
104 159 OP
178 160 QR
148 161 ab
36 162 cd
129 163 ef
11 164 gh
172 165 ij
186 166 skip
145 167 mn
142 168 op
150 169 qr
185 170 st
14 171 uv
141 172 skip
22 173 CD
163 174 skip
48 175 GH
164 176 IJ
117 177 skip
25 178 MN
110 179 OP
111 180 QR
175 181 ab
60 182 cd
195 183 ef
44 184 gh
163 185 ij
4 186 skip
103 187 mn
95 188 op
127 189 qr
10 190 st
11 191 uv
182 192 skip
162 193 CD
179 194 skip
76 195 GH
104 196 IJ
153 197 skip
103 198 MN
4 199 OP
154 200 QR
Also the output has 200 rows.
ftemp=fopen('temp.csv','w');
march=cell(1,20);tc=0;
march={'ab';'cd';'ef';'gh';'ij';'klm';'mn';'op';'qr';'st';'uv';'ABls';'CD';'E3F';'GH';'IJ';'dynaKL';'MN';'OP';'QR'};
matlabpool open 4;
for k=1:10
%parpool local; % here in my version of Matlab 2015 there are no more "matlabpool" if it doesen't work change it back
parfor j=1:20
%i changed variable a(j,1) into a(j) and b(j,2) into b(j): may
%contain empty arrays , idem: for c and d
a(j)=randi(200,1,1);
b(j)=j+tc;% indexing purposes to identify the order when parallel processing going on
if length(march{j})>2 % this kind of conditions and computations are there in my actual code
c{j}='skip';
else
c{j}=march{j};
end
%d(j)=randi(200,1,1)/200;
end
fprintf(ftemp,'%d\t%d\t%s\n',a,b,c{:}); % char(c) in order to convert cell array to array of strings
clear a b c;
tc=tc+20;
end
fclose(ftemp);
matlabpool close;
but the output of the program i coudlnt get it
130 127 Abf5ȱ3³]À#¾
1 2

97 98 cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67 68 skip
71 72 IJ
115 107 ip
77 78 OP
81 82 168 57 ®$ «E�,vp|}¤
21 22 !"#$%&'(
97 98 cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67 68 skip
71 72 IJ
115 107 ip
77 78 OP
81 82 88 125 S([g 0'0Â"
41 42 +,-./0123456789:;<
97 98 cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67 68 skip
71 72 IJ
115 107 ip
77 78 OP
81 82 161 170 7Na�U;6´q¶nl®v¤
61 62 ?#ABCDEFGHIJKLMNOP
97 98 cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67 68 skip
71 72 IJ
115 107 ip
77 78 OP
81 82 171 139 ZF­ºPµ~R(`b[
81 82 STUVWXYZ[\]^_`abcd
97 98 cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67 68 skip
71 72 IJ
115 107 ip
77 78 OP
81 82 132 80 ||£)Á1[ªavb-
101 102 ghijklmnopqrstuvwx
97 98 cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67 68 skip
71 72 IJ
115 107 ip
77 78 OP
81 82 186 63 ª`neepw»fq°r1
121 122 {|}~�
97 98 cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67 68 skip
71 72 IJ
115 107 ip
77 78 OP
81 82 51 160 M(1´uOAF¦{¬©
141 142 ��� 
97 98 cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67 68 skip
71 72 IJ
115 107 ip
77 78 OP
81 82 90 21 <,uZgh�lu¨B,¦
0
161 162 £¤¥¦§¨©ª«¬­®¯°±²³´
97 98 cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67 68 skip
71 72 IJ
115 107 ip
77 78 OP
81 82 192 160 o£¿^hyµÄ¨�Ä�O
181 182 ·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈ
97 98 cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67 68 skip
71 72 IJ
115 107 ip
77 78 OP
81 82
i mean to say why only 124 iterations took place instead of 200. and why these arbitrary outputs are there in the 3rd column

Matlab getting point coordinates

How can I get the coordinates at a specific point?
I want to get the X coordinate of the points with Y = 18.1 ; Y = 33; Y = 70
Those points need to lie on the function that I plot.
Sample Code
t = [0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180];
y = [0 5 9 19 25 32 46 65 79 90 100 115 123 141 153 159 160 171 181 185 193 200 205 211 215 220 223 222 225 224 228 231 231 228 235 234 231];
plot(t,y) , grid on
Unfortunately, you are trying to find the values of t associated with a y, and your function is not monotonic so we need to actually code up a mock linear interpolation. Note, there may be a better way, but I do not know of it right now. Try the following code where yVals are the values you want an associated t for, and possArray will include all values of t that may satisfy those conditions.
clc; close all; clear all;
t = [0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160 165 170 175 180];
y = [0 5 9 19 25 32 46 65 79 90 100 115 123 141 153 159 160 171 181 185 193 200 205 211 215 220 223 222 225 224 228 231 231 228 235 234 231];
plot(t,y)
grid on
hold on
yVals = [18.1,33,70,222.5,230];
possArray = cell(1,numel(yVals));
iter = 1;
for val = yVals;
poss = [];
possNum = 1;
for i = 1:numel(y)-1
if y(i) <= val && y(i+1) >= val
minDiff = val-y(i);
yDiff = y(i+1)-y(i);
percAlong = minDiff/yDiff;
poss(possNum) = (t(i+1)-t(i))*percAlong+t(i);
possNum = possNum+1;
end
end
possArray{iter} = poss;
iter = iter + 1;
end
colors = hsv(numel(yVals));
legendCell = cell(numel(yVals)+1,1);
legendCell{1} = 'Original Line';
for i = 1:numel(yVals)
plot(possArray{i},yVals(i)*ones(size(possArray{i})),...
'x','MarkerSize',10,'LineWidth',2,'Color',colors(i,:))
legendCell{i+1} = ['Values for Y = ' ,num2str(yVals(i))];
end
legend(legendCell)
hold off
As stated previously, this is linear interpolation, so if you need it to be more complicated that is on you, the concept should however be similar
UPDATE
Updated code above to be a little more clean, and added a plot indicating that multiple possibilities may arise for a single value, and that the code will return all possibilities.