Last active
August 16, 2024 06:11
-
-
Save TheBrenny/518f4eed345f0ebb4ad6ba0d2d46e381 to your computer and use it in GitHub Desktop.
Sorry, I didn't scale the 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
// x and y are arrays of -10 to +10 at steps of 0.2, | |
// f is the function of the answer line, | |
// targets is a 2d array of all f(x,y) | |
let x = genNumbers(-10, 10, 0.2); | |
let y = genNumbers(10, -10, -0.2); // 10 is min and -10 is max so it looks more normal in the array | |
let f = (x1, x2) => (3 * x1 + 6 * x2 - 24); | |
// let targets = y.map(y => y.map(x => f(x, y))); | |
// The random weights and bias, as well as the generated nn-node | |
let w1 = rand(-10, 10); | |
let w2 = rand(-10, 10); | |
let b = rand(-10, 10); | |
let nn = (x1, x2) => (w1 * x1 + w2 * x2 - b); | |
console.log(` w1: ${w1.toFixed(2)}, w2: ${w2.toFixed(2)}, b: ${b.toFixed(2)}`); | |
console.log(`Targets: 2.00, 1.00, 4.00`); | |
console.log(""); | |
// find a random point and determine the target and the actual result | |
let t = [rand(-10, 10).toFixed(2), rand(-10, 10).toFixed(2)]; | |
console.log(` f(${t[0]}, ${t[1]}) = ${f(t[0], t[1])}`); | |
console.log(`nn(${t[0]}, ${t[1]}) = ${nn(t[0], t[1])}`); | |
// Calculate the error | |
let eSquared = Math.pow(f(t[0], t[1]) - nn(t[0], t[1]), 2); | |
console.log(` e: ${eSquared}`); | |
// ------------------- COMPARISONS | |
function sign(x) { | |
return Math.sign(x); | |
} | |
// ------------------- GENERATE NUMBERS | |
function genNumbers(min, max, step) { | |
step = step || 1; | |
let r = []; | |
let c = (min, max, i) => min < max ? i <= max : i >= min; | |
for (let i = min; c(min, max, i); i += step) r.push(i); | |
return r; | |
} | |
function numOfRands(min, max, num) { | |
let r = []; | |
for (let i = 0; i < num; i++) r.push(rand(min, max)); | |
return r; | |
} | |
function rand(min, max) { | |
return Math.random() * (max - min) + min; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment