diff --git a/examples/iris_classification/.gitignore b/examples/iris_classification/.gitignore new file mode 100644 index 0000000..748daf9 --- /dev/null +++ b/examples/iris_classification/.gitignore @@ -0,0 +1,66 @@ + +# Created by https://www.gitignore.io/api/node + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# Typescript v1 declaration files +typings/ + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env + + + +# End of https://www.gitignore.io/api/node diff --git a/examples/iris_classification/ReadMe.md b/examples/iris_classification/ReadMe.md new file mode 100644 index 0000000..bc3366d --- /dev/null +++ b/examples/iris_classification/ReadMe.md @@ -0,0 +1,10 @@ +#Instructions + +Make sure you have installed [Nodejs](https://nodejs.org/en/) + +Then cd into this directory from cmd prompt. And then run the below commands, + +`npm install` +`npm start` + +Once the training process has completed your neural net model will be saved as model.json \ No newline at end of file diff --git a/examples/iris_classification/index.html b/examples/iris_classification/index.html new file mode 100644 index 0000000..ca3c033 --- /dev/null +++ b/examples/iris_classification/index.html @@ -0,0 +1,106 @@ + + + + + + + + CSV NeuralNetwork Parser + + + + + + + + + + +
+
+

CSV FiletoHTMLTable using AJAX Jquery

+
+
+ +
+
+ +
+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
IDFirst NameLast NameFisrt NameLast Name
1JohnnyEnglishBudSpencer
1JohnnyEnglishBudSpencer
1JohnnyEnglishBudSpencer
1JohnnyEnglishBudSpencer
1JohnnyEnglishBudSpencer
1JohnnyEnglishBudSpencer
1JohnnyEnglishBudSpencer
+
+
+ + + \ No newline at end of file diff --git a/examples/iris_classification/index.js b/examples/iris_classification/index.js new file mode 100644 index 0000000..b8d73c4 --- /dev/null +++ b/examples/iris_classification/index.js @@ -0,0 +1,110 @@ +const NeuralNet = require("../../lib/nn"); +const csv = require("csvtojson"); +const fs = require("fs"); +require("colors"); + +const LoggerType = Object.freeze({ + Error: 1, + Warn: 2, + Info: 3, + Data: 4 +}); +function debug(text, type) { + if (type) { + if (type === LoggerType.Error) { + process.stdout.write("ERROR: ".red); + console.error(JSON.stringify(text) || text); + } else if (type === LoggerType.Warn) { + process.stdout.write("WARN: ".yellow); + console.warn(JSON.stringify(text) || text); + } else if (type === LoggerType.Info) { + process.stdout.write("INFO: ".cyan); + console.log(JSON.stringify(text) || text); + } else if (type === LoggerType.Data) { + process.stdout.write("DATA: ".green); + console.log(JSON.stringify(text) || text); + } + return; + } + console.log(text); +} + +const trainIP = []; +const trainOP = []; +const testIP = []; +const testOP = []; + +const noOfInputs = 4; +const noOfOutputs = 3; +const nn = new NeuralNet.NeuralNetwork(noOfInputs, 5, noOfOutputs); + +function outputArrGenerator(op, opSize) { + let opArr = new Array(opSize); + opArr.fill(0); + op = parseInt(op, 10); + opArr[op - 1] = 1; + return opArr; +} + +csv() + .fromFile("./train.csv") + .on("json", jsonObj => { + const temp = Object.values(jsonObj); + trainOP.push(temp.slice(temp.length - 1, temp.length)); + trainIP.push(temp.slice(0, temp.length - 1)); + }) + .on("done", error => { + if (error) { + throw error; + } + const epoch = 20000; + for (let i = 0; i < epoch; i++) { + const randomIndex = Math.floor(Math.random() * trainIP.length); + nn.train(trainIP[randomIndex], outputArrGenerator(trainOP[randomIndex][0], noOfOutputs)); + } + debug("Training Complete...", LoggerType.Info); + }); + +const predVal = []; + +csv() + .fromFile("./test.csv") + .on("json", jsonObj => { + const temp = Object.values(jsonObj); + testOP.push(temp.slice(temp.length - 1, temp.length)); + testIP.push(temp.slice(0, temp.length - 1)); + }) + .on("done", error => { + if (error) { + throw error; + } + debug( + `|************************************|`, + LoggerType.Info + ); + debug( + `| Predicted Class | Actual Class |`, + LoggerType.Info + ); + debug( + `|************************************|`, + LoggerType.Info + ); + for (let i = 0; i < testIP.length; i++) { + predVal.push(nn.predict(testIP[i])); + debug( + `| ${predVal[i].indexOf(Math.max(...predVal[i])) + 1} | ${testOP[i]} |`, + LoggerType.Info + ); debug( + `|------------------------------------|`, + LoggerType.Info + ); + } + const modal = nn.serialize(); + fs.writeFile("./model.json", modal, err => { + if (err) { + debug(err, LoggerType.Error); + } + }); + debug("end", LoggerType.Info); + }); diff --git a/examples/iris_classification/main.js b/examples/iris_classification/main.js new file mode 100644 index 0000000..60e116f --- /dev/null +++ b/examples/iris_classification/main.js @@ -0,0 +1,66 @@ +$(document).ready(function () { + function checkCSVFile(name) { + return (name.slice(name.length - 4, name.length) === '.csv') + } + function renderCSVtoTable(data) { + var csv_data = data.split(/\r?\n|\r/); + var table_data = ''; + for (let row_count = 0; row_count < csv_data.length; row_count++) { + const cell_data = csv_data[row_count].split(','); + table_data += ''; + for (let cell_count = 0; cell_count < cell_data.length; cell_count++) { + const element = cell_data[cell_count]; + if (cell_count === 0) { + table_data += ''; + } else { + table_data += ''; + } + } + table_data += ''; + } + table_data += '
' + element + '' + element + '
'; + return table_data; + } + function loadHandler(event) { + var csv = event.target.result; + $('#data_table').html(renderCSVtoTable(csv));; + } + function errorHandler(evt) { + if (evt.target.error.name == "NotReadableError") { + alert("Canno't read file !"); + } + } + function getAsText(fileToRead) { + var reader = new FileReader(); + // Read file into memory as UTF-8 + reader.readAsText(fileToRead); + // Handle errors load + reader.onload = loadHandler; + reader.onerror = errorHandler; + } + function handleFiles(files) { + // Check for the various File API support. + if (window.FileReader) { + // FileReader are supported. + getAsText(files[0]); + } else { + alert('FileReader are not supported in this browser.'); + } + } + // detect a change in a file input with an id of “the-file-input” + $("#csv-upload").change(function () { + // will log a FileList object, view gifs below + if(checkCSVFile(this.files[0].name)) { + $('#data_table').html(handleFiles(this.files)); + } + }); + $('#load_data').click(function () { + $.ajax({ + url: "train.csv", + dataType: "text", + success: function (data) { + $('#data_table').html(renderCSVtoTable(data)); + } + }) + }) +}) \ No newline at end of file diff --git a/examples/iris_classification/package-lock.json b/examples/iris_classification/package-lock.json new file mode 100644 index 0000000..7dfc78f --- /dev/null +++ b/examples/iris_classification/package-lock.json @@ -0,0 +1,46 @@ +{ + "name": "iris_classification", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "colors": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.1.2.tgz", + "integrity": "sha1-FopHAXVran9RoSzgyXv6KMCE7WM=" + }, + "csvtojson": { + "version": "1.1.9", + "resolved": "https://registry.npmjs.org/csvtojson/-/csvtojson-1.1.9.tgz", + "integrity": "sha1-5kGucve8L6P5qvEn4CH8iUR8HNE=", + "requires": { + "lodash": "4.17.5", + "strip-bom": "1.0.0" + } + }, + "first-chunk-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/first-chunk-stream/-/first-chunk-stream-1.0.0.tgz", + "integrity": "sha1-Wb+1DNkF9g18OUzT2ayqtOatk04=" + }, + "is-utf8": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/is-utf8/-/is-utf8-0.2.1.tgz", + "integrity": "sha1-Sw2hRCEE0bM2NA6AeX6GXPOffXI=" + }, + "lodash": { + "version": "4.17.5", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.5.tgz", + "integrity": "sha512-svL3uiZf1RwhH+cWrfZn3A4+U58wbP0tGVTLQPbjplZxZ8ROD9VLuNgsRniTlLe7OlSqR79RUehXgpBW/s0IQw==" + }, + "strip-bom": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/strip-bom/-/strip-bom-1.0.0.tgz", + "integrity": "sha1-hbiGLzhEtabV7IRnqTWYFzo295Q=", + "requires": { + "first-chunk-stream": "1.0.0", + "is-utf8": "0.2.1" + } + } + } +} diff --git a/examples/iris_classification/package.json b/examples/iris_classification/package.json new file mode 100644 index 0000000..ab12c53 --- /dev/null +++ b/examples/iris_classification/package.json @@ -0,0 +1,16 @@ +{ + "name": "iris_classification", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "node index.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "colors": "^1.1.2", + "csvtojson": "^1.1.9" + } +} diff --git a/examples/iris_classification/test.csv b/examples/iris_classification/test.csv new file mode 100644 index 0000000..ae76663 --- /dev/null +++ b/examples/iris_classification/test.csv @@ -0,0 +1,31 @@ +sepal_l,sepal_w,petal_l,petal_w,class +5.0,3.5,1.3,0.3,1 +4.5,2.3,1.3,0.3,1 +4.4,3.2,1.3,0.2,1 +5.0,3.5,1.6,0.6,1 +5.1,3.8,1.9,0.4,1 +4.8,3.0,1.4,0.3,1 +5.1,3.8,1.6,0.2,1 +4.6,3.2,1.4,0.2,1 +5.3,3.7,1.5,0.2,1 +5.0,3.3,1.4,0.2,1 +5.5,2.6,4.4,1.2,2 +6.1,3.0,4.6,1.4,2 +5.8,2.6,4.0,1.2,2 +5.0,2.3,3.3,1.0,2 +5.6,2.7,4.2,1.3,2 +5.7,3.0,4.2,1.2,2 +5.7,2.9,4.2,1.3,2 +6.2,2.9,4.3,1.3,2 +5.1,2.5,3.0,1.1,2 +5.7,2.8,4.1,1.3,2 +6.7,3.1,5.6,2.4,3 +6.9,3.1,5.1,2.3,3 +5.8,2.7,5.1,1.9,3 +6.8,3.2,5.9,2.3,3 +6.7,3.3,5.7,2.5,3 +6.7,3.0,5.2,2.3,3 +6.3,2.5,5.0,1.9,3 +6.5,3.0,5.2,2.0,3 +6.2,3.4,5.4,2.3,3 +5.9,3.0,5.1,1.8,3 \ No newline at end of file diff --git a/examples/iris_classification/train.csv b/examples/iris_classification/train.csv new file mode 100644 index 0000000..341b648 --- /dev/null +++ b/examples/iris_classification/train.csv @@ -0,0 +1,121 @@ +sepal_l,sepal_w,petal_l,petal_w,class +5.1,3.5,1.4,0.2,1 +4.9,3.0,1.4,0.2,1 +4.7,3.2,1.3,0.2,1 +4.6,3.1,1.5,0.2,1 +5.0,3.6,1.4,0.3,1 +5.4,3.9,1.7,0.4,1 +4.6,3.4,1.4,0.3,1 +5.0,3.4,1.5,0.2,1 +4.4,2.9,1.4,0.2,1 +4.9,3.1,1.5,0.1,1 +5.4,3.7,1.5,0.2,1 +4.8,3.4,1.6,0.2,1 +4.8,3.0,1.4,0.1,1 +4.3,3.0,1.1,0.1,1 +5.8,4.0,1.2,0.2,1 +5.7,4.4,1.5,0.4,1 +5.4,3.9,1.3,0.4,1 +5.1,3.5,1.4,0.3,1 +5.7,3.8,1.7,0.3,1 +5.1,3.8,1.5,0.3,1 +5.4,3.4,1.7,0.2,1 +5.1,3.7,1.5,0.4,1 +4.6,3.6,1.0,0.2,1 +5.1,3.3,1.7,0.5,1 +4.8,3.4,1.9,0.2,1 +5.0,3.0,1.6,0.2,1 +5.0,3.4,1.6,0.4,1 +5.2,3.5,1.5,0.2,1 +5.2,3.4,1.4,0.2,1 +4.7,3.2,1.6,0.2,1 +4.8,3.1,1.6,0.2,1 +5.4,3.4,1.5,0.4,1 +5.2,4.1,1.5,0.1,1 +5.5,4.2,1.4,0.2,1 +4.9,3.1,1.5,0.2,1 +5.0,3.2,1.2,0.2,1 +5.5,3.5,1.3,0.2,1 +4.9,3.6,1.4,0.1,1 +4.4,3.0,1.3,0.2,1 +5.1,3.4,1.5,0.2,1 +7.0,3.2,4.7,1.4,2 +6.4,3.2,4.5,1.5,2 +6.9,3.1,4.9,1.5,2 +5.5,2.3,4.0,1.3,2 +6.5,2.8,4.6,1.5,2 +5.7,2.8,4.5,1.3,2 +6.3,3.3,4.7,1.6,2 +4.9,2.4,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8,4.9,2.0,3 +7.7,2.8,6.7,2.0,3 +6.3,2.7,4.9,1.8,3 +6.7,3.3,5.7,2.1,3 +7.2,3.2,6.0,1.8,3 +6.2,2.8,4.8,1.8,3 +6.1,3.0,4.9,1.8,3 +6.4,2.8,5.6,2.1,3 +7.2,3.0,5.8,1.6,3 +7.4,2.8,6.1,1.9,3 +7.9,3.8,6.4,2.0,3 +6.4,2.8,5.6,2.2,3 +6.3,2.8,5.1,1.5,3 +6.1,2.6,5.6,1.4,3 +7.7,3.0,6.1,2.3,3 +6.3,3.4,5.6,2.4,3 +6.4,3.1,5.5,1.8,3 +6.0,3.0,4.8,1.8,3 +6.9,3.1,5.4,2.1,3 \ No newline at end of file diff --git a/lib/nn.js b/lib/nn.js index 5cce746..1fd5394 100644 --- a/lib/nn.js +++ b/lib/nn.js @@ -1,4 +1,5 @@ // Other techniques for learning +const Matrix = require('./Matrix'); class ActivationFunction{ constructor(func, dfunc){ @@ -142,3 +143,9 @@ class NeuralNetwork { } } + +if (typeof module !== 'undefined') { + module.exports = { + NeuralNetwork, + }; +} \ No newline at end of file