Javascript實現BP神經網路

來源:互聯網
上載者:User

標籤:BP神經網路   機器學習   Javascript   

BP神經網路是一種按照誤差逆向傳播演算法訓練的多層前饋神經網路,是目前應用最廣泛的神經網路。
BP神經網路誤差反向傳播神經網路:

  1. 置各權和閾值的初始化
  2. 給定P個訓練樣本Xp(p=1,2,...,p) 和對應的理想輸出Dp(p=1,2,...p)
  3. 資訊前向傳遞:
    計算網路各層的輸出

4.誤差反向傳播

5.修改權和閾值

6.重複2~5步,直至P個樣本都訓練一邊
7.判斷是否滿足精度要求。若滿足,則停止訓練,否則重複第2步。

根據上述流程,編寫代碼:

class BPNet{        constructor(layernum, n, fn, fd, miu, iter ,eps){            if(!(n instanceof Array)) {                throw ‘參數錯誤‘            }            if(!n.length == layernum) {                throw ‘參數錯誤‘            }            this.layernum = layernum            this.n = n            //輸出函數            if(!fn) {                this.fn = function (x) {                    return 1.0/(1.0 + Math.exp(-x))                }            }else {                this.fn = fn            }            //誤差函數            if(!fd) {                this.fd = function(x) {                    return x * (1 - x)                }            }else {                this.fd = fd            }            this.w = new Array()//權值矩陣            this.b = new Array() //閾值矩陣            this.miu = miu    || 0.5 //學習速率            this.iter = iter || 500 //迭代次數            this.e = 0.0 //誤差            this.eps = eps || 0.0001            for(let l = 1; l < this.layernum;l++) {                let item = new Array()                let bitem = new Array()                for(let j = 0;j < n[l]; j++) {                    let temp = new Array()                    for(let i = 0;i < n[l - 1];i++) {                        temp[i] = Math.random()                    }                    item.push(temp)                    bitem.push(Math.random())                }                this.w[l] = item                this.b[l] = bitem            }        }        //預測函數        forward(x) {            let y = new Array()            y[0] = x            for(let l = 1; l < this.layernum;l++) {                y[l] = new Array()                for(let j = 0;j < this.n[l]; j++) {                    let u = 0.0                    for(let i = 0;i < this.n[l - 1]; i++) {                        u = u + this.w[l][j][i] * y[l - 1][i]                    }                    u = u + this.b[l][j]                    y[l][j] = this.fn(u)                }            }            return y        }        //計算誤差        calcdelta(d, y) {            let delta = new Array()            let last = new Array()            for(let j = 0;j < this.n[this.layernum - 1];j++){                last[j] = (d[j] - y[this.layernum - 1][j]) * this.fd(y[this.layernum - 1][j])            }            delta[this.layernum - 1] = last            for(let l = this.layernum - 2;l > 0; l--) {                delta[l] = new Array()                for(let j = 0;j < this.n[l]; j++) {                    delta[l][j] = 0.0                    for(let i = 0; i < this.n[l + 1];i++) {                        delta[l][j] += delta[l + 1][i] * this.w[l+1][i][j]                    }                    delta[l][j] = this.fd(y[l][j])*delta[l][j]                }            }            return delta        }        //調整權值和閾值        update(y, delta) {            for(let l = 0; l < this.layernum;l++) {                for(let j = 0;j < this.n[l];j++) {                    for(let i = 0;i < this.n[l - 1];i++) {                        this.w[l][j][i] += this.miu * delta[l][j] * y[l-1][i]                        this.b[l][j] += this.miu * delta[l][j]                    }                }            }        }        //樣本訓練        train(x, d) {            for(let p = 0;p < this.iter;p++) {                this.e = 0                for(let i = 0;i < x.length;i++) {                    let y = this.forward(x[i])                    let delta = this.calcdelta(d[i], y)                    this.update(y, delta)                    let ep = 0.0                    let l1 = this.layernum - 1                    for(let l = 0;l < this.n[l1];l++) {                        ep += (d[i][l] - y[l1][l]) * (d[i][l] - y[l1][l])                    }                    this.e += ep/2.0                }                if(this.e < this.eps) {                    break;                }            }        }    }

使用方式:
用BP神經網路實現異或邏輯:

let x = [[0,0],[0,1],[1,0],[1,1]]//輸入樣本    let d = [[0],[1],[1],[0]]//理想輸出    let bp = new BPNet(3, [2,6,1], undefined, undefined, 0.5, 5000 ,0.0001)    bp.train(x,d)   let y = bp.forward([0, 1])    console.log(y[2][0])    let y2 = bp.forward([0,0])    console.log(y2[2][0])    let y3 = bp.forward([1,1])    console.log(y3[2][0])    let y4 = bp.forward([1, 0])    console.log(y4[2][0])

結果:

Javascript實現BP神經網路

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.