Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing Scaling Params Prevents Scaling Data - FANN Error 18 #6

Open
geekgirljoy opened this issue Jul 8, 2021 · 1 comment
Open

Comments

@geekgirljoy
Copy link

geekgirljoy commented Jul 8, 2021

So it seems that the function necessary for setting the the scaling params on a neural network is missing from fann.js.

For example, your dataset might look like this:

var UNSCALED_XOR_DATA = [
    [[0, 0], [0]],
    [[100, 100], [0]],
    [[0, 100], [100]],
    [[100, 0], [100]]
];

Where the range is not between -1 and 1 and FANN can rescale this data for us if we set the parameters based on the range we want and the dataset.

I.E.

FANN can turn the UNSCALED_XOR_DATA into:

var SCALED_XOR_DATA = [
    [[-1, -1], [-1]],
    [[ 1,  1], [-1]],
    [[-1,  1], [ 1]],
    [[ 1, -1], [ 1]]
];

However in order to do so you have to set the params on the ANN object which seems to be missing from FANN.js.

Here is an example of how this would work:

<script async src="../fann.js"></script>
<script>

var UNSCALED_XOR_DATA = [
    [[0, 0], [0]],
    [[ 100, 100], [0]],
    [[0, 100], [100]],
    [[100, 0], [100]]
];

function XOR () {
    
    ////////////////////
    // Configure ANN  //
    ////////////////////
    
    // New ANN
    NN = FANN.create([2, 3, 1]);
    
    // Set activation functions
    NN.set_activation_function_hidden(FANN.SIGMOID_SYMMETRIC);
    NN.set_activation_function_output(FANN.SIGMOID_SYMMETRIC);
    
    // Read raw (un-scaled) training data
    data = FANN.createTraining(UNSCALED_XOR_DATA);
    

    // Given the data range present in the data variable
    // configure the ANN with the desired scaling range
    NN.set_input_scaling_params(data, -1, 1);
    NN.set_output_scaling_params(data, -1, 1);
    
    // Scale the data range to -1 to 1
    // Scale the data - DOES NOT WORK because the ANN has not been configured with scaling params
    data = NN.scale_train(data, -1, 1); // Results in FANN Error 18: Scaling parameters not present.
    
    
    ///////////
    // Train //
    ///////////
    NN.train_on_data(data, 1000, 10, 0.01);
    

    //////////
    // Test //
    //////////
    var results = "";
    results += " -1 , -1 => " + NN.run([-1, -1])[0] + '\n<br>';
    results += "-1 ,  1 => " + NN.run([-1, 1])[0] + '\n<br>';
    results += " 1 , -1 => " + NN.run([1, -1])[0] + '\n<br>';
    results += " 1 ,  1 => " + NN.run([1, 1])[0] + '\n<br>';
    
    ////////////////////
    // Output Results //
    ////////////////////
    document.getElementById('results').innerHTML = results;
}

FANN_ready = function () {
    XOR();
};
</script>
<div id='results'></div>
@geekgirljoy geekgirljoy changed the title Missing Scaling Params Prevents Scaling Data Missing Scaling Params Prevents Scaling Data - FANN Error 18 Jul 8, 2021
@geekgirljoy
Copy link
Author

The following code will work around this limitation however ideally FANN.JS would support scaling natively through FANN rather than doing it manually as I demonstrate here.

<meta charset="utf-8"/>
<script async src="../fann.js"></script>
<script>

var UNSCALED_XOR_DATA = [
    [[0, 0], [0]],
    [[100, 100], [0]],
    [[0, 100], [100]],
    [[100, 0], [100]]
];

function Scale(dataset, min_scaled_value, max_scaled_value){

    var dataset = dataset.slice(); // no references to the array passed to the Scale() function

    var max_value = Math.max(...[].concat(...[].concat(...dataset)));
    var min_value = Math.min(...[].concat(...[].concat(...dataset)));
  

    // for all the unscaled data
    for (var i = 0; i < dataset.length; i++) {
        
        // for each input and output set
        for (var j = 0; j < dataset[i].length; j++) {
            
            // for each value in a set
            dataset[i][j].forEach((n, key) =>
                dataset[i][j][key] = ((max_scaled_value - min_scaled_value) * (n - min_value) / (max_value - min_value) + min_scaled_value)
            );
        }
    }  

    return dataset;
}

function XOR () {
    
    ////////////////////
    // Configure ANN  //
    ////////////////////
    
    // New ANN
    NN = FANN.create([2, 3, 1]);
    
    // Set activation functions
    NN.set_activation_function_hidden(FANN.SIGMOID_SYMMETRIC);
    NN.set_activation_function_output(FANN.SIGMOID_SYMMETRIC);
    
    
    var scaled_dataset = Scale(UNSCALED_XOR_DATA, -1, 1); // scale data to a range of -1 to 1
        
    // Read scaled training data
    var data = FANN.createTraining(scaled_dataset);
    
    NN.init_weights(data);
    ///////////
    // Train //
    ///////////
    NN.train_on_data(data, 1000, 10, 0.01);
    

    //////////
    // Test //
    //////////
    var results = "";
    results += " -1 , -1 => " + NN.run([-1, -1])[0] + '\n<br>';
    results += "-1 ,  1 => " + NN.run([-1, 1])[0] + '\n<br>';
    results += " 1 , -1 => " + NN.run([1, -1])[0] + '\n<br>';
    results += " 1 ,  1 => " + NN.run([1, 1])[0] + '\n<br>';
    
    ////////////////////
    // Output Results //
    ////////////////////
    document.getElementById('results').innerHTML = results;
}

FANN_ready = function () {
    XOR();
};
</script>
<div id='results'></div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant