/*
    http://cs.stanford.edu/people/karpathy/convnetjs/docs.html
*/
"use strict";

const convnetjs = require("convnetjs");

class DomainNetJS {
    constructor() {
        this.net = new convnetjs.Net();
        this.net.makeLayers([
            {type:'input', out_sx:1, out_sy:1, out_depth:6},
            {type:'fc', num_neurons:32, activation:'relu'},
            {type:'fc', num_neurons:32, activation:'relu'},
            {type:'fc', num_neurons:32, activation:'relu'},
            {type:'regression', num_neurons:1}
        ]);
        this.trainer = new convnetjs.Trainer(this.net, {
            method:'adadelta',
            l2_decay:0.05
        });
    }
    code(c) {
        let d = 0;
        if(0x2D<=c && c<=0x39) d = (c-0x2D)+1;   // '-' ~ '9'
        if(0x41<=c && c<=0x5A) d = (c-0x41)+14;  // 'A' ~ 'Z'
        if(0x61<=c && c<=0x7A) d = (c-0x61)+14;  // 'a' ~ 'Z'
        return d;
    }
    domain(name) {
        let label = name.split('.');
        let v = [label[0].length, 3, 1, 0, 0, 0];
        for(let j=0; j<label.length; j++)
            for(let i=0; i<label[j].length; i++) {
                let d = this.code(label[j].charCodeAt(i));
                if(!j) {
                    if(14<=d && d<=39) v[1] = 0;
                    else if(4<=d && d<=13) {
                        if(v[1]>=3 && d===4) v[1] = 2;
                        if(v[1]>=2 && d===8) v[1] = 1;
                        v[2] = 0;
                    }
                    else v[1] = v[2] = 0, v[3] = 1;
                    v[5] *= 40, v[5] += d;
                }
                else {
                    v[4] *= 40, v[4] += d;
                }
            }
        return new convnetjs.Vol(v);
    }
    train(name, price) {
        return this.trainer.train(this.domain(name), [price]);
    }
    evaluate(name) {
        return this.net.forward(this.domain(name)).w[0];
    }
    toJSON() {
        return this.net.toJSON();
    }
    fromJSON(json) {
        this.net.fromJSON(json);
    }
}

let dn = new DomainNetJS();
let sample = [
    {name: 'nicb.wang',  price: 10},
    {name: 'NIC.WANG',  price: 130},
    {name: 'NIcD.WANG',  price: 1},
    {name: 'nice.wang',  price: 5},
    {name: 'nicf.wang',  price: 4},
    {name: 'nicg.wang',  price: 6},
    {name: 'nich.wang',  price: 6},
    {name: 'nicb.com',   price: 80},
    {name: 'NIC.COM',   price: 1000},
    {name: 'NIcD.COM',   price: 20},
    {name: 'nice.com',   price: 100},
    {name: 'nicf.com',   price: 80},
    {name: 'nicg.com',   price: 90},
    {name: 'nich.com',   price: 90},
    {name: '1201.WANG', price: 600},
    {name: '1201.COM',  price: 4000}
];
for(let i=0; i<50000; i++) {
    let stats = dn.train(sample[i%sample.length].name,
                         sample[i%sample.length].price);
    if(i%1000===0) console.log(stats);
}
for(let i=0; i<sample.length; i++)
    console.log(sample[i].name, sample[i].price,
                dn.evaluate(sample[i].name));

console.log('nicz.wang', dn.evaluate('nicz.wang'));
console.log('nicz.com', dn.evaluate('nicz.com'));
console.log('niz.wang', dn.evaluate('niz.wang'));
console.log('niz.com', dn.evaluate('niz.com'));

一͛世͛珍͛藏͛
86 声望6 粉丝

« 上一篇
amiaodaifu.com
下一篇 »
NOIP2016