A Bit Stuck - Attempt 2
Hopefully code will be better formatted this time.
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 - the bit i am stuck on is the "Do While" commented part at 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 network
privateint[] theError;
privateint[] layer_h;
privateint[] layer_o;
privatefloat[][] weight_h;
privatefloat[][] weight_o;
private String theFileName;
privatefloat error_c;// pattern error
privatefloat error_e = 1;// epoch or loop error
privateint no_hidden = 10;
privateint no_output = 26;
public Training()
{
theLetters =newint[26][7][5];
layer_i =newint[35];
theError =newfloat[no_output];
layer_h =newfloat[no_hidden];
layer_o =newfloat[no_output];
weight_h =newfloat[35][no_hidden];
weight_o =newfloat[no_hidden][no_output];
}
public String createFileName(char choice)
{
theFileName ="F:\\Semester 2\\ANN\\tr-data\\data 2\\" +choice + ".sv";
return theFileName;
}
publicvoid 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());
}
}
publicvoid 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();
}
}
privateint changeType(char choice)
{
int reply = (int) choice - 65;
return reply;
}
publicvoid 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];
}
}
}
}
publicvoid 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();
}
}
publicvoid 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;
}
}
}
publicstaticvoid 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 training
int 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

