/// <summary> /// Train the neural network for the specified pattern. The neural network /// can be trained for more than one pattern. To do this simply call the /// train method more than once. /// </summary> /// <param name="pattern">The pattern to train on.</param> public void Train(bool[] pattern) { if (pattern.Length != this.weightMatrix.Rows) { throw new NeuralNetworkError("Can't train a pattern of size " + pattern.Length + " on a hopfield network of size " + this.weightMatrix.Rows); } // Create a row matrix from the input, convert boolean to bipolar Matrix.Matrix m2 = Matrix.Matrix.CreateRowMatrix(BiPolarUtil .Bipolar2double(pattern)); // Transpose the matrix and multiply by the original input matrix Matrix.Matrix m1 = MatrixMath.Transpose(m2); Matrix.Matrix m3 = MatrixMath.Multiply(m1, m2); // matrix 3 should be square by now, so create an identity // matrix of the same size. Matrix.Matrix identity = MatrixMath.Identity(m3.Rows); // subtract the identity matrix Matrix.Matrix m4 = MatrixMath.Subtract(m3, identity); // now add the calculated matrix, for this pattern, to the // existing weight matrix. this.weightMatrix = MatrixMath.Add(this.weightMatrix, m4); }
/// <summary> /// Present a pattern to the neural network and receive the result. /// </summary> /// <param name="pattern">The pattern to be presented to the neural network.</param> /// <returns>The output from the neural network.</returns> public bool[] Present(bool[] pattern) { bool[] output = new bool[pattern.Length]; // convert the input pattern into a matrix with a single row. // also convert the boolean values to bipolar(-1=false, 1=true) Matrix.Matrix inputMatrix = Matrix.Matrix.CreateRowMatrix(BiPolarUtil .Bipolar2double(pattern)); // Process each value in the pattern for (int col = 0; col < pattern.Length; col++) { Matrix.Matrix columnMatrix = this.weightMatrix.GetCol(col); columnMatrix = MatrixMath.Transpose(columnMatrix); // The output for this input element is the dot product of the // input matrix and one column from the weight matrix. double dotProduct = MatrixMath.DotProduct(inputMatrix, columnMatrix); // Convert the dot product to either true or false. if (dotProduct > 0) { output[col] = true; } else { output[col] = false; } } return(output); }
public bool[] Present(bool[] pattern) { var output = new bool[pattern.Length]; Matrix inputMatrix = Matrix.CreateRowMatrix(BiPolarUtil.Bipolar2Double(pattern)); for (int col = 0; col < pattern.Length; col++) { Matrix columnMatrix = _weightMatrix.GetCol(col); columnMatrix = MatrixMath.Transpose(columnMatrix); double dotProduct = MatrixMath.FindMult(inputMatrix, columnMatrix); if (dotProduct > 0) { output[col] = true; } else { output[col] = false; } } return(output); }
/// <summary> /// Allowes indexed access to the data. /// </summary> /// <param name="x">The index.</param> /// <returns>The value at the specified index.</returns> public double this[int x] { get { return(BiPolarUtil.Bipolar2double(this.data[x])); } set { this.data[x] = BiPolarUtil.Double2bipolar(value); } }
public void Train(bool[] pattern) { Matrix m2 = Matrix.CreateRowMatrix(BiPolarUtil.Bipolar2Double(pattern)); Matrix m1 = MatrixMath.Transpose(m2); Matrix m3 = MatrixMath.Multiply(m1, m2); Matrix identity = MatrixMath.CreateIdentityMatrix(m3.Rows); Matrix m4 = MatrixMath.Subtract(m3, identity); _weightMatrix = MatrixMath.Add(_weightMatrix, m4); }
/// <summary> /// Note: for Hopfield networks, you will usually want to call the "run" /// method to compute the output. /// This method can be used to copy the input data to the current state. A /// single iteration is then run, and the new current state is returned. /// </summary> /// /// <param name="input">The input pattern.</param> /// <returns>The new current state.</returns> public override sealed IMLData Compute(IMLData input) { var result = new BiPolarMLData(input.Count); input.CopyTo(CurrentState.Data, 0, input.Count); Run(); for (int i = 0; i < CurrentState.Count; i++) { result.SetBoolean(i, BiPolarUtil.Double2bipolar(CurrentState[i])); } EngineArray.ArrayCopy(CurrentState.Data, result.Data); return(result); }