We are about to switch to a new forum software. Until then we have removed the registration on this forum.
`function Perceptron(n,lines){
this.weights = new Array(lines);
console.log(this.weights);
for (var i = 0; i < this.weights.length; i++){
this.weights[i] = new Array(n);
}
this.lr = 0.002;
for (var i = 0; i < this.weights.length; i++){
for (var j = 0; j < this.weights[i].length; j++){
this.weights[i][j] = random(-1,1);
}
}
this.guesses = new Array(lines);
for (var i = 0; i < this.guesses.length; i++){
this.guesses[i] = function(inputs){
sum = 0;
console.log(n+','+lines);
console.log(this.weights.length);
for (var j = 0; j < this.weights[i].length; j++){
sum += inputs[j]*this.weights[i][j];
}
return sign(sum);
}
}
}`
Here's my output: https://gyazo.com/318a9f2982f0a52efc735cfdb410caa2
As you can see, I first define 'this.weights' as a new array, with length of 'lines' and yes, both arguments to the function are numbers.
But for some reason, I can log this.weights, and it says it's undefined, and I guess that's because all of the elements are undefined?
But it throws an error at line 85, which is simply console.log(this.weights.length);
If I take that line out, then for (var j = 0; j < this.weights[i].length; j++){
throws an error, how can I fix this?
Answers
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
After a quick reading, I've spotted 2 serious JS logical errors in your array of functions.
There can still be others though: :-SS
My fix proposal: declare iterator variable i w/
let
instead ofvar
. *-:)The other error is that you accessthis
inside your looped-created inner functions.However, when again they're eventually invoked, thatthis
is gonna point to the global window, rather than pointing to Perceptron'sthis
instance!My fix proposal: Use lambda fat arrow functions instead of regular 1s for such cases:this.guesses[i] = inputs => {
:>Thanks for the information on
let
and=>
So basically
let
is how to define a local variable, and=>
is a way to define an inner function?let
is 1 of the 5 ways we can declare variables in JS.=>
function is similar to usebind(this)
over a regularfunction
:https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
Ok, thank you! I'm a bit new to java (started about 3-4 days ago)
Your sketch is in JS, not Java... ~O)