JSFiddle - React, Tailwind, and code Playground
HTML
<button onclick="location.reload()">
Reload page
</button>
<div id="info">
Wait for calculations...
</div>
JavaScript
const INPUT_LAYER_SIZE = 16
const OUTPUT_LAYER_SIZE = 1
const HIDDEN_LAYER_SIZE = 3
const ITERATIONS = 10000
function main(){
document.querySelector("#info").innerHTML = ""
var trainingSet = generateTestData(0, 100)
var testSet = generateTestData(10000, 11000)
var weights, bestWeights
var bestCorrectness = -1
for (var i=0; i<ITERATIONS; i++){
weights = getRandomWeights()
var correctness = getCorrectness(weights, trainingSet)
if (correctness > bestCorrectness) {
bestCorrectness = correctness
bestWeights = cloneObject(weights)
var testSetCorrectness = getCorrectness(weights, testSet)
document.querySelector("#info").innerHTML += "New best correctness in training (test) set:" + (correctness * 100) + "%(" + testSetCorrectness * 100 + "%)" + "<br>"
}
}
}
function getRandomWeights(){
return {
hiddenLayer: getRandomLayerWeights(INPUT_LAYER_SIZE, HIDDEN_LAYER_SIZE),
outputLayer: getRandomLayerWeights(HIDDEN_LAYER_SIZE, OUTPUT_LAYER_SIZE)
}
}
function getRandomLayerWeights(inputSize, neuronCount){
var layerWeights = [];
for (var j=0; j<neuronCount; j++) {
var neuronWeights = getArrayWithNRandomNumbers(inputSize, -5, 5);
layerWeights.push(neuronWeights)
}
return layerWeights;
}
function predict(inputLayer, weights){
var neuronLayers = [
weights.hiddenLayer.map(neuronWeights => new Neuron(neuronWeights)),
weights.outputLayer.map(neuronWeights => new Neuron(neuronWeights))
]
var previousLayerResult = inputLayer;
neuronLayers.forEach(function(layer){
previousLayerResult = layer.map(neuron => neuron.process(previousLayerResult))
})
return previousLayerResult
}
function exampleIsPredictedCorrectly(example, weights){
var prediction = predict(example.input, weights);
prediction[0] = limitRangeBetween0And1(prediction[0])
var error =...