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!