Skip to content

Commit

Permalink
Merge pull request #61 from mateogianolio/logistic-regression
Browse files Browse the repository at this point in the history
add logistic regression example
  • Loading branch information
mateogianolio committed Mar 1, 2016
2 parents 15aca68 + 4f46e7a commit 9fb6988
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ At the moment, Google Chrome seems to offer the best TypedArray support.

**Machine learning**
* [**Backpropagation**](https://github.com/mateogianolio/vectorious/tree/master/examples/neural-network.js) (by [@lucidrains](https://github.com/lucidrains))
* [**Gradient descent**](https://github.com/mateogianolio/vectorious/tree/master/examples/neural-network.js)
* [**Gradient descent**](https://github.com/mateogianolio/vectorious/tree/master/examples/gradient-descent.js)
* [**Logistic regression**](https://github.com/mateogianolio/vectorious/tree/master/examples/logistic-regression.js)

### Documentation

Expand Down
4 changes: 1 addition & 3 deletions examples/backpropagation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

var Matrix = require('../vectorious').Matrix;

// aliases
var add = Matrix.add,
subtract = Matrix.subtract,
random = Matrix.random;
Expand All @@ -26,15 +25,14 @@
};
}

// inputs and outputs
// input and output
var X = new Matrix([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]]),
y = new Matrix([[0, 1, 1, 0]]).T;

// initialize weights with a standard deviation of 2 and mean -1
var syn0 = random.apply(null, X.T.shape, 2, -1),
syn1 = random.apply(null, y.shape, 2, -1);

// layers and deltas
var l0,
l1,
l0_delta,
Expand Down
4 changes: 1 addition & 3 deletions examples/gradient-descent.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

var Matrix = require('../vectorious').Matrix;

// aliases
var subtract = Matrix.subtract,
random = Matrix.random;

Expand All @@ -25,7 +24,7 @@
};
}

// inputs and outputs
// input and output
var X = new Matrix([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]]),
y = new Matrix([[0, 1, 1, 0]]).T;

Expand All @@ -36,7 +35,6 @@
var syn0 = random(X.T.shape[0], hidden_dim, 2, -1),
syn1 = random(hidden_dim, 1, 2, -1);

// layers and deltas
var l0,
l1,
l0_delta,
Expand Down
93 changes: 93 additions & 0 deletions examples/logistic-regression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
(function () {
// logistic regression example based on https://github.com/junku901/dnn
'use strict';

var v = require('../vectorious'),
Matrix = v.Matrix,
Vector = v.Vector;

var subtract = Matrix.subtract;

// perform row-wise softmax on matrix
function softmax(m) {
var c = m.shape[1],
max = new Vector(m).max(),
sum;

return m.map(function (x, i, j) {
if (j === 0) {
sum = 0;
for (var k = 0; k < c; k++)
sum += Math.exp(m.get(i, k) - max);
}

return Math.exp(x - max) / sum;
});
}

// get col-wise mean of matrix as vector
function mean(m) {
var c = m.shape[1],
v = Vector.zeros(c),
sum;

return v.map(function (x, i) {
sum = 0;
for (var j = 0; j < c; j++)
sum += m.get(i, j);

return sum / c;
});
}

// row-wise add vector to matrix
function addMatVec(m, v) {
return m.map(function (x, r, c) {
return x + v.get(c);
});
}

var X = new Matrix([
[1, 1, 1, 0, 0, 0],
[1, 0, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 0],
[0, 0, 1, 0, 1, 0],
[0, 0, 1, 1, 1, 0]
]);

var y = new Matrix([
[1, 0],
[1, 0],
[1, 0],
[0, 1],
[0, 1],
[0, 1]
]);

var W = Matrix.zeros(X.shape[0], y.shape[1]),
b = Vector.zeros(y.shape[1]);

var rate = 0.01,
prob,
delta;

// train
for (var i = 0; i < 800; i++) {
prob = softmax(addMatVec(X.multiply(W), b));
delta = subtract(y, prob);

W.add(X.T.multiply(delta).scale(rate));
b.add(mean(delta).scale(rate));
}

// predict
var x = new Matrix([
[1, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0],
[1, 1, 1, 1, 1, 0]
]);

console.log(softmax(addMatVec(x.multiply(W), b)).toArray());
// prediction should be close to [[1, 0], [0, 1], [0.5, 0.5]]
}());

0 comments on commit 9fb6988

Please sign in to comment.