A Bit Stuck!
I'm currently studying artificial neural networks and coding a feed forward network in Java. I have coded a lot of it but i'm a bit stuck now as i ain't done java in a long time. I have commented what i need to do as i understand what its doing, just not how to code it. Any ideas - i have highlighted the bit i'm stuck on in bold down near the bottom. Here is the code:
import java.io.*;import java.util.StringTokenizer;import java.util.Random;publicclass Training{privateint[][][] theLetters;// The training data
privateint[] layer_i;// The Inputs to the networkprivate int[] theError;private int[] layer_h;private int[] layer_o;private float[][] weight_h;private float[][] weight_o;private String theFileName;private float error_c; // pattern errorprivate float error_e = 1; // epoch or loop errorprivate int no_hidden = 10;private int no_output = 26; public Training(){theLetters = new int[26][7][5];layer_i = new int[35];theError = new float[no_output];layer_h = new float[no_hidden];layer_o = new float[no_output];weight_h = new float[35][no_hidden];weight_o = new float[no_hidden][no_output]; } public String createFileName(char choice){theFileName = "F:\\Semester 2\\ANN\\tr-data\\data 2\\" +choice + ".sv";return theFileName;} public void readFile(char choice){int choiceNumber = changeType(choice);try{FileReader letterFile = new FileReader(createFileName(choice));BufferedReader digitStream = new BufferedReader(letterFile);String s = digitStream.readLine();while (s != null){ for(int row = 0; row < 7; row++){StringTokenizer st = new StringTokenizer(s);while (st.hasMoreTokens()){for(int column = 0; column < 5; column++){String word = st.nextToken();int data = Integer.parseInt(word);theLetters[choiceNumber][row][column] = data;}s= digitStream.readLine();} } }digitStream.close();} catch(IOException ex){System.out.println(ex.getMessage());} } public void displayLetter(char choice){int choiceNumber = changeType(choice);System.out.println("displaying Letter "+choice +" details");for(int row =0; row < 7; row++){for(int column = 0; column < 5; column++){System.out.print(theLetters[choiceNumber][row][column]);}System.out.println();}} private int changeType(char choice){int reply = (int) choice - 65;return reply;} public void datatoInputs(int dataset){for(int row = 0; row < 7; row++){{for(int column = 0; column < 5; column++){layer_i[row*5+column] = theLetters[dataset][row][column];}}}} public void displayInput(int dataset){System.out.println("displaying Letter "+dataset+" details");for(int row =0; row < 7; row++){for(int column = 0; column < 5; column++){if (layer_i[row*5+column] > 0 ) System.out.print("+");System.out.print(layer_i[row*5+column]);}System.out.println();}}public void createWeights(){for(int i = 0; i < 35; i++){for(int j = 0; j < no_hidden; j++){float randomValue = (float) ((Math.random() *4.8)-2.4);randomValue = randomValue/35;System.out.println("RANDOM = " + randomValue);weight_h[i][j] = randomValue;System.out.println(weight_h[i][j]);}} for(int i = 0; i < no_hidden; i++){for(int j = 0; j < no_output; j++){float randomValue = (float) ((Math.random() *4.8)-2.4);randomValue = randomValue/no_hidden;weight_o[i][j] = randomValue;}}}public static void main(String args[]){Training myTraining = new Training();for(char readin = 'A'; readin <= 'Z'; readin++) // Reads data from file to array{myTraining.readFile(readin);System.out.println("char " + readin + " is stored as ");myTraining.displayLetter(readin);System.out.println("\n\n");}System.out.println("End of Data read \n\n"); myTraining.createWeights(); [b]do{ // start trainingint count = 0;for(int i; i<layer_i; i++){ // for loop for each pattern// find outputs of hidden layer// data to inputs for this pattern //for each neuron in turn sum += input * connecting weight //ouput = sigmoid of sum// find outputs of ouput layer and error[neuron], error_c, increment error_e// train for current pattern }// end for loop} while(); // error_e > .5 or epoch count < 10[/b] System.out.println("Displaying initial data against results"); }}
Message was edited by:
geebs2006

