This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // console log the output of each iteration | |
| net.train(trainingData, { | |
| log: true | |
| }) | |
| // test data - actually virginica | |
| var output = net.run([6.2,3.4,5.1,2.6]) | |
| console.log(output) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // load each line of the training data into a new index of an array | |
| var trainingFile = fs.readFileSync("data.csv").toString().split("\n") | |
| // iterate over each line of the training data, adding it to a new array for training later | |
| for (var i = 0; i < trainingFile.length; i++) { | |
| var entry = trainingFile[i] | |
| var values = entry.split(",") | |
| var points = [parseFloat(values[0]), parseFloat(values[1]), parseFloat(values[2]), parseFloat(values[3])] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var brain = require('brain.js') | |
| var fs = require('fs') | |
| // configuration to be used in the brain | |
| const config = { | |
| binaryThresh: 0.5, // arbitary value | |
| hiddenLayers: [3], // the size of the hidden layers in the network | |
| activation: 'sigmoid' // activation function | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var fs = require('fs') | |
| var brain = require('brain.js') | |
| var bPath = "Z:\\Development\\Javascript\\NodeJS\\Neural Networks\\Brain.JS\\002\\src\\dataset\\business" | |
| var tPath = "Z:\\Development\\Javascript\\NodeJS\\Neural Networks\\Brain.JS\\002\\src\\dataset\\tech" | |
| var sample = "Z:\\Development\\Javascript\\NodeJS\\Neural Networks\\Brain.JS\\002\\src\\dataset\\011.txt" | |
| var net = new brain.recurrent.LSTM() | |
| var bFiles = fs.readdirSync(bPath) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| net.train(inputs, { | |
| log: true, // prints to the screen the error rate, and current iteration | |
| errorThresh: 0.05, // a relatively high error margin - but I'm not shooting for 100% accuracy (and it's merely a test) | |
| }) | |
| var testFileContent = fs.readFileSync(testFile) // read the entire sample file into memory | |
| var output = net.run(testFileContent) // and then run it! This will predict the class for the testFile | |
| console.log(output) | |
| // output: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var businessFiles = fs.readdirSync(businessPath) // retrieve directory listing | |
| var techFiles = fs.readdirSync(techPath) | |
| for (var i = 0; i < businessFiles.length; i++) { | |
| var file = businessFiles[i] | |
| var content = fs.readFileSync(businessFiles + "\\" + file) // opens the file as a stream, reading the whole file into memory | |
| inputs.push({input: content.toString(), output: 'business' }) // the entire file content is stored in a two dimensional array | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var fs = require('fs') // to get directory listings, and read files - npm install fs | |
| var brain = require('brain.js') // contains core functions - npm install brain.js | |
| var businessPath = "path/to/business/dataset" // location of 100 training files | |
| var techPath = "path/to/tech/dataset" // second class 100 training files | |
| var testFile = "path/to/test/file.txt" // file which will be used to test accuracy - belongs to tech | |
| var inputs = [] // will be used to store all training data prior to training | |
| var errorThreshold = 0.085 // the minimum error margin needed to class the nn as trained |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "io/ioutil" | |
| "github.com/jbrukh/bayesian" | |
| ) | |
| // exported: the classes that are used to store the learned data. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func main() { | |
| classifier := bayesian.NewClassifier(Business, Tech) | |
| enumerateClasses() | |
| learn(classifier) | |
| predict(testFileLocation, classifier) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func predict(location string, classifier *bayesian.Classifier) { | |
| probabilities, _, _ := classifier.ProbScores([]string{readFile(location)}) | |
| fmt.Println(probabilities) | |
| } |