convnetjsとtensorflow その5

#概要
tensorflowで学習したデータをconvnetjsで使ってみた。

#写真
![](http://jsdo-static-contents.s3.amazonaws.com/images/capture/8/E/Z/8EZS.jpg?t=1496633542)

#成果物
http://jsdo.it/ohisama1/8EZS

#jsonデータ

```
{"layers":[{"out_depth":8,"out_sx":1,"out_sy":1,"layer_type":"input"},{"out_depth":8,"out_sx":1,"out_sy":1,"layer_type":"fc","num_inputs":8,"l1_decay_mul":0,"l2_decay_mul":1,"filters":[{"sx":1,"sy":1,"depth":8,"w":{"0":2.442659854888916,"1":0.03036925569176674,"2":0.02670571394264698,"3":0.03237731382250786,"4":0.03344754874706268,"5":0.023709991946816444,"6":0.022925833240151405,"7":0.04079948738217354}},{"sx":1,"sy":1,"depth":8,"w":{"0":-0.547324001789093,"1":0.12742550671100616,"2":0.11382655799388885,"3":0.13964508473873138,"4":0.12777401506900787,"5":0.14834047853946686,"6":0.14010080695152283,"7":0.3141750395298004}},{"sx":1,"sy":1,"depth":8,"w":{"0":-0.00005239853635430336,"1":0.00019328249618411064,"2":-0.007552160881459713,"3":-0.00951959379017353,"4":0.000612911768257618,"5":-0.0010924148373305798,"6":-0.007068033330142498,"7":-0.006832115817815065}},{"sx":1,"sy":1,"depth":8,"w":{"0":0.2559449374675751,"1":0.0033684324007481337,"2":0.002972407964989543,"3":0.0035757659934461117,"4":0.0037168306298553944,"5":0.0025941806379705667,"6":0.0025307636242359877,"7":0.004344699438661337}},{"sx":1,"sy":1,"depth":8,"w":{"0":1.8640464544296265,"1":0.023495623841881752,"2":0.020266765728592873,"3":0.02486380748450756,"4":0.02596082165837288,"5":0.017020730301737785,"6":0.016434457153081894,"7":0.031406860798597336}},{"sx":1,"sy":1,"depth":8,"w":{"0":0.6324511170387268,"1":0.009626517072319984,"2":0.008553959429264069,"3":0.009925338439643383,"4":0.010424340143799782,"5":0.008031414821743965,"6":0.007866865023970604,"7":0.014363305643200874}},{"sx":1,"sy":1,"depth":8,"w":{"0":1.6223074197769165,"1":0.019033635035157204,"2":0.01610303670167923,"3":0.020258886739611626,"4":0.020998209714889526,"5":0.014346327632665634,"6":0.014054631814360619,"7":0.02381935529410839}},{"sx":1,"sy":1,"depth":8,"w":{"0":-1.2208105325698853,"1":0.28274935483932495,"2":0.2482161819934845,"3":0.2927584946155548,"4":0.30302655696868896,"5":0.33476969599723816,"6":0.3265475332736969,"7":0.7027792930603027}}],"biases":{"sx":1,"sy":1,"depth":8,"w":{"0":-0.09138263761997223,"1":0.4328855276107788,"2":-0.010236646980047226,"3":-0.009968763217329979,"4":-0.06888556480407715,"5":-0.03107363171875477,"6":-0.05482921376824379,"7":0.9436742663383484}}},{"out_depth":8,"out_sx":1,"out_sy":1,"layer_type":"relu"},{"out_depth":2,"out_sx":1,"out_sy":1,"layer_type":"fc","num_inputs":8,"l1_decay_mul":0,"l2_decay_mul":1,"filters":[{"sx":1,"sy":1,"depth":8,"w":{"0":-1.5700905323028564,"1":0.5314898490905762,"2":-0.00263063982129097,"3":-0.14202024042606354,"4":-1.1866055727005005,"5":-0.4542549252510071,"6":-1.08609938621521,"7":1.3101390600204468}},{"sx":1,"sy":1,"depth":8,"w":{"0":1.5915061235427856,"1":-0.6129405498504639,"2":-0.04454798623919487,"3":0.1886073797941208,"4":1.2258621454238892,"5":0.366698682308197,"6":1.0125174522399902,"7":-1.229146957397461}}],"biases":{"sx":1,"sy":1,"depth":2,"w":{"0":1.1223243474960327,"1":-1.122314214706421}}},{"out_depth":2,"out_sx":1,"out_sy":1,"layer_type":"softmax","num_inputs":2}]}
```

#サンプルコード

```
var net;
function toarray(n) {
    var ary = new Array;
    for (var i = -1 ; i < 7; i++)
    {
        var s = 1;
        if (i > -1) s = 2 << i;
        var b = n & (s)
        if (b > 0)
        {
            ary.push(1.0);
        }
        else
        {
            ary.push(0.0);
        }
    }
    return ary;
}
function odd(j, w) {
    var str = "";
    if (w[0] > 0.9)
    {
        str = "even";
    }
    else if (w[1] > 0.9)
    {
        str = "odd";
    }
    return str;
}
function test() {
    var str = "";
    var trainer = new convnetjs.Trainer(net);
    for (var j = 1; j < 101; j++) 
    {
        var point = new convnetjs.Vol(1, 1, 10);
        point.w = toarray(j);
        var prediction = net.forward(point);
        str += j + " : "+ odd(j, prediction.w) + "    ";
        if (j % 4 == 0) str += "<br>";
    }
    return str;
}
var load_from_json = function() {
    $.getJSON("/assets/M/E/h/s/MEhsN", function(json) {
        net = new convnetjs.Net();
        net.fromJSON(json);
        var a = test();
        document.getElementById('helloWorld').innerHTML = a;
    });
}
load_from_json();



```

#使用したライブラリ

https://github.com/ohisama/tf2convnet