Code Monkey home page Code Monkey logo

fann.js's Introduction

FANN.js

The FANN (Fast Artificial Neural Network) library compiled through Emscripten. This library contains some higher level bindings.

Much of the original documentation is still relevant. The noticable changes will be documented below. Note not all functions have a binding. You may see mention of Fixed vs Float mode, FANN.js uses Float mode.

How to use

FANN.js can be used almost like for like with the original library. These bindings provide an object oriented approach. For example take fann_print_connections(ann). In FANN.js this function is available on a Network instance as network.print_connections(). Notice the fann_ prefix isn't necessary nor is passing the neural network reference.

Before using the FANN library you should set a callback to window.FANN_ready.

FANN_ready = function () {
	// FANN.js is ready to use	
	var network = FANN.create([3, 3, 1]);
};

Demos

FANN.

Network create(neurons Array<Number>)

  • neurons the number of neurons in an array. The length of the array is the number of layers

Create a network with the provided structure. Returns an instance of the Network class. The neurons array argument specifies how many neurons are in that layer. The first index will be the input layer, the last index will be the output layer and anything between will be hidden layer(s).

var network = FANN.create([
	2, // Input layer: 2 neurons
	2, // Hidden layers (1): 2 neurons
	1  // Output layer: 1 neuron
]);

Network create(exported_network String)

  • exported_network String of a network exported through .export()

Create a network from a previously exported network as a string. This is useful when restoring a network.

TrainingData createTraining(Array data)

  • data a multidimensional array containing a set of inputs and desired outputs.

Create a TrainingData instance based on the provided data.

var tdata = FANN.createTraining([
    [[-1, -1], [-1]],
    [[ 1,  1], [-1]],
    [[-1,  1], [ 1]],
    [[ 1, -1], [ 1]]
]);

TrainingData createTraining(String data)

  • data a string of the training data. The required format is documented here.

Create a TrainingData instance based on the provided data. The string format will be the same as when exported via .export().

Network.

Array<Number> run(inputs Array<Number>)

  • inputs array of number inputs. Length should correspond to the amount of input neurons

Run the network with a set of inputs. Returns an array of the output neurons value.

var network = FANN.create([2, 2, 1]);
console.log(network.run([-1, 1]));

String export()

Returns a large string containing a snapshot of the network. You can store this string and restore a network based on the snapshot data using FANN.create().

Other functions

The following functions are available on a Network instance. The only difference between the original documented function is you do not need to pass the network reference as the first argument or use the fann_ prefix.

TrainingData.

String export()

Returns a large string containing a snapshot of the training data. You can store this string and create a TrainingData instance using FANN.createTraining().

Other functions

Constants

All of the enum and constants from the original library are available under the FANN. namespace. For example, FANN_TRAIN_INCREMENTAL becomes FANN.TRAIN_INCREMENTAL.

Compiling

After cloning, simply run ./build.sh build. The built file will be in the root directory as fann.js.

fann.js's People

Contributors

louisstow avatar

Stargazers

Andy Mac avatar Michael Barley avatar Yasin ATEŞ avatar 新无止竞博客 avatar WhileTrue avatar  avatar Velichko Ilya avatar Znak Zorro avatar Prone Bone avatar  avatar  avatar Adriano Maciel avatar wth avatar Sergei Sokolov avatar Laurent Tempier avatar D. Reyburn avatar Rolf Lindén avatar nobody avatar Sebastian Herrlinger avatar TiX avatar Nutmeg Anne avatar Amila Welihinda avatar Jérôme Beau avatar Erik Österberg avatar Nick Lyell avatar Marc Lebrun avatar QuantJin avatar hamlet avatar  avatar Martin Stratiev avatar Ruben avatar Jan Kaniewski avatar Sam Collard avatar Ankit Kuwadekar avatar Harsh Vakharia avatar Lukas Liesis avatar  avatar Sunny Gonnabathula avatar TSD avatar Sébastien ELET avatar Dirk Adler avatar Angus H. avatar Florian avatar Andżelo avatar Alan James avatar David Šolc avatar Isaac Su avatar Jeffrey Mathews avatar Alvar Laigna avatar Michael Anthony avatar Nuno Correia avatar Ruben Vicario Gonzalez avatar Taehoon Moon avatar Ryan Wu avatar fxwan avatar Huang Liang avatar DarkMan avatar Andrew Griffiths avatar jd avatar April Johnson avatar Glenn Y. Rolland avatar Allan Marques Baptista avatar Alexander Kozhevin avatar xeLL avatar Sjoerd de Jong avatar Tianyu Yao avatar dann toliver avatar Drew Miller avatar David Pollard avatar Sina Sharafzadeh avatar  avatar Mohammad Sadegh Khoeini avatar SomeRandomPerson avatar zenyoda avatar Pawel Wieladek avatar Veaceslav Cotruta avatar Andrei Cacio avatar Yang Li avatar srbk avatar KAMATA Yoshiharu avatar Fanli (Christian) Ramsey avatar  avatar Steve Chikwaya avatar Prathamesh Satpute avatar Vladislav Botvin avatar  avatar David Levy avatar Michael Isgro avatar Jonathan Hendler avatar Franco Barbeite avatar LB (Ben Johnston) avatar Sean Powell avatar Brandon B avatar Athan avatar John S. Dvorak avatar Steven avatar  avatar  avatar Robert Jefe Lindstädt avatar Rakesh mohanta avatar

Watchers

 avatar evandrix avatar Jacob Santos avatar Alen Birindzic avatar Hongbo Zhang avatar timelyportfolio avatar Zaktus avatar Tom Siwik avatar Dachi avatar Michael Anthony avatar Ruben Amaury avatar Fredy Mendez avatar 新无止竞博客 avatar Camilo Barbosa avatar  avatar  avatar Michael Ding avatar Abhishek Kumar avatar Demeter Macik avatar  avatar

fann.js's Issues

Missing Scaling Params Prevents Scaling Data - FANN Error 18

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>

Missing FANN Merge Training Data Binding

The binding for fann_merge_train_data is not available.

In an ideal world that binding would exist but here is a workaround if/until it ever gets added to FANN.js:

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

function MergeData(data1, data2, data_has_header){
    
    // Split by new-lines
    data1 = data1.split(/\n/);
    data2 = data2.split(/\n/);
	
    // Remove header if present
    data1 = data1.slice(data_has_header ? 1 : 0);
    data2 = data2.slice(data_has_header ? 1 : 0);
  
    // Merge arrays
    var data = data1.concat(data2);
 
    // Create new FANN header
    var input_count = data1[0].split(' ').length;
    var output_count = data1[1].split(' ').length;
    var header = ((data1.length / 2)+(data2.length / 2)) + " " + input_count + " " + output_count;

    // Add the header to the beginning of the data
    data.unshift(header); 
    
    // Convert the data back into a new-line delimited string and return
    return data.join('\n', data);
}

function XOR () {

    // New ANN
    NN = FANN.create([2, 3, 1]);
	
    ////////////////////
    // Configure ANN  //
    ////////////////////
    NN.set_activation_steepness_hidden(1);
    NN.set_activation_steepness_output(1);
    NN.set_activation_function_hidden(FANN.SIGMOID_SYMMETRIC);
    NN.set_activation_function_output(FANN.SIGMOID_SYMMETRIC);
    NN.set_train_stop_function(FANN.STOPFUNC_BIT);
    NN.set_bit_fail_limit(0.01);
    NN.set_training_algorithm(FANN.TRAIN_RPROP);

    ////////////////////
    // Training Data  //
    ////////////////////
    var data_has_header = true;
    var data1 = "2 2 1\n-1 -1\n-1\n-1 1\n1";
    var data2 = "2 2 1\n1 -1\n1\n1 1\n-1";

    // Merge data1 & data2 
    // NOTE: The inputs and outputs MUST be the same length in both datasets
    var merged_xor = MergeData(data1, data2, data_has_header);
    //console.log(merged_xor);
    /*
	Result:

	"4 2 1
	-1 -1
	-1
	-1 1
	1
	1 -1
	1
	1 1
	-1"

    */
	
    data = FANN.createTraining(merged_xor);
	
    ///////////
    // Train //
    ///////////
    NN.init_weights(data);
    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>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.