Primitive Neural Network array problem

edited December 2016 in Questions about Code

Hello all. I am currently building a primitive neural network that reads an input of five letter English words and uses the data it gathers from them to produce words that could be English. I am having a problem where, in trying to assign percentage values to a 2D array, the program freezes and times out. Any input would be appreciated. Problem occurs on line 170 . Thanks.

String input = "aa";
String output = "aa";
//===============================
final int CRETVAL = 1000;
int ax = 400;
int by = 20;
int cx = CRETVAL;
int dy = 20;
int size = 22;
int spacing = 25;
//===============================
int numLetters = 26;
int numLetPerWord = 5;
//===============================
int baseval =0;
//===============================
//String all[] = {""};
String testing[] = new String[8560];
int testLength;
//===============================
int iter = 0;
int[][] Grid = new int[27][27];
float[][] percentGrid = new float[27][27];
float[] percents = new float[26];
//===============================
String interim = "";
char plholder1 = 'a';
char plholder2 = 'a';
int arx, ary;
//===============================

void setup() {
  frameRate(240);
  background(0);
  size(700, 700);
  strokeWeight(4);
  stroke(255);
  line(width/2, 0, width/2, height);
  String all5Letters[]  = loadStrings("5letterwords.txt");
  //String Test[]  = loadStrings("5lettertest.txt");
  arrayCopy(all5Letters, testing);
  // arrayCopy(Test, testing);
  //println("there are " + all5Letters.length + " lines");
  //println("there are " + Test.length + " lines in the test. They are as follows:");
  //println(Test);
  testLength= all5Letters.length;
  fill(255, 255, 0);
  stroke(0, 200);
  println("Length = " +testLength);
  for (int i = 0; i<27; i++) {
    for (int j = 0; j<27; j++) {
      Grid[i][j] = baseval;
      //printArray(Grid[i][j]);
      //baseval ++;
    }
  }

  initGridVal();


  percentGrid = calculatePercents(Grid);
} // end setup


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void draw() {
  textSize(50);
  background(0);
  strokeWeight(4);
  stroke(255);
  line(width/2, 0, width/2, height);

  input = testing[iter];
  output = testing[iter];
  pushMatrix();
  textAlign(LEFT);
  text(input, 100, 60);
  textAlign(RIGHT);
  text(output, width-100, 60);
  popMatrix();
  if ( iter<testLength-1) {
    iter++;
  }
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//|GRID_INITIALIZATION|GRID_INITIALIZATION|GRID_INITIALIZATION|GRID_INITIALIZATION|
public void initGridVal() {
  for (int i = 0; i<testLength; i++) {
    interim = testing[i];
    for (int j = 0; j<numLetPerWord-1; j++) {
      plholder1 =  interim.charAt(j);
      plholder2 =  interim.charAt(j+1);
      arx = xArrayVal(plholder1);
      ary = yArrayVal(plholder2);
      Grid[arx][ary] ++;
    }
  }
  for (int i = 0; i<26; i++) {
    for (int j = 0; j<26; j++) {
      printArray(Grid[i][j]);
      //baseval ++;
    }
  }
}
//|GRID_INITIALIZATION|GRID_INITIALIZATION|GRID_INITIALIZATION|GRID_INITIALIZATION|

//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public int xArrayVal(char in1) { //first letter
  //  int retX = Character.getNumericValue(in1)-36;
  int retX = (int)in1-97;
  return retX;
}

public int yArrayVal(char in2) { // second letter
  // int retY = Character.getNumericValue(in2)-36;
  int retY = (int)in2-97;
  return retY;
}
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

/*
                                                i

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 1                                                                         
 2                                                                          
 3                                                                          
 4                                                                          
 5                                                                            
 6
 7
 8
 9
 10
 11
 12
 j     13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 */

//% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
public float[][]calculatePercents(int[][] importGrid) {
  float[][] ArrPercents = new float[27][27];
  int[] totalPerRow = new int[27]; 

  for (int k = 0; k<27; k++) {    
    for (int l = 0; l<27; l++) {
      totalPerRow[k] += importGrid[k][l];
    }
  }
  printArray(totalPerRow);

  for (int I = 0; I<27; I++) {    
    for (int J = 0; J<27; J++) {

      ArrPercents[I][J] = (Grid[I][J]) /(totalPerRow[I]);
    }
  }
  print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
  printArray(ArrPercents);

  return ArrPercents;
}
//% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 


//+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+
public String returnWeightedLetters(float[] percentArray) {
  String weightedLetters = "";
  int percent = 0;
  char added = 'a';
  //for(int i = 0; i< percentArray.length; i++){
  for (int i = 0; i< 26; i++) {
    percent = int(100*percentArray[i]);
    for (int j = 0; j<percent; j++) {
      weightedLetters = weightedLetters + added;
    }
    added++;
  }

  return weightedLetters;
}


public String chooseNextLetter(String weightedInputLetters) {
  String returnLetter= "";
  int x = int(random(0, weightedInputLetters.length()));
  returnLetter = returnLetter + weightedInputLetters.charAt(x);
  return returnLetter;
}
//+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+C+


public String splice(String a, String b) {
  String result = "";

  return result;
}

Answers

  • Also, after changing all the lengths and widths of the arrays to 26, the program prints the ++++++ (on line 173) but does not print the array of percents, only the string of letters "[[F@7e4a77d4". What does this mean?

  • edited December 2016 Answer ✓

    First: Good job in realizing the error in accessing elements at index 27. If your array has 27 elements, their indexes are 0 to 26. Accessing index 27 is not right!

    I don't know what you thing printArray() does, but it's probably not printing the array how you want. Maybe you should try writing your own code to print an array?

  • edited December 2016 Answer ✓

    printArray() can only print 1D arrays:
    https://Processing.org/reference/printArray_.html

    Try something like this instead: for (float[] oneDim : ArrPercents) printArray(oneDim);

    [[F@7e4a77d4 means a 2D array :[[ of datatype float: F, at: @ memory address: 7e4a77d4. :-B

  • Thanks for the help. Will try (although it is strange that it allowed me to print a 2d array of type int with no problem).

  • edited December 2016 Answer ✓

    I believe that may be because the print() will mostly print an array (1D) of type int without problem, yet won't always work like that with a float array (1D).

  • Answer ✓

    @Trilobyte===

    looking to your code and guessing that you want to get the weights for letters i think that your calculatepercents() cannot return anything till you cast the result to float:

    ArrPercents[I][J] = float(Grid[I][J]) /(totalPerRow[I]);

  • thanks. I happened to fix this prior, since I realized I have a bad habit of not casting. Thanks anyways, though.

Sign In or Register to comment.