/ GRADIENT DESCENT / We generate data based on a random weight matrix. / Then we train on that data and see what the correlation is. / generate data numweights: 20; numdatapoints: 5000; eta: 0.001; weights: 1 - 2*(numweights ? 1.0); datamat: (); target: (); / targets based on weight matrix do[numdatapoints; x: 10 * (numweights ? 1.0); datamat,: enlist x; y: sum weights * x; y+: first 1 - 2 * 1 ? 1.0; / randomize target target,: y]; / So, now we have datamat and target value. / Later we can randomize target to throw in noise. std:{sqrt var[x]} corr:{ (cov[x;y])%((std[x]) * (std[y]))} graddescent:{[datamat; target] candweights: 1 - 2*(numweights ? 1.0); origcorr: corr[candweights; weights]; / original correlation errorvec: () i: 0; while[i < count datamat; predict: sum candweights * datamat[i]; error: (target[i] - predict) % numweights; errorvec,: error; influence: error * eta * datamat[i]; candweights+: influence; / candweights-: avg candweights; i+:1]; newcorr: corr[candweights; weights]; (origcorr; newcorr; candweights)} trip: graddescent[datamat; target]; ("Original correlation: " ), string trip[0] ("Correlation after training: "),string trip[1]