ConvNetJS

/*
    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'));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值