Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A tiny neural network #9

Open
mateogianolio opened this issue Mar 17, 2016 · 0 comments
Open

A tiny neural network #9

mateogianolio opened this issue Mar 17, 2016 · 0 comments

Comments

@mateogianolio
Copy link
Member

The code in this post is functionally equivalent to A Neural Network in 11 lines of Python. The JavaScript translation was originally written by @lucidrains.

Vectorious is an open-source linear algebra library which I am currently maintaining. We'll use it as a replacement for Python's numpy.

This is the source presented at the top of the Python article:

X = np.array([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]])
y = np.array([[0, 1, 1, 0]]).T
syn0 = 2 * np.random.random((3, 4)) - 1
syn1 = 2 * np.random.random((4, 1)) - 1
for j in xrange(60000):
    l1 = 1 / (1 + np.exp(-(np.dot(X, syn0))))
    l2 = 1 / (1 + np.exp(-(np.dot(l1, syn1))))
    l2_delta = (y - l2) * (l2 * (1 - l2))
    l1_delta = l2_delta.dot(syn1.T) * (l1 * (1 - l1))
    syn1 += l1.T.dot(l2_delta)
    syn0 += X.T.dot(l1_delta)

Below is the JavaScript translation. To make it a bit more readable I extracted the sigmoid function and its derivative, which can be mapped to the elements of a matrix using matrix.map(fn).

function sigmoid(ddx) {
  return function (x) {
    return ddx ?
      x * (1 - x) :
      1.0 / (1 + Math.exp(-x));
  };
}

var X = new Matrix([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]]),
    y = new Matrix([[0, 1, 1, 0]]).T,
    syn0 = Matrix.random(3, 4, 2, -1),
    syn1 = Matrix.random(4, 1, 2, -1),
    l1, l2, l1_delta, l2_delta, i;
for (i = 0; i < 60000; i++) {
  l1 = X.multiply(syn0).map(sigmoid());
  l2 = l1.multiply(syn1).map(sigmoid());
  l2_delta = Matrix.subtract(y, l2).product(l2.map(sigmoid(true)));
  l1_delta = l2_delta.multiply(syn1.T).product(l1.map(sigmoid(true)));
  syn1.add(l1.T.multiply(l2_delta));
  syn0.add(X.T.multiply(l1_delta));
}

Almost the same and above code can be used both in Node.js and the browser! The final prediction is in the l2 variable and should be close to y.

Here is the full example with comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant