# 用JS实现简单的神经网络算法

2016/06/03 13:13

### 两层神经网络

Inputs 0 Inputs 1 Inputs 2 Output
0 0 1 0
1 1 1 1
1 0 1 1
0 1 1 0

// Sigmod function
function nonlin(x, deriv) {
if (deriv) {
return numeric.mul(x, numeric.sub(1, x));
}

return numeric.div(1, numeric.add(1, numeric.exp(numeric.neg(x))));
}

function train_neural(X, y, iteration) {
// initialize weights
var syn0 = numeric.sub(numeric.mul(2, numeric.random([3, 1])), 1);
//Training loop
var i = 0;
for (; i < iteration; i++) {
var l0 = X;
var l1 = nonlin(numeric.dot(l0, syn0));
var l1_error = numeric.sub(y, l1);
var l1_delta = numeric.mul(l1_error, nonlin(l1, true));
syn0 = numeric.add(syn0, numeric.dot(numeric.transpose(l0), l1_delta));
}
}
}

//Initial input/ouput values
var X = [
[0, 0, 1],
[0, 1, 1],
[1, 0, 1],
[1, 1, 1]
];

var y = [
[0],
[0],
[1],
[1]
];

train_neural(X, y, 1000);

• X 输入数据
• y 输出数据
• nonlin， S函数
• l0，网络第一层，这是等于输入数据
• l1，网络第二层，这里就是输出层
• syn0，第一层网络的权重

1000次迭代后的网络输出： ［0.03，0.02， 0.979， 0.974］

1000次迭代后的syn0权重值： ［7.266，－0.221，－3.415］

### 三层神经网络

Inputs 0 Inputs 1 Inputs 2 Output
0 0 1 0
0 1 1 1
1 0 1 1
1 1 1 0

// Sigmod function
function nonlin(x, deriv) {
if (deriv) {
return numeric.mul(x, numeric.sub(1, x));
}

return numeric.div(1, numeric.add(1, numeric.exp(numeric.neg(x))));
}

function train_neural(X, y, iteration) {
// initialize weights
var syn0 = [
[-0.1653904, 0.11737966, -0.71922612, -0.60379702],
[0.60148914, 0.93652315, -0.37315164, 0.38464523],
[0.7527783, 0.78921333, -0.82991158, -0.92189043]
];

var syn1 = [
[-0.66033916],
[0.75628501],
[-0.80330633],
[-0.15778475]
];

//Training loop
var i = 0;
for (; i < 1000; i++) {
var l0 = X;
var l1 = nonlin(numeric.dot(l0, syn0));
var l2 = nonlin(numeric.dot(l1, syn1));
var l2_error = numeric.sub(y, l2);
var l2_delta = numeric.mul(l2_error, nonlin(l2, true));
var l1_error = numeric.dot(l2_delta, numeric.transpose(syn1));
var l1_delta = numeric.mul(l1_error, nonlin(l1, true));
syn1 = numeric.add(syn1, numeric.dot(numeric.transpose(l1), l2_delta));
syn0 = numeric.add(syn0, numeric.dot(numeric.transpose(l0), l1_delta));
}
}

//Initial input/output values
var X = [
[0, 0, 1],
[0, 1, 1],
[1, 0, 1],
[1, 1, 1]
];

var y = [
[0],
[1],
[1],
[0]
];

train_neural(X, y, 1000);

syn0 ：

7
2 收藏

13 评论
2 收藏
7