jeffheaton's picture

    This chapter presents several classes that can be used to create and manipulate matrixes. These matrix classes will be used throughout this book. These classes are summarized in Table 2.1.

Table 2.1: Matrix Classes

Class Purpose
BiPolarUtil A utility class to convert between Boolean and bipolar numbers.
Matrix Holds a matrix.
MatrixMath Performs mathematical operations on a matrix.

    The next three sections will examine each of these classes.

The BiPolarUtil Class

    The BiPolarUtil class is used to switch between a bipolar number and a bool value. A bool value is either true or false. A bipolar number is either 1 or -1. Using this class, the bool value of false is expressed as a -1 bipolar value and the bool value of true is expressed as a 1 bipolar value. The BiPolarUtil class is a collection of static methods. The signatures for these methods are shown here.

public static double Bipolar2double(bool b) 
public static double[] Bipolar2double(bool[] b) 
public static double[,] Bipolar2double(bool[,] b) 
public static bool Double2bipolar(double d) 
public static bool[] Double2bipolar(double[] d) 
public static bool[,] Double2bipolar(double[,] d) 
public static double NormalizeBinary(double d) 
public static double ToBinary(double d) 
public static double ToBiPolar(double d) 
public static double ToNormalizedBinary(double d) 

    Table 2.2 summarizes the functions provided by the BiPolarUtil class.

Table 2.2: The BiPolarUtil Class

Method Purpose
Bipolar2double Converts a Boolean bipolar to a double. For example, true is converted to 1.
Double2bipolar Converts a double value to a bipolar Boolean. For example, -1 is converted to false.

    The above two methods are overloaded, so you can convert a single value, a single dimensional array, or a two dimensional array. Bipolar values are particularly useful for Hopfield neural networks. Hopfield neural networks will be discussed in the next chapter.

The Matrix Class

    The Matrix class is used to construct two dimensional matrixes. The values contained in the matrixes are stored as C# double variables. The Matrix class provides the fundamental operations of numerical linear algebra. For operations involving two or more matrixes, the MatrixMath class is used. The MatrixMath class is discussed in the next section.

    The signatures for the Matrix class methods are shown here.

public double[] ToPackedArray() 
public static Matrix CreateColumnMatrix(double[] input) public static Matrix CreateRowMatrix(double[] input) 
public void Add(int row, int col, double value) 
public void Clear() public Matrix Clone() 
public bool Equals(Matrix matrix) 
public bool Equals(Matrix matrix, int precision) 
public int FromPackedArray(double[] array, int index) 
public Matrix GetCol(int col) 
public Matrix GetRow(int row) public bool IsVector() 
public bool IsZero() 
public void Ramdomize(double min, double max) 
public double Sum() 

    The methods provided by the Matrix class are summarized in Table 2.3.

Table 2.3: Methods of the Matrix Class

Method Purpose
CreateColumnMatrix Static method which creates a matrix with a single column.
CreateRowMatrix Static method which creates a matrix with a single row.
Add Adds the specified value to every cell in the matrix.
Clear Sets every cell in a matrix to zero.
Clone Creates an exact copy of a matrix.
Equals Determines if two matrixes are equal to each other.
GetCol Gets one column of a matrix object as a new matrix object.
GetRow Gets one row of a matrix object as a new matrix object.
IsVector Determines if a matrix is a vector. A vector matrix has either a single row or a single column.
IsZero Determines if every cell in a matrix object is zero.
Set Sets the value of a cell.
Sum Returns the sum of every cell in a matrix object.
ToPackedArray Converts a two dimensional matrix array into a one dimensional array of C# double variables.

    The Matrix class also contains a number of properties. The signatures for these properties are shown here.

public double this[int row, int col] public int Cols 
public int Rows
public int Size 

    These properties are summarized in Table 2.4.

Table 2.4: Methods of the Matrix Class

Method Purpose
Cols Get the number of columns in the matrix.
Rows Get the number of rows in the matrix.
Size Get the number of cells in the matrix.
This Allows index access to the matrix, for example matrix[3,2].

    The Matrix class will be used to construct the weight matrixes for all neural networks presented in this book.

The MatrixMath Class

    Most mathematical operations on a Matrix class are accomplished using the MatrixMath class. All methods in the MatrixMath class are static. Further, they always return a new matrix and do not modify the matrixes passed to them. The signatures for the MatrixMath methods are shown here.

public static Matrix Add(Matrix a, Matrix b) 
public static void Copy(Matrix source, Matrix target) 
public static Matrix DeleteCol(Matrix matrix, int deleted) 
public static Matrix DeleteRow(Matrix matrix, int deleted) 
public static Matrix Divide(Matrix a, double b) 
public static double DotProduct(Matrix a, Matrix b) 
public static Matrix Identity(int size) 
public static Matrix Multiply(Matrix a, double b) 
public static Matrix Multiply(Matrix a, Matrix b) 
public static Matrix Subtract(Matrix a, Matrix b) 
public static Matrix Transpose(Matrix input) 
public static double VectorLength(Matrix input) 

    These methods are summarized in Table 2.5.

Table 2.5: The MatrixMath Class

Method Purpose
Add Adds two matrixes and produces a third matrix.
Divide Divides one matrix by a scalar and produces a second matrix.
DotProduct Calculates the dot product of a matrix.
Identity Creates an identity matrix of a specified size.
Multiply Multiplies one matrix by another and produces a third matrix.
Subtract Subtracts one matrix from another and produces a third matrix.
Transpose Transposes a matrix and produces a new matrix.
VectorLength Calculates the squared length of a vector.

    These are the primary mathematical operations that neural networks need to perform. Each of these operations will be discussed at length later in this chapter.

    Many C# neural network implementations build matrix operations directly into their neural network classes. The result being many nested for loops inside the neural network class. For example, the following code allows a neural network to learn.

public void Learn(double learnRate, double momentum) {
		
  if (layer.HasMatrix() ) {
    for (int i1 = 0; i1 < layer.NeuronCount; i1++) {
      for (int i2 = 0; i2 < layer.Next.NeuronCount; i2++) {
matrixDelta[i1][i2] = (learnRate * accMatrixDelta[i1][i2])
+ (momentum * matrixDelta[i1][i2]);
        layer.getMatrix().setMatrix(i1,i2,layer.getMatrix().getMatrix(i1,i2) + matrixDelta[i1][i2]);
        accMatrixDelta[i1][i2] = 0;
      }
    }
  }
}

    The above code performs several matrix operations; however, it is not completely obvious which matrix operations are being performed. By encapsulating the matrix operations inside several matrix classes, the above code can be simplified. Further, you will be able to tell, at a glance, which matrix operations are being performed. The following code accomplishes the same as the code above; however, it uses matrix classes.

public void Learn(double learnRate, double momentum)
{
  if (this.layer.HasMatrix()) {
			
    Matrix m1 = MatrixMath.Multiply( accMatrixDelta, learnRate );
    Matrix m2 = MatrixMath.Multiply( matrixDelta, momentum);
    matrixDelta = MatrixMath.Add(m1, m2);
    layer.setMatrix(MatrixMath.Add(layer.getMatrix(),matrixDelta));
    accMatrixDelta.Clear();
  }
}

    As you can see, several matrixes are constructed and then the MatrixMath class is used to perform operations upon them.


Copyright 2005 - 2012 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright, license and trademark information.