xxxxxxxxxx
75
// Step 1: load data or create some data
// const data = [
// {r:255, g:0, b:0, color:'red-ish'},
// {r:254, g:0, b:0, color:'red-ish'},
// {r:253, g:0, b:0, color:'red-ish'},
// {r:0, g:255, b:0, color:'green-ish'},
// {r:0, g:254, b:0, color:'green-ish'},
// {r:0, g:253, b:0, color:'green-ish'},
// {r:0, g:0, b:255, color:'blue-ish'},
// {r:0, g:0, b:254, color:'blue-ish'},
// {r:0, g:0, b:253, color:'blue-ish'}
// ];
// Step 2: set your neural network options
// const options = {
// task: 'classification',
// debug: true,
// hiddenUnits: 3
// }
const options = {
dataUrl: "data/entries.json",
task: 'classification',
inputs:["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
outputs:['color'],
debug: true
}
// Step 2: initialize your neural network
const nn = ml5.neuralNetwork(options, dataLoaded);
// Step 3: normalize data and train the model
function dataLoaded(){
nn.normalizeData();
trainModel();
}
// Step 4: train the model
function trainModel(){
const trainingOptions = {
epochs: 32,
batchSize: 12
}
nn.train(trainingOptions, finishedTraining);
}
// Step 5: use the trained model
function finishedTraining(){
// classify();
nn.neuralNetwork.model.layers[0].getWeights()[0].print()
nn.neuralNetwork.model.layers[0].getWeights()[1].print()
console.log(nn.neuralNetwork.model.layers)
}
// Step 6: make a classification
function classify(){
const input = {
r: 255,
g: 0,
b: 0
}
nn.classify(input, handleResults);
}
// Step 7: define a function to handle the results of your classification
function handleResults(error, result) {
if(error){
console.error(error);
return;
}
console.log(result); // {label: 'red', confidence: 0.8};
}