Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

223 lines (199 sloc) 7.855 kb
<html>
<head>
<title>Neural.js</title>
<!--<link rel="stylesheet" href="/css/style.css">-->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
<div id="result"></div>
<script type="text/javascript">
// Global
var network, Network;
var network_shorthand_map = {
'word-topic': 'wt'
, 'topic-action': 'ta'
};
$(document).ready(function() {
network = new TopicNetwork();
});
var TopicNetwork = function() {
var request = {};
request.database = "Neural-021";
request.description = "Basic MLP neural network."
Network = new Neural( request );
TopicNetwork.prototype.shorthand.map.set( network_shorthand_map );
//TODO: extend shorthand
};
// Adds one node to the Network
TopicNetwork.prototype.node = TopicNetwork.prototype.node || {};
TopicNetwork.prototype.node.add = function( request ) {
var nodes = request.nodes;
var on_success = function( result ) {
console.log( 'TopicNetwork.prototype.node.add success', result );
if( 'function' === typeof request.on_success ) {
request.on_success( result );
}
};
var on_error = function( context ) {
console.log( 'TopicNetwork.prototype.node.add error', context );
if( 'function' === typeof request.on_error ) {
request.on_error( result );
}
};
Network.neurons.add( { 'nodes': nodes, 'on_success': on_success, 'on_error': on_error } );
return this;
}
// Adds one or more topic nodes to the network
TopicNetwork.prototype.nodes = TopicNetwork.prototype.nodes || {};
TopicNetwork.prototype.nodes.populate = function( request ) {
var input = request.input;
var output = request.output;
if( 'undefined' === typeof input || 'undefined' === typeof output ) {
return null;
}
if( 'string' == typeof request.input ) {
request.input = [ request.input ];
}
if( 'string' == typeof request.output ) {
request.output = [ request.output ];
}
TopicNetwork.prototype.nodes.create( { 'input': request.input, 'output': request.output } );
};
/*
* Example:
* input (total: 3) = [ 'iPod', 'competitor', 'Zune' ]
* output (total: 2) = [ 'Apple', 'Microsoft' ]
* hidden = (total: 6) = [ [ 'Apple', 'iPod' ], [ 'iPod', 'Microsoft' ], [ 'Apple', 'competitor' ], [ 'competitor', 'Microsoft' ], [ 'Apple', 'Zune' ], [ 'Microsoft', 'Zune' ] ]
* connections (total: 6) =
iPod (input, total: 2) -> [ [ 'Apple', 'iPod' ], [ 'iPod', 'Microsoft' ] ]
competitor (input, total: 2) -> [ [ 'Apple', 'competitor' ], [ 'competitor', 'Microsoft' ] ]
Zune (input, total: 2) -> [ [ 'Apple', 'Zune' ], [ 'Microsoft', 'Zune' ] ]
//FALSE: Apple (output, total: 3) -> [ [ 'Apple', 'iPod' ], [ 'Apple', 'competitor', ], [ 'Apple', 'Zune' ] ]
//FALSE: Microsoft (output, total: 3) -> [ [ 'iPod', 'Microsoft' ], [ 'competitor', 'Microsoft' ], [ 'Microsoft', 'Zune' ] ]
*/
TopicNetwork.prototype.nodes.create = function( request ) {
var input = request.input;
var input_length = input.length;
var output = request.output;
var output_length = input.length;
if( !( ouput_length > 0 || input_length > 0 ) ) {
return null;
}
var input_nodes = {};
var output_nodes = {};
var nodes = [];
// Add 1 node for each input and output, and a hidden node combination
// Total # of nodes should be input.length + output.length + ( input.length * output.length )
if( input_length > 0 ) {
//change input to keyed dictionary objects key becomes hash, value is payload of node
for( var x = 0; x < input_length; x++ ) {
var val = input[ x ];
var hash = TopicNetwork.prototype.utils.topicToId( val );
nodes.push( { 'network': network_shorthand[ 'word-topic' ], 'type': 'input', 'hash': val, 'payload': val } );
// Add a pairwise hidden node
for( var y = 0; y < input_length; y++ ) {
var value = [ val, input[ y ] ];
var hash = TopicNetwork.prototype.utils.topicsToId( value );
nodes.push( { 'type': 'hidden', 'hash': hash, 'payload': value } );
}
}
}
if( output_length > 0 ) {
for( var x = 0; x < output_length; x++ ) {
//output[ x ];
output_nodes.push( {} );
}
}
// Add 1 connection between each input and hidden node, and each output and hidden node
// Total # of connections should be input.length^2
for( var x = 0; x < hidden_nodes.length; x++ ) {
for( var x = 0; x < input_nodes.length; x++ ) {
//if input is part of hidden node
}
}
return this;
}
TopicNetwork.prototype.nodes.add = function( request ) {
var topics = request.topics || request.topic;
var nodes = [];
if( 'undefined' === typeof topics.length ) {
var hash = topics.hash;
hash = ( 'undefined' !== typeof hash ) ? hash : topics;
var type = topics.type;
type = ( 'undefined' !== typeof type ) ? type : 'hidden';
nodes.push( {
'type': type
, 'hash': hash
} );
} else {
for( var x = 0; x < topics.length; x++ ) {
var topic = topics[ x ];
var hash = topic.hash;
hash = ( 'undefined' !== typeof hash ) ? hash : topic;
var type = topics.type;
type = ( 'undefined' !== typeof type ) ? type : 'hidden';
nodes.push( {
'type': type
, 'hash': hash
} );
}
}
var on_success = function( result ) {
console.log( 'TopicNetwork.prototype.topic.add success', result );
if( 'function' === typeof request.on_success ) {
request.on_success( result );
}
};
var on_error = function( context ) {
console.log( 'TopicNetwork.prototype.topic.add error', context );
if( 'function' === typeof request.on_error ) {
request.on_error( result );
}
};
var on_complete = function() {
console.log( 'TopicNetwork.prototype.topic.add complete' );
if( 'function' === typeof request.on_complete ) {
request.on_complete();
}
};
for( var x = 0; x < nodes.length; x++ ) {
TopicNetwork.prototype.node.add( { 'nodes': nodes, 'on_success': on_success, 'on_error': on_error } );
}
if( 'undefined' !== typeof on_complete ) {
on_complete();
}
return this;
};
TopicNetwork.prototype.utils = TopicNetwork.prototype.utils || {};
TopicNetwork.prototype.utils.alphaSortArray = function( unsorted ) {
return unsorted.sort( TopicNetwork.prototype.utils.alphaSort );
};
TopicNetwork.prototype.utils.alphaSort = function( a, b ) {
// http://stackoverflow.com/questions/4340227/sort-mixed-alpha-numeric-array
var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;
var aA = a.replace(reA, "");
var bA = b.replace(reA, "");
if(aA === bA) {
var aN = parseInt(a.replace(reN, ""), 10);
var bN = parseInt(b.replace(reN, ""), 10);
return aN === bN ? 0 : aN > bN ? 1 : -1;
} else {
return aA > bA ? 1 : -1;
}
}
// Takes an array of topic_ids and returns a unique hash
TopicNetwork.prototype.utils.topicsToId = function( topics ) {
var sorted_topics = TopicNetwork.prototype.utils.alphaSortArray( topics );
return md5( sorted_topics.join("|") );
};
// Takes an array of topic_ids and returns a unique hash
TopicNetwork.prototype.utils.topicToId = function( topic_id ) {
return md5( topic_id );
};
</script>
<script type="text/javascript" src="http://github.com/editor/TopicNetwork.prototype/raw/master/indb.dev.js"></script>
<script type="text/javascript" src="https://github.com/wbond/md5-js/raw/master/md5.js"></script>
<script type="text/javascript" src="https://github.com/editor/Neural/raw/master/neural.dev.js"></script>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.