input 0 | input 2 | input 3 | Output |
0 | 0 | 1 | 0 |
1 | 1 | 1 | 1 |
1 | 0 | 1 | 1 |
0 | 1 | 1 | 0 |
// Sigmod functionfunction 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 weightsvar syn0 = numeric.sub(numeric.mul(2, numeric.random([3, 1])), 1);//Training loopvar 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 valuesvar X = [[0, 0, 1],[0, 1, 1],[1, 0, 1],[1, 1, 1]];var y = [[0],[0],[1],[1]];train_neural(X, y, 1000);
input 0 | input 1 | input 2 | Output |
0 | 0 | 1 | 0 |
0 | 1 | 1 | 1 |
1 | 0 | 1 | 1 |
1 | 1 | 1 | 0 |
// Sigmod functionfunction 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 weightsvar 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 loopvar 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 valuesvar X = [[0, 0, 1],[0, 1, 1],[1, 0, 1],[1, 1, 1]];
var y = [[0],[1],[1],[0]];
train_neural(X, y, 1000);
1.jpg (68.82 KB, 下载次数: 30)
2.png (27.93 KB, 下载次数: 42)
3.jpg (47.36 KB, 下载次数: 57)
4.png (16.5 KB, 下载次数: 28)
str.arr 发表于 2017-7-8 19:59
js看起来好像很好用,这个是就业班的课程吗?
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |