JSFiddle - React, Tailwind, and code Playground
by Robodude
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Babel + JSX
function sigmoid(t) {
return 1/(1+Math.pow(Math.E, -t));
}
const data = [];
for (let i = 0; i < 90000; i++) {
data.push({
values: [0, 0],
answer: 0
})
data.push({
values: [0, 1],
answer: 1
})
data.push({
values: [1, 0],
answer: 1
})
data.push({
values: [1, 1],
answer: 0
})
}
function train(data){
const learningRate = 0.0001
let weights = [0, 0];
let hl1 = [0, 0];
let hl2 = [0, 0];
shuffled.forEach(item => {
// input is now an array, so let's create a weight for each item
item.values.forEach((x, i, arr) => {
// make a guess
const guess = x * weights[i]
const error = item.answer - weights[i]
const adjustedError = error * learningRate
weights[i] = weights[i] + adjustedError
const sum = _.sum(arr)
})
})
console.log(weights);
return (d) => d.reduce((acc, x, i) => acc + (x * weights[i]), 0)
}
const shuffled = _.shuffle(data);
const predict = train(shuffled)
console.log(predict([0, 0]))
console.log(predict([0, 1]))
console.log(predict([1, 0]))
console.log(predict([1, 1]))