matiasvlevi / Dann

Deep Neural Network Library for JavaScript.
https://dannjs.org
MIT License
425 stars 23 forks source link

[🔺 malformed arrows when logging nn.toFunction ] #48

Closed dsmith73 closed 2 years ago

dsmith73 commented 2 years ago

Bug description

the console.log provides a function with malformed arrow statements

To Reproduce

// logging the following snippet toFunction will produce malformed arrow statements
// ex. 
// a[1]=(1+Math.exp(-t)=>);a[2]=(1+Math.exp(-t)=>);a[3]=(1+Math.exp(-t)=>);  

// initialize Dann with 1 input and 1 output  
let nn = new Dann(1, 1)

// Number of neuron layers in the neural net  
nn.addHiddenLayer(3, "sigmoid")
nn.addHiddenLayer(3, "sigmoid")

// how to calculate output  
nn.outputActivation("sigmoid")

// assign random weights to layers  
nn.makeWeights()

// How fast should it learn?  
nn.lr = 0.1

// mean square errorrate  
nn.setLossFunction("mse")

// show info about the neural network  
nn.log()

// Training data  
for(let count=0; count < 1000; count++) {
    let randNum = Math.random()*10 - 5
    nn.backpropagate([randNum], [randNum < 0 ? 0 : 1])
}
console.log(nn.loss)

// log the function
console.log(nn.toFunction())

// Logging the function produces the following -   
// function myDannFunction(input){let w=[];w[0]=[[118.16350397261459],[125.7198197305115],[-61.353668013979465]];w[1]=[[-0.3268324018128853,-0.10547783949606436,1.2385617474541086],[-4.756040201258138,-5.530586211507047,2.0654393849840065],[-6.638909077737027,-6.373098375160245,3.4436506766914343]];w[2]=[[-0.27843549703223947,-1.8499126518203834,-2.4900563442361467]];let b=[];b[0]=[[6.431720708819896],[5.712699746115524],[-8.928168116731628]];b[1]=[[-0.8442173421026961],[-1.8347328438421329],[-1.6789862537895264]];b[2]=[[1.2994856424588022]];let c=[1,3,3,1];let a=[];a[1]=(1+Math.exp(-t)=>);a[2]=(1+Math.exp(-t)=>);a[3]=(1+Math.exp(-t)=>);let l=[];l[0]=[];for(let i=0;i<1;i++){l[0][i]=[input[i]]};for(let i=1;i<4;i++){l[i]=[];for(let j=0;j<c[i];j++){l[i][j]=[0]}};for(let m=0;m<3;m++){for(let i=0;i<w[m].length;i++){for(let j=0;j<l[m][0].length;j++){let sum=0;for(let k=0;k<w[m][0].length;k++){sum+=w[m][i][k]*l[m][k][j]};l[m+1][i][j]=sum}};for(let i=0;i<l[m+1].length;i++){for(let j=0;j<l[m+1][0].length;j++){l[m+1][i][j]=l[m+1][i][j]+b[m][i][j]}};for(let i=0;i<l[m+1].length;i++){for(let j=0;j<l[m+1][0].length;j++){l[m+1][i][j]=a[m+1](l[m+1][i][j])}}};let o=[];for(let i=0;i<1;i++){o[i]=l[3][i][0]};return o}

// passing data to the model  
nn.feedForward([25], {log: true, decimals: 3})

Expected behavior

Generate a usable function

Actual behavior

Logging produces the following statements:

a[1]=(1+Math.exp(-t)=>);
a[2]=(1+Math.exp(-t)=>);
a[3]=(1+Math.exp(-t)=>);  

Platform


matiasvlevi commented 2 years ago

Hey! Thank you for pointing that out! It seemed to work on the non-minified build, but did not work for the minified build.

This was because .toString outputs the function written as is, and with the minified build having a minified code, the nn.toFunction tried parsing a function like a normal function declaration.

I changed how the library converts pre-es6 functions to es6 ones. It looks like it's fixed now. I ran your code in the browser with chrome and it produced an output!

As of v2.4.1c the issue should be resolved