From ba147461d96142f810567ef6258062782beee8ce Mon Sep 17 00:00:00 2001 From: erelsgl Date: Tue, 28 May 2013 14:53:09 +0300 Subject: [PATCH 1/9] Add associative perceptron and custom feature extractor --- .gitignore | 2 + demos/and.js | 32 ++++ package.json | 2 +- perceptron_associative.js | 172 ++++++++++++++++++ tests/test-perceptron_associative.js | 51 ++++++ ...erceptron_associative_feature_extractor.js | 35 ++++ 6 files changed, 293 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 demos/and.js create mode 100644 perceptron_associative.js create mode 100644 tests/test-perceptron_associative.js create mode 100644 tests/test-perceptron_associative_feature_extractor.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a116156 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules +/.project diff --git a/demos/and.js b/demos/and.js new file mode 100644 index 0000000..1ab4a3e --- /dev/null +++ b/demos/and.js @@ -0,0 +1,32 @@ +var perceptron = require('../perceptron') + +var and = perceptron() + +and.train([1, 1], 1) +and.train([0, 1], 0) +and.train([1, 0], 0) +and.train([0, 0], 0) + +// practice makes perfect (we hope...) +while(!and.retrain()) {} + +console.log("2 FEATURES: "); +console.log(and.perceive([1, 1])) // 1 +console.log(and.perceive([0, 1])) // 0 +console.log(and.perceive([1, 0])) // 0 +console.log(and.perceive([0, 0])) // 0 +console.log(and.perceive([2, 0])) // ? +console.log(and.perceive([0, 2])) // ? + +console.log("3 FEATURES: "); +console.log(and.perceive([0, 0, 0])) // 0 +console.log(and.perceive([0, 0, 1])) // 0 +console.log(and.perceive([0, 1, 0])) // 0 +console.log(and.perceive([1, 0, 0])) // 0 +console.log(and.perceive([1, 1, 0])) // 0 +console.log(and.perceive([1, 0, 1])) // 0 +console.log(and.perceive([0, 1, 1])) // 0 +console.log(and.perceive([1, 1, 1])) // 0 + +console.dir(and); + diff --git a/package.json b/package.json index 5d7fce6..f715b97 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ }, "dependencies": {}, "devDependencies": { - "specify": "~1.1.2" + "specify": "*" }, "keywords": [ "machine learning", diff --git a/perceptron_associative.js b/perceptron_associative.js new file mode 100644 index 0000000..728de70 --- /dev/null +++ b/perceptron_associative.js @@ -0,0 +1,172 @@ +/** + * A version of Perceptron where the weights vector is an associative array (not a numeric array), + * so the features can be any objects (not just nubmers). + * @author Erel Segal-haLevi + * @since 2013-05-27 + * + * @param opts optional parameters: