-
Notifications
You must be signed in to change notification settings - Fork 22
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
Comments
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
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:
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:
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:
The text was updated successfully, but these errors were encountered: