I am newbie in machine learning, but decided to make own js library for neural networks, everything went perfect until i tryed to train my NN. In My Mini Library i created some functions...
1) A Function That Creates My Neuron-Object:
this.Node = function (conns) {
var output = {};
output.b = hyth.Random({type: "TanH"});
output.w = [];
for (var a = 0; a < conns; a++){
output.w[a] = hyth.Random({type: "TanH"});
}
output.Value = function (i) {
if (i.length == conns) {
var sum = 0;
for (var a = 0; a < conns; a++){
sum += i[a] * output.w[a];
}
sum += output.b;
return myMath.Activate(sum, {type: "Sigmoid"});
}
}
return output;
}
This function has one argument , which is the amount of wanted weights from neuron, and it returns an object with two properties - "b" the float (bias), and "w" the 1D Array which contains floats, and one method - which calculates the activation of neuron-object.
2) A Function That Creates My Neural Net
this.Network = function () {
var p = arguments;
var arr = [];
for (var a = 0; a < p.length-1; a++){
arr[a] = [];
for (var b = 0; b < p[a+1]; b++){
arr[a][b] = this.Node(p[a]);
}
}
return arr;
}
This Function Returns A 2D Array with Neuron-Object as It's final value, using argument array as settings for layer count and node count for each layer.
3) A Function That Feeds Forward The NN
this.Forward = function (network, input) {
if (network[0][0].w.length == input.length) {
var activations = [];
for (var a = 0; a < network.length; a++){
activations[a] = [];
for (var c = 0; c < network[a].length; c++){
if (a == 0){
activations[0][c] = network[0][c].Value(input);
continue;
}
activations[a][c] = network[a][c].Value(activations[a-1]);
}
}
return activations;
}
}
This Function Returns 2D array with an activation float for every neuron as it's final value. It uses 2 agruments - the output of 2nd function, input array.
4) And Final Function That Backpropagates
this.Backward = function (network, input, target) {
if (network[0][0].w.length == input.length && network[network.length-1].length == target.length) {
var activations = this.Forward(network, input, true);
var predictions = activations[activations.length-1];
var errors = [];
for (var v = 0; v < network.length; v++) {
errors[v] = [];
}
for (var a = network.length-1; a > -1; a--){
for (var x = 0; x < network[a].length; x++) {
var deract = hyth.Deractivate(activations[a][x]);
if (a == network.length-1) {
errors[a][x] = (predictions[x] - target[x]) * deract;
} else {
errors[a][x] = 0;
for (var y = 0; y < network[a+1].length; y++) {
errors[a][x] += network[a+1][y].w[x] * errors[a+1][y];
}
errors[a][x] *= deract;
}
}
}
return errors;
}
}
This Function Returns 2D array with the rror float for every neuron as it's final value. Arguments are 3 - the nnet , input and wanted output.
So I can make a neural network, feed forward and and backpropagate, receive activations and errors, but i always fail to train my net with my errors and activations to work perfect , last time it was outputing same result for every type of input. I want to understand training algorithm from zero , so i need someone's help.
P.S. - i dont want someone say that i need to use famous libraries , i want to understand and make it myself.
ic is my input array and cbList is my internal array that im checking agaist. I want to check the whole array and return the highest value in based on the cbList. I have some ugly code already tried but i dont want to use it, can someone basically make this better and more efficient. I've been searching and cant find anything in my situation.
So basically just want to check ic and if highest pops up, break out and return that value, if no highest is found, then go on to check high value, and so on...
function hcbin(inv){
var fh;
var ic = ['low','med','low','high'];
var cbList = [ 'low', 'med', 'high','highest'];
for( var i = 0; i < ic.length; i++) {
if (ic[i] === cbList[3]) {
$scope.hdc = ic[i];
fh = true;
}
}
if (!fh){
for( var ii = 0; ii < ic.length; ii++) {
if (ic[ii] === cbList[2]) {
$scope.hdc = ic[ii];
fh = true;
}
}
}
if (!fh){
for( var iii = 0; iii < ic.length; iii++) {
if (ic[iii] === cbList[1]) {
$scope.hdc = ic[iii];
fh = true;
}
}
}
if (!fh){
for( var iiii = 0; iiii < ic.length; iiii++) {
if (ic[iiii] === cbList[0]) {
$scope.hdc = ic[iiii];
fh = true;
}
}
}
};
Try this snippet:
var ic = ['low','med','low', 'high'];
var cbList = [ 'low', 'med', 'high','highest'];
var result = null;
for (var i = (cbList.length - 1); i >=0; i--) {
if (ic.indexOf(cbList[i]) > -1) {
result = cbList[i];
break;
}
}
console.log(result);
Make sure cbList keeps the priority order(from low to highest importance) because the for is based upon that.
I have written a Javascript file of two algorithms. As shown in the code below, I am using a for loop to generate random values which are used by both algorithms as input.
At present, I am displaying output of the binarySearch and SearchSorted alternatively.
The problem I am facing is I have to pass the same array values generated by randomlyGenerateArray in the main program to both the algorithms for a meaningful comparison. But I don't know how to change the output format.
I have thought of adding them in different loops, but as I have explained above i need to use the same randomArray values for both the algorithms.
i.e., The below code produces output as shown below -
Binary Search Successful 1
Search Sorted Successful 5
Binary Search Successful 3
Search Sorted Successful 10
How do I display the output of Binary Search First and then display output of Search Sorted? it's something like this. Any help will be greatly appreciated.
Binary Search Successful 1
Binary Search Successful 3
Search Sorted Successful 5
Search Sorted Successful 10
// Binary Search Algorithm
function binarySearch(A,K)
{
var l = 0; // min
var r = A.length - 1; //max
var n = A.length;
var operations = 0;
while(l <= r)
{
var m = Math.floor((l + r)/2);
operations++;
if(K == A[m])
{
console.log('Binary Search Successful %d',operations);
return m;
}
else if(K < A[m])
{
r = m - 1;
}
else
{
l = m + 1;
}
}
operations++;
console.log('Binary Search Unsuccessful %d',operations);
return -1;
}
// Search Sorted Algorithm
function searchSorted(A, K)
{
var n = A.length;
var i = 0;
var operations = 0;
while (i < n)
{
operations++;
if (K < A[i])
{
return -1;
}
else if (K == A[i])
{
console.log('Search Sorted Successful %d', operations);
return i;
}
else
{
i = i + 1;
}
}
operations++;
console.log('Search Sorted Unsuccessful %d', operations);
return -1;
}
// Random Array generator
var randomlyGenerateArray = function(size)
{
var array = [];
for (var i = 0; i < size; i++)
{
var temp = Math.floor(Math.random() * maxArrayValue);
var final = array.splice(5, 0, 30);
array.push(final);
}
return array;
}
//Sort the Array
var sortNumber = function(a, b)
{
return a - b;
}
// Main Program
var program = function()
{
var incrementSize = largestArray / numberOfArrays;
for (var i = smallestArray; i <= largestArray; i += incrementSize)
{
var randomArray = randomlyGenerateArray(i);
var sort = randomArray.sort(sortNumber);
var randomKey = 30;
binarySearch(sort, randomKey);
searchSorted(sort, randomKey);
}
}
var smallestArray = 10;
var largestArray = 10000;
var numberOfArrays = 1000;
var minArrayValue = 1;
var maxArrayValue = 1000;
program();
You could store the sorted randomArrays in an array (which I've called sortedRandomArrays), then run a for loop for each search.
The Main Program would then look like:
// Main Program
var program = function()
{
var incrementSize = largestArray / numberOfArrays;
var sortedRandomArrays = [];
for (var i = smallestArray; i <= largestArray; i += incrementSize)
{
var randomArray = randomlyGenerateArray(i));
var sort = randomArray.sort(sortNumber);
sortedRandomArrays.push(sort);
var randomKey = 30;
}
for (var i = 0; i < sortedRandomArrays.length; i++)
{
binarySearch(sortedRandomArrays[i], randomKey);
}
for (var i = 0; i < sortedRandomArrays.length; i++)
{
searchSorted(sortedRandomArrays[i], randomKey);
}
}
Solution is simple: store the results and print with 2 separate loops (take out the printing from within the functions).
var program = function()
{
var binarySearchResults = [];
var sortedSearchResults = [];
var incrementSize = largestArray / numberOfArrays;
for (var i = smallestArray; i <= largestArray; i += incrementSize)
{
var randomArray = randomlyGenerateArray(i);
var sort = randomArray.sort(sortNumber);
var randomKey = 30;
binarySearchResults[i] = binarySearch(sort, randomKey);
sortedSearchResults[i] = searchSorted(sort, randomKey);
}
for (var i = smallestArray; i <= largestArray; i += incrementSize)
{
//print binary results
}
for (var i = smallestArray; i <= largestArray; i += incrementSize)
{
//print sorted results
}
}
I have quite complicated array which I need to transform to specific format. The array looks like this:
arr = [["name1",51,1,"code1",3],["name2",52,0,"code2",4,"code3",6],["name3",51,2,"code4",3,"code5",6,"code6",1],["name4",55,5,"code7",7,"code8",1],["name5",54,2,"code9",5,"code10",8]];
Each array in my output need contains only 5 values - 3 first values always will be the same like in input. The next 2 values should contain code and the lowest value from the rest of the array. So this case output will look like this:
output = [["name1",51,1,"code1",3],["name2",52,0,"code2",4],["name3",51,2,"code6",1],["name4",55,5,"code8",1],["name5",54,2,"code9",5]];
For start I think the best is use loop for and if instruction, but I don't know how to cope with this later on.
for (i=0; i<arr.length; i++) {
if (arr[i]>5) {
//dont know what to put here
}
}
My solution:
arr = [
["name1",51,1,"code1",3],
["name2",52,0,"code2",4,"code3",6],
["name3",51,2,"code4",3,"code5",6,"code6",1],
["name4",55,5,"code7",7,"code8",1],
["name5",54,2,"code9",5,"code10",8]
];
function process_array(in_array) {
var output = [];
/* check each element in array */
in_array.forEach(function(sub_array){
/* store new sub array here*/
var last_sub_out = [];
var code;
var lowest_num = Number.MAX_VALUE;
/* check sub array
#value is value of sub_array
#index is index of that value.
*/
sub_array.forEach(function(value, index){
/* add first 3 values */
if(index < 3) {
last_sub_out.push( value );
return;
}
/* checking only numbers(even index: 4,6,8, ...) */
if(index % 2 == 0) {
if(value < lowest_num) {
code = sub_array[index - 1];
lowest_num = value;
}
}
});
/* add found code with lowest number */
last_sub_out.push(code, lowest_num);
output.push( last_sub_out );
/* LOG */
document.write(last_sub_out.join(', ') + "</br>");
});
return output;
}
var out = process_array(arr);
var arr = [["name1",51,1,"code1",3],["name2",52,0,"code2",4,"code3",6],["name3",51,2,"code4",3,"code5",6,"code6",1],["name4",55,5,"code7",7,"code8",1],["name5",54,2,"code9",5,"code10",8]];
var out=[];
for (i=0; i < arr.length; i++) {
if (arr[i].length>5) {
out[i] = [arr[i][0], arr[i][1], arr[i][2]];
c = [arr[i][3], arr[i][4]];
for (j=0; j < (arr[i].length - 5)/2; j++){
if (c[1] > arr[i][6+2*j]){
c = [arr[i][5+2*j], arr[i][6+2*j]];
}
}
out[i][3] = c[0]; out[i][4] = c[1];
} else {
out[i] = arr[i]
}
}
Try:
var arr = [["name1",51,1,"code1",3],["name2",52,0,"code2",4,"code3",6],["name3",51,2,"code4",3,"code5",6,"code6",1],["name4",55,5,"code7",7,"code8",1],["name5",54,2,"code9",5,"code10",8]];
var output = [], startIndex = 3, i = 0;
while(i < arr.length){
var item = arr[i++], j = startIndex, count = 0, min = Infinity, code;
while(j < item.length){
count++ % 2 && item[j] < min && (min = item[j], code = item[j-1]);
j++
}
item.splice(startIndex, item.length, code, min);
output.push(item)
}
document.write("<pre>" + JSON.stringify(output, null, 4) + "<pre>");
I'm creating a program that will ask a question and give 5 choices for answers.
One is pre-defined and is correct, the others I want to be random selections from a bank of answers and the entire array is to be shuffled too.
I've written something, but it has some inconsistencies.
For one, sometimes the pre-defined choice appears twice in the list (it appears to skip over my if check).
Another is that sometimes, the editor crashes when I run it.
I use for in loops and I'm worried the crash is caused by a never-ending loop.
Here's my code:
private var numberOfComponents:int;
private var maxComponents:int = 5;
//numberOfComponents returns the length property of my 'components' answer bank
componentsSelection = buildComponentSelectionList(0); //0 is the index of my correct answer
function buildComponentSelectionList(correctItemIndex){
var theArray:Array = new Array();
var indicesOfSelection:Array = getIndicesByIncluding(correctItemIndex);
Debug.Log(indicesOfSelection);
for (var i=0;i<indicesOfSelection.length;i++)
theArray.Push(components[indicesOfSelection[i]]);
return theArray;
}
function getIndicesByIncluding(correctItem){
var indicesArray:Array = new Array();
var numberOfChoices = maxComponents-1;
for(var i=0;i<numberOfChoices;i++){
var number = Mathf.Round(Random.value*(numberOfComponents-1));
addToRandomNumberSelection(indicesArray, number,correctItem);
}
indicesArray.Push(correctItem);
RandomizeArray(indicesArray);
return indicesArray;
}
function addToRandomNumberSelection(indicesArray:Array,number,correctItem){
if(indicesArray.length == 0){
indicesArray.Push(number);
} else {
var doesntExist = true;
for(var i=0;i<indicesArray.length;i++){
if(indicesArray[i] == correctItem)
doesntExist = false;
if (indicesArray[i] == number)
doesntExist = false;
}
if(doesntExist) {
indicesArray.Push(number);
} else {
addToRandomNumberSelection(indicesArray, Mathf.Round(Random.value*(numberOfComponents-1)),correctItem);
}
}
}
function RandomizeArray(arr : Array)
{
for (var i = arr.length - 1; i > 0; i--) {
var r = Random.Range(0,i);
var tmp = arr[i];
arr[i] = arr[r];
arr[r] = tmp;
}
}
The editor is Unity3D, and the code is a version of JavaScript; I think my error is a logic one, rather than a syntactical one.
I feel I've been staring at this code for too long now and I'm missing something obvious.
Can anybody help me?
You can loop through the options and determine the probability that it should be included, then shuffle the included options:
function getRandomOptions(allOptions, correctIndex, count){
var result = [allOptions[correctIndex]];
count--;
var left = allOptions.length;
for (var i = 0; count > 0; i++) {
if (i != correctIndex && Math.floor(Math.random() * left) < count) {
result.push(allOptions[i]);
count--;
}
left--;
}
shuffleArray(result);
return result;
}
function shuffleArray(arr) {
for (var i = arr.length - 1; i > 0; i--) {
var r = Math.floor(Math.random() * i);
var tmp = arr[i];
arr[i] = arr[r];
arr[r] = tmp;
}
}
Demo: http://jsfiddle.net/Guffa/wXsjz/