jsdoでtensorflow.js その14

#概要

jsdoでtensorflow.jsやってみた。
学習したウェイトとバイアスを使って、tensorflow.jsを使わないで7segmentLEDを分類してみた。

#写真

![](http://jsdo-static-contents.s3.amazonaws.com/images/capture/g/G/f/gGfP.jpg?t=1540354224)

#サンプルコード

```
var canvas = document.getElementById('canvas');
canvas.width = 500;
canvas.height = 200;
var context = canvas.getContext('2d');
function draw7SegLED(properties) {
     var context = properties.context;
     var x = properties.x;
     var y = properties.y;
     var w = properties.width;
     var h = properties.height;
     var seg = properties.seg;
     function drawHorizontal(left, top, width, height) {
         var w_ = width;
         var h_ = height / 2;
         var x_ = left;
         var y_ = top + h_;
         context.moveTo(x_ , y_);
         context.lineTo(x_ + 5, y_ - h_);
         context.lineTo(x_ + w_ - 5, y_ - h_);
         context.lineTo(x_ + w_, y_);
         context.lineTo(x_ + w_ - 5, y_ + h_);
         context.lineTo(x_ + 5, y_ + h_);
         context.fill();
     }
     function drawVertical(left, top, width, height) {
         var w_ = width / 2;
         var h_ = height;
         var x_ = left + w_;
         var y_ = top;
         context.moveTo(x_, y_);
         context.lineTo(x_ + w_, y_ + 5);
         context.lineTo(x_ + w_, y_ + h_ - 5);
         context.lineTo(x_, y_ + h_);
         context.lineTo(x_ - w_, y_ + h_ - 5);
         context.lineTo(x_ - w_, y_ + 5);
         context.fill();
     }
     function drawDp(left, top, radius) {
         context.moveTo(left, top);
         context.arc(left, top, radius, 0, Math.PI * 2, false);
         context.fill();
     }
     context.fillStyle = properties.backColor;
     context.beginPath();
     context.fillRect(x, y, w, h);
     context.fillStyle = properties.fontColor;
     var canvasMargin = 5;
     var barMargin = 5;
     var barWeight = 10;
     var a = {
         x: x + canvasMargin,
         y: y + canvasMargin,
         w: w - (canvasMargin * 2) - (barWeight / 2) - barMargin
     };
     var b = {
         x: x + w - canvasMargin - (barMargin * 2) - (barWeight / 2),
         y: y + (barMargin * 2) + canvasMargin,
         h: (h / 2) - (barWeight * 2) - (canvasMargin * 2) + (barMargin * 3) - (barMargin / 2)
     };
     var c = {
         y: y + (barMargin * 2) + (h / 2) - (barWeight * 2) + (barMargin * 3) + (barMargin / 2)
     };
     var e = {
         x: x + canvasMargin - barMargin
     };
     var dp = {
         x: x + w - (barWeight / 2),
         y: y + h - (barWeight / 2),
         radius: barWeight / 2
     };
     if (seg.a) drawHorizontal(a.x, a.y, a.w, barWeight);
     if (seg.b) drawVertical(b.x, b.y, barWeight, b.h);
     if (seg.c) drawVertical(b.x, c.y, barWeight, b.h);
     if (seg.d) drawHorizontal(a.x, y + h - canvasMargin * 2, a.w, barWeight);
     if (seg.e) drawVertical(e.x, c.y, barWeight, b.h);
     if (seg.f) drawVertical(e.x, y + canvasMargin + barMargin * 2, barWeight, b.h);
     if (seg.g) drawHorizontal(a.x, y + h / 2 - barMargin / 2, a.w, barWeight);
     if (seg.dp) drawDp(dp.x, dp.y, dp.radius);                                              
}
var w1 = [
    0.153122678399086,0.5944046378135681,-0.08533942699432373,0.18124920129776,-0.08094681054353714,0.24508264660835266,-1.1661443710327148,0.5707349181175232,-1.8759199380874634,-0.40594616532325745,0.4383302330970764,-0.5592394471168518,-1.0399842262268066,1.0180584192276,0.9884814620018005,0.10749341547489166,-0.48740941286087036,0.09538574516773224,-0.6637247800827026,-0.6662670373916626,0.2639852464199066,0.5875835418701172,0.5962975025177002,-1.215815782546997,-0.010047351941466331,0.45461151003837585,2.2610526084899902,0.5972310900688171,1.1126104593276978,0.24319806694984436,0.9081928133964539,-0.522864818572998,0.22849087417125702,0.15914557874202728,-0.6874650716781616,0.07943246513605118,-0.25611844658851624,0.12877602875232697,1.134350299835205,0.22664140164852142,2.7824630737304688,0.23774012923240662,-1.2080574035644531,-1.189767837524414,0.7316118478775024,1.3292620182037354,0.5558644533157349,0.8544521927833557,-0.21388454735279083,1.1940791606903076,0.9684807062149048,1.6892057657241821,0.6000819206237793,1.7941160202026367,-0.948235273361206,-0.130396768450737,-0.30800512433052063,0.3201132118701935,1.5979706048965454,1.7607431411743164,0.3452439308166504,-0.21823996305465698,-0.09050583839416504,-0.6499814987182617,-0.6038988828659058,0.42143547534942627,-0.5568815469741821,0.2629009783267975,1.9101674556732178,0.02825801819562912,0.841042160987854,-0.46732187271118164,0.7559794187545776,-0.43253329396247864,-0.5281680822372437,-0.38666367530822754,-2.328017234802246,-2.3020284175872803,-0.568560004234314,-0.6035656332969666,-2.1688072681427,-0.9791187644004822,-1.3431447744369507,-0.304378479719162,0.6189827919006348,0.08975015580654144,0.4176925718784332,-0.8470453023910522,0.10019742697477341,-0.6708015203475952,-0.3862525522708893,0.17411638796329498,-0.5187639594078064,-0.6287205219268799,-0.1851566582918167,0.04810098558664322,-1.6666442155838013,0.3929312527179718,-0.34002605080604553,-1.138098120689392,0.6858562231063843,1.9690628051757812,0.2567981779575348,-0.49866676330566406,-0.31581664085388184,-1.2181892395019531,-0.6354829668998718,-0.5702751874923706,-1.6169120073318481,-0.3199160099029541,0.3872409760951996,0.52848881483078,0.000409649102948606,0.5372066497802734,0.5524466037750244,0.29392340779304504,1.0459809303283691,-1.7781684398651123,-0.365347683429718,0.9146674871444702,-0.8583271503448486,-0.04612934961915016,0.8220736384391785,0.9281856417655945,-0.2916054427623749,0.7537029981613159,-2.2108685970306396,0.9792181253433228,-1.1753073930740356,-1.490282416343689,1.6843359470367432,1.2421292066574097,-0.2906726002693176,-0.7520861625671387,1.847747564315796,0.7936442494392395,-1.7898999452590942,1.5255537033081055,-1.034895420074463,0.7279852628707886,-0.23684155941009521,1.1550313234329224,-0.824001133441925,-1.3300334215164185,-0.5770341753959656,0.9206408858299255,1.9914913177490234,-2.805525541305542,0.397225946187973,-0.18921589851379395,-1.61776864528656,1.280743956565857,-0.6781944632530212,-0.4299599528312683,1.1042940616607666,-0.8882224559783936,-1.542972445487976,0.4022047221660614,-0.613456666469574,1.400730013847351,-0.1778920441865921,0.3729200065135956,0.9369065761566162,0.548341691493988,-0.7680536508560181,0.11413178592920303,-0.21814580261707306,0.8284967541694641,1.2669278383255005,-0.9432098269462585,0.423269659280777,-1.2704014778137207,-0.09357868880033493,-0.3728669285774231,-0.5526217818260193,0.34031665325164795,0.16526947915554047,-0.3214572072029114,-0.6424847841262817,0.8166301250457764,1.6166009902954102,0.6518601179122925,-1.205533504486084,1.6274030208587646,-1.0835256576538086,-0.3252986669540405,0.14625929296016693,-1.1987724304199219,-0.7098519206047058,1.0391952991485596,-0.4695970416069031,1.082233190536499,1.3972924947738647,1.2237237691879272,-0.8246264457702637,0.3751767575740814,0.14311052858829498,0.5651769638061523,0.01152600534260273,-1.1359673738479614,-0.9212192893028259,0.813730776309967,1.539064645767212,-0.5926545262336731,1.9650321006774902,-0.14725781977176666,-1.2916017770767212,0.4936199486255646,-1.9820622205734253,1.3655132055282593,-1.8336457014083862,-0.0053100562654435635,-0.17887383699417114,-0.4263961911201477,0.31987711787223816,-0.6333395838737488,-2.521745443344116,-0.1107807606458664,-0.8430693745613098,-0.027018370106816292,0.13554786145687103,0.3604452311992645,0.25579842925071716,0.3132382333278656,-1.3582007884979248,1.5307823419570923,0.1349606215953827,0.39881381392478943,-1.4450242519378662,-0.17425823211669922,-0.20863045752048492,-0.2571221888065338,0.4466306269168854,1.2426456212997437,0.4896466135978699,-0.5627720355987549,0.8949769139289856,-0.6091416478157043,-0.41257649660110474,-0.2445770800113678,-1.4257031679153442,3.00837779045105,0.6263371706008911,-0.04621727019548416,0.8629324436187744,0.5099217295646667,-0.727993905544281,-1.0691624879837036,0.12231938540935516,0.5598771572113037,-3.1807126998901367,0.7703575491905212,0.4753929376602173,-0.46033039689064026,-0.6392017006874084,1.00064218044281,0.7502865195274353,1.3640947341918945,0.28825098276138306,0.6357626914978027,0.44751232862472534,-1.157153844833374,-0.48374509811401367,0.6037033200263977,0.00309889018535614,0.4570114314556122,0.12445496767759323,0.5852152705192566,-0.19481472671031952,0.15736566483974457,1.933010458946228,0.49203136563301086,-0.7640934586524963,0.34594038128852844,0.43069350719451904,-1.533843994140625,0.932510495185852,-1.021092414855957,1.1245167255401611,-0.33774372935295105,0.04171159118413925,-1.825873851776123,0.9844433665275574,0.23128068447113037,0.44925206899642944,0.7588831782341003,-0.6143396496772766,0.7231398820877075,-0.9520547986030579,0.08976563066244125,-0.250108003616333,-1.0340434312820435,-0.5898817777633667,0.38409286737442017,-1.983451247215271,-2.0183463096618652,-0.13478927314281464,-0.9448941349983215,0.8351046442985535,0.11090803146362305,-0.9000033736228943,-1.0995591878890991,-0.4662216007709503,1.5611302852630615,1.1420263051986694,1.3525545597076416,-0.21901188790798187,-0.6617565155029297,-1.3524688482284546,-0.08346211165189743,0.19869646430015564,0.6900956034660339,0.09837295114994049,-0.935364305973053,2.0304718017578125,-0.23460471630096436,0.8507344126701355,0.05196633189916611,-0.25166597962379456,0.8636643290519714,-1.1473535299301147,-1.119608998298645,-1.224042296409607,0.345360666513443,0.8790814280509949,0.6880958676338196,0.42263299226760864,-0.8009740710258484,-0.8403847813606262,-0.42372238636016846,0.8846765756607056,0.008216890506446362,-1.3689810037612915,0.5761739611625671,0.7436318397521973,-0.5146975517272949,0.03811496123671532,-0.8828745484352112,0.37926968932151794,-0.2088041454553604,-1.2081218957901,-0.8813842535018921,-0.4491870403289795,1.8100335597991943,-1.5407594442367554,-1.3547190427780151,-0.9927709102630615,0.25797179341316223,1.5138113498687744,2.21522855758667,0.37613189220428467,2.052435874938965,-0.9036638140678406,1.4347994327545166,-1.0583890676498413,1.1834229230880737,0.22875870764255524,-0.15773974359035492,0.8126526474952698,0.8355133533477783,-0.8104392886161804,-0.272115021944046,1.0242664813995361,-0.16979581117630005,-0.35611826181411743,0.3289620578289032,-0.9086414575576782,-1.268301248550415,0.30672571063041687,0.32421013712882996,-0.026934700086712837,-0.44904404878616333,-0.127841517329216,0.31784623861312866,-0.11273536086082458,0.3991553485393524,-1.3629525899887085,0.47256600856781006,1.1377711296081543,-1.314186930656433,-1.4258346557617188,-0.24445608258247375,-1.2745195627212524,0.12743563950061798,0.7754288911819458,0.47228652238845825,0.6915730834007263,0.2363058626651764,-2.5561904907226562,0.13145959377288818,-2.6139519214630127,0.775188148021698,-1.9252350330352783,0.6055596470832825,0.3983364403247833,-1.1145795583724976,0.14609509706497192,0.09791436046361923,0.16862735152244568,-0.45158320665359497
];
var b1 = [
    -1.315558910369873,-0.5609900951385498,-0.4700244665145874,0.03527142107486725,-0.003995520528405905,0.0952947661280632,-1.0892722606658936,-1.2010776996612549,-0.42137137055397034,-0.3994099199771881,0.44531914591789246,-1.2443383932113647,2.520181179046631,0.5229395627975464,-1.0832970142364502,-0.3073088228702545,-0.8685360550880432,-0.6958643198013306,-0.8239687085151672,0.6627947092056274,1.0347944498062134,-1.5497065782546997,-0.3106662631034851,-0.528752326965332,0.5704811215400696,-0.6376674771308899,-0.4282442629337311,-1.3576321601867676,1.4626526832580566,-0.9936455488204956,1.3881034851074219,-0.46990153193473816,-0.9800161123275757,-1.5228477716445923,-0.16285963356494904,-0.357307493686676,1.6746840476989746,-1.0254223346710205,-0.440673291683197,0.49844714999198914
];
var w2 = [
    -1.193839430809021,1.3521558046340942,-0.36734551191329956,-0.28109806776046753,-0.31395402550697327,0.7218171954154968,1.1764817237854004,-0.4356899857521057,-0.312974214553833,-0.9127114415168762,-0.21995308995246887,0.9624466300010681,-2.060220956802368,-0.13732104003429413,0.41033223271369934,-1.5020718574523926,0.49034351110458374,-0.42728370428085327,-0.07672926783561707,-0.6347255110740662,0.476190447807312,0.2534807324409485,2.022974729537964,0.7077998518943787,-0.8791752457618713,-0.09272118657827377,0.15515603125095367,-0.5693243145942688,1.0081067085266113,0.5672234296798706,0.2882840037345886,-1.184003233909607,0.5216734409332275,1.1518044471740723,-0.37840309739112854,0.48655328154563904,-1.0321364402770996,-0.33566784858703613,-0.49772632122039795,0.4530829191207886,-0.9398810267448425,-1.5221410989761353,2.7407917976379395,-0.2433464080095291,0.8641723990440369,-1.6565784215927124,-0.47212347388267517,-0.4226974546909332,-1.120812177658081,-0.9021924734115601,-2.5369069576263428,-2.179025650024414,2.928731918334961,-1.0049852132797241,0.0059621213003993034,-2.0930583477020264,-1.0763757228851318,1.4129014015197754,-1.4727412462234497,-0.23734425008296967,0.7137167453765869,1.5376167297363281,-1.6479638814926147,0.18236714601516724,0.6965206265449524,0.5253361463546753,-1.0747424364089966,0.48335161805152893,0.010642394423484802,0.13548710942268372,0.9527344703674316,0.33779391646385193,0.16628052294254303,-1.6343495845794678,1.6983797550201416,0.25092849135398865,1.4526818990707397,-0.8754790425300598,-0.8190569877624512,0.4153902232646942,1.3384945392608643,0.6185258626937866,-0.0373249389231205,0.017379803583025932,0.0027557406574487686,-0.2954283356666565,-1.0414807796478271,0.725295901298523,-1.4622374773025513,-0.04272378608584404,-0.7579365968704224,0.28063175082206726,-1.1991922855377197,2.762458086013794,0.6882385611534119,-1.5538933277130127,0.8407226800918579,1.4993113279342651,-0.4821142554283142,-1.2812223434448242,1.0132362842559814,0.05720086023211479,-0.12758882343769073,0.3872978091239929,-1.5206048488616943,0.9366623759269714,-1.5482069253921509,-0.6808310747146606,0.020197417587041855,0.5573250651359558,-0.3751184940338135,0.008463687263429165,3.226428985595703,-0.476767361164093,0.5029918551445007,0.515173077583313,-0.4833347201347351,-0.6178487539291382,0.4915267825126648,0.18318435549736023,1.3634721040725708,1.3636510372161865,-1.122610330581665,-0.49900054931640625,0.6821146607398987,1.3633922338485718,-1.1297062635421753,-0.521876335144043,-0.6637779474258423,0.20826782286167145,-0.5046555995941162,0.1638249158859253,-0.10250440239906311,1.4392764568328857,-0.2904970347881317,-0.29739925265312195,-1.0206570625305176,0.07142697274684906,0.43934401869773865,0.8758823871612549,-0.6387203931808472,0.133206307888031,1.8997036218643188,0.4973422586917877,-0.8228775858879089,0.8041938543319702,0.5890457630157471,-0.368416965007782,0.16973304748535156,0.9969806671142578,0.1706993579864502,-0.8917257785797119,0.5353884696960449,1.1742628812789917,0.6039779782295227,-0.08535990118980408,0.6264979839324951,0.3909045159816742,0.6253018379211426,0.5537876486778259,-0.11938512325286865,-0.14984577894210815,0.8086215257644653,1.6495636701583862,-0.032242849469184875,-0.649555504322052,0.7979274392127991,0.6736510992050171,0.43898892402648926,0.14012746512889862,-0.8531606793403625,-1.5199077129364014,0.7389150261878967,-0.7156473398208618,-0.7037031054496765,-0.4460035264492035,1.6715295314788818,-0.8254377841949463,0.7244270443916321,1.211894154548645,0.5421628952026367,-1.0914044380187988,0.8040482401847839,0.880897581577301,-0.19275972247123718,-1.037895679473877,-0.3008624017238617,0.5334180593490601,1.7598496675491333,-1.3046613931655884,1.679922103881836,-1.2075791358947754,-1.4867980480194092,-0.3927346467971802,0.9348537921905518,1.799997091293335,-1.1961736679077148,1.2875192165374756,-1.128882884979248,-0.6161973476409912,-0.7288563251495361,0.8872636556625366,-0.6847226619720459,0.03399168699979782,0.9204903244972229,-0.12505152821540833,0.39863067865371704,-0.07000795006752014,-1.0270799398422241,1.3212249279022217,-0.6874005794525146,-1.2518327236175537,-0.35411813855171204,-0.5270957946777344,0.6821045875549316,-0.7184951305389404,1.0197216272354126,-1.1798765659332275,-0.7621378898620605,0.5524483919143677,1.1002212762832642,0.6973507404327393,-0.07940085232257843,0.43503692746162415,1.018552303314209,0.20681999623775482,1.2615132331848145,-0.46551641821861267,0.813632607460022,-0.5426837801933289,0.28386327624320984,-0.24511073529720306,-0.6193476915359497,-1.9101766347885132,-1.9169992208480835,0.0890534445643425,0.9596741199493408,-0.1954168975353241,-0.33696016669273376,-0.5201517939567566,-0.19952292740345,0.09753028303384781,-0.03296639770269394,1.2723655700683594,1.3183809518814087,0.5203564763069153,-0.6150986552238464,-1.962992548942566,-0.3389991819858551,0.8874709010124207,-0.4281553626060486,-0.6883965134620667,-0.6893264651298523,0.3894244432449341,-0.44620856642723083,-0.8611055016517639,-0.008748039603233337,0.8521829843521118,0.8398941159248352,-1.4471217393875122,0.06115031987428665,0.5367264151573181,-0.7223606109619141,-0.6795543432235718,-0.26059603691101074,-0.6091687679290771,-0.9806747436523438,-1.144774317741394,0.5777533650398254,2.1473724842071533,0.2561212480068207,1.6277555227279663,-0.852466881275177,-0.18893571197986603,0.6308016777038574,-0.9578377604484558,-0.3298248052597046,0.19065941870212555,-1.3406566381454468,-0.051671482622623444
];
var b2 = [
    0.4722766578197479,0.7164351344108582,-0.7587081789970398,0.08690053224563599,0.5552196502685547,1.1072479486465454,-0.9816940426826477
];
function tanh(x) {
	if (x > 20) return 1.0;
	else if (x < -20) return -1.0;
	else
	{
		var a = Math.exp(x);
		var b = Math.exp(-x);
		return (a - b) / (a + b);
	}
}
for (var i = 0; i < 10; i++)
{
    var j;
    var k;
    var l;
    var x = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];   
    var y = [0, 0, 0, 0, 0, 0, 0];
    var h = [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];
    x[i] = 1.0;
    for (j = 0; j < 10; j++)
    {
	    for (k = 0; k < 40; k++)
    	{
	    	l = j * 40 + k;
    		h[k] += x[j] * w1[l];
    	}    	
    }
    for (j = 0; j < 40; j++)
    {
        h[j] += b1[j];
	    h[j] = tanh(h[j]);
    }
    for (j = 0; j < 40; j++)
    {
	    for (k = 0; k < 7; k++)
    	{
	    	l = j * 7 + k;
    		y[k] += h[j] * w2[l];
    	}
    }
    for (k = 0; k < 7; k++)
    {
	    y[k] += b2[k];
    }
    var a = 0;
    var b = 0;
    var c = 0;
    var d = 0;
    var e = 0;
    var f = 0;
    var g = 0;
    if (y[0] > 0.1) g = 1;
    if (y[1] > 0.1) f = 1;
    if (y[2] > 0.1) e = 1;
    if (y[3] > 0.1) d = 1;
    if (y[4] > 0.1) c = 1;
    if (y[5] > 0.1) b = 1;
    if (y[6] > 0.1) a = 1;
    draw7SegLED({
        context: context,
        fontColor: 'rgba(255, 255, 0, 1)',
        backColor: 'rgba(34, 34, 34, 1)',
        x: i * 50,
        y: 0,
        width: 50,
        height: 100,
        seg: {
            a: a,
            b: b,
            c: c,
            d: d,
            e: e,
            f: f,
            g: g,
            dp: 0
        }
    });
}


```

#成果物

http://jsdo.it/ohisama1/gGfP

以上。