c# - Array out of bounds exception in neural network code -


i know there large number of these questions asked problem code specific. reason, don't know how make relevant large number of people.

i practicing iteration in preparation creating first neural network (trying understand possible structure write it). program aims go through , assign weights in similar way neural network might. not use math, iteration purposes only. if there possible suggestions/ recommendations how best write grateful.

main question:

the first out of bounds can see. however, 1 towards bottom cannot see or understand have gone wrong.

using system;  namespace layers {     class program     {         private static random rand = new random();         public static void main(string[] args)         {             // note: first hidden layer (0) input layer.             // initialize layers.             console.writeline("how many layers?");             int[][] hiddenlayers = new int[convert.toint32(console.readline())][];              (int hlcount = 0; hlcount < hiddenlayers.length; hlcount++)             {                 // make first layer input.                 if (hlcount == 0)                 {                     console.writeline("how many inputs?");                 } else                 {                     console.writeline("how many cells in hidden layer " + hlcount + "?");                 }                 hiddenlayers[hlcount] = new int[convert.toint32(console.readline())];             }              // set values inputs.             (int inputcount = 0; inputcount < hiddenlayers[0].length; inputcount++)             {                 console.writeline("enter value input " + (inputcount + 1) + ":");                 hiddenlayers[0][inputcount] = convert.toint32(console.readline());             }              // initialize weights.             int[][] weights = new int[hiddenlayers.length][]; // set of weights each layer.              (int weightcount = 0; weightcount < weights.length; weightcount++)             {                 try                 {                     // create weights layers underneath. +1 attaches layer below exceed array.                     weights[weightcount] = new int[hiddenlayers[weightcount +1].length];                  } catch (exception ex)                 {                  }             }              (int inputcount = 0; inputcount < weights[0].length; inputcount++)             {                 weights[0][inputcount] = rand.next(10); // set first layers weights.             }              int intcount = 0;             (int layerindex = 1; layerindex < hiddenlayers.length; layerindex++)             {                 // re-calculate weights. go through layer , change weights.                 if (intcount < weights[layerindex-1].length)                 {                     weights[layerindex][intcount] = rand.next(1, 10);                     intcount++;                 }                  console.writeline("layer: " + convert.tostring(layerindex));                  // go through cells on layer.                 (int cellindex = 0; cellindex < hiddenlayers[layerindex].length; cellindex++)                 {                     // out of bounds exception. catching affects overall performance of outcome.                     hiddenlayers[layerindex][cellindex] += (convert.toint32(hiddenlayers[layerindex - 1][cellindex]) + convert.toint32(weights[layerindex][cellindex]));                 }             }              console.readline();         }          static void getweights()         {          }     } } 

here more information trying make. modifications such user being able determine layer sizes, inputs etc.

http://i.stack.imgur.com/x9cn3.png

the inputs used was: 3 layers 3 inputs hidden layer 1: 3 hidden layer 2: 3 input 1: 12 input 2: 13

most important: not ever use try/catch "solve" indexoutofrangeexception problem. matter, don't use solve any exception did not expect get. doing guaranteed wrong thing do, always. if exception occurs did not anticipate occur, have bug , need fix bug, not try sweep under rug try/catch.

as far rest of question goes…


when run code, nullreferenceexception due uninitialized array member (because caught earlier exception instead of fixing code). happens before code reaches line claim indexoutofrangeexception occurs.

just looking @ program statement asking about, see no reason should throw exception. it's hard sure, because code isn't written in way reproduce problem you're describing.

note because force enter data run program, have no way know if running same data are. thus, have not provided a good, minimal, complete code example reliably reproduces problem.

when change code first computational loop looks this:

for (int weightcount = 0; weightcount < weights.length; weightcount++) {     // create weights layers underneath (caught out of bounds exception doesn't seem matter).     weights[weightcount] = new int[hiddenlayers[weightcount].length]; } 

…both exceptions able reproduce go away , code runs completion. whether want, have no idea. seems reasonable thing do, without more detail actual algorithm you're trying implement, it's impossible know sure.


if above not seem address question in useful way, please improve question a) include code example, , b) it's clear asking.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

wso2esb - How to concatenate JSON array values in WSO2 ESB? -