Neural Network help

Hi everybody, I built a Neural Network super easy and now try to test it out I discovered that when I have to multiply the Weights * Input (sigmoid = W * I + B) I found different matrices size and so I cant do that and the whole process being skipped, so my final sigmoid result 0.5 on all output!
My current net is Net(4 IN, 16 HID, 4 OUT).
Somebody have more info that can help me?

In case you need here is the class code:

import java.util.Random;

class Net
{
  int input_nodes;
  int hidden_nodes;
  int output_nodes;
  
  Matrix weights_ih;
  Matrix weights_ho;

  Random rand = new Random();

  //Constructor
  Net(int I_node, int H_node, int O_node)
  {
    input_nodes = I_node;
    hidden_nodes = H_node;
    output_nodes = O_node;
    
    weights_ih = new Matrix(H_node, I_node+1);//Add Bias weight
    weights_ho = new Matrix(O_node, H_node+1);//Add Bias weight
    
    weights_ih.randomize(); //-1 and 1
    weights_ho.randomize();     

    //bias_h = new Matrix(hidden_nodes, 1);   
    //bias_o = new Matrix(output_nodes, 1);
    //bias_h.randomize();
    //bias_o.randomize();
  }

//---------------------------------------------------------------------------------------

  float[] feedforward(float[] input_array)
  {
    //Transform input_array into a matrix
    //Matrix tmp = new Matrix();//Used cause there isnt method class
    Matrix inputs = new Matrix();
    Matrix outputs = new Matrix();

    //inputs.fromArray(input_array);    
    inputs = inputs.singleColumnMatrixFromArray(input_array);
    inputs = inputs.addBias();
    
    //-------------Generating the HIDDEN OUTPUTS--------------------
    //Now multiply the 2 matrices, weight_ih * input
    Matrix hidden = new Matrix();            
    hidden = weights_ih; //Clone weights_ih into hidden as temp matrix  
    
    hidden = hidden.multiply(inputs); //Multiply the 2 matrices hidden(weights_ih) * input    
    hidden = hidden.addBias(); //Add biasis before Multiply cause an Matrices incompatibility
    hidden = hidden.activate(); //Finally activation sigmoid :D    
    hidden.output();
    //-------------------------------------------------

    //Generating the OUTPUT'S OUTPUT    
    outputs = weights_ho;    
    outputs = outputs.multiply(hidden);//HERE I GET the error
    outputs = outputs.addBias();
    outputs = outputs.activate();              
    //Send it to the caller
    return outputs.toArray();
  }
}

Here is the Matrix class:

class Matrix
{  
  //local variables
  int rows;
  int columns;
  float[][] matrix;
  
  //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  //constructor
  Matrix(int Rows, int Columns)
  {
    rows = Rows;
    columns = Columns;
    matrix = new float[rows][columns];
  }
  
  //---------------------------------------------------------------------------------------------------------------------------------------------------------  
  //constructor from 2D array
  Matrix(float[][] Matrix)
  {
    matrix = Matrix;
    columns = Matrix.length;
    rows = Matrix[0].length;
  }

  Matrix()
  {
    //matrix = new float[][];
    columns = 0;
    rows = 0;
  }

//---------------------------------------------------------------------------------------------------------------------------------------------------------  
  //return a matrix which is this matrix * parameter matrix (element wise multiplication)
  Matrix multiply(Matrix n)
  {
    Matrix newMatrix = new Matrix(rows, columns);
    if (columns == n.columns && rows == n.rows)
    {
      for (int i = 0; i < rows; i++)
      {
        for (int j = 0; j < columns; j++)
        {
          newMatrix.matrix[i][j] = matrix[i][j] * n.matrix[i][j];
        }
      }
    }
    return newMatrix;
  }
}

All methods works fine except multiply, I guess!

I’m not familiar at all with Neural Nets, but I figure this an implementation problem.

You pointed out that your multiply() does not run because the Matrix n is not equal in dimensions to the ones you set - is this intentional?

Well, yes it is! Infact I think I messing up with something into the creation of various layers!

Any luck? I can’t tell if you solved the problem or identified the problem :grimacing:

Yes, problem solved! Adding bias give me some problem, now it works 100%.
Now The problem is Fitness function :smiley:

1 Like

@Manatee whenever you get a chance, please let us know if we can help you with that function. Best of luck on your project :slight_smile:

Well, I really need an hand on improvement of Fitness function or how can I modelling a right layers level if you are interested!
Actually my evolution with Genetic Alg is really really slow, only after 200+ generation he starts to learn something and Im not sure if It is cause the Fitness function or layers in Neural Net or something else!
If you have some advice or wanna see the code I will post :smiley: