public void Backward(MatrixFloat expected) { //error = (1/2)*(actual-expected)^2 var lastLayer = neuronLayers[neuronLayers.Count - 1]; var deltaErrorOut = 0.1f * (lastLayer.Neurons - expected) * lastLayer.ActivateDeriv(); lastLayer.Gradient = deltaErrorOut; var update = neuronLayers[neuronLayers.Count - 2].Activate().Transpose().Multiply(deltaErrorOut); weightLayers[neuronLayers.Count - 2].Gradient = update; var prevDelta = deltaErrorOut; for (int i = neuronLayers.Count - 2; i >= 1; i--) { var dLayer = prevDelta.Multiply(weightLayers[i].Weights.Transpose()) * neuronLayers[i].ActivateDeriv(); neuronLayers[i].Gradient = dLayer; update = neuronLayers[i - 1].Activate().Transpose().Multiply(dLayer); weightLayers[i - 1].Gradient = update; prevDelta = dLayer; } for (int i = 0; i < weightLayers.Count; i++) { weightLayers[i].Weights -= weightLayers[i].Gradient; } }
public MatrixFloat Apply(Func <float, float> func) { var a = new MatrixFloat(Rows, Columns); for (int i = 0; i < Rows * Columns; i++) { a._array[i] = func(_array[i]); } return(a); }
public MatrixFloat Transpose() { var t = new MatrixFloat(Columns, Rows); for (int row = 0; row < Rows; row++) { for (int column = 0; column < Columns; column++) { t[column, row] = this[row, column]; } } return(t); }
public MatrixFloat Substract(MatrixFloat other) { #if DEBUG if (Columns != other.Columns || Rows != other.Rows) { throw new Exception($"Columns {Columns} does not equal other.Columns {other.Columns} or Rows {Rows} does not equal other.Rows {other.Rows}"); } #endif var a = new MatrixFloat(Rows, Columns); for (int i = 0; i < Rows * Columns; i++) { a._array[i] = _array[i] - other._array[i]; } return(a); }
public MatrixFloat Multiply(MatrixFloat other) { #if DEBUG if (Columns != other.Rows) { throw new Exception($"Columns {Columns} does not equal other.Rows {other.Rows}"); } #endif var result = new MatrixFloat(Rows, other.Columns); for (int cColumn = 0; cColumn < result.Columns; cColumn++) { for (int cRow = 0; cRow < result.Rows; cRow++) { for (int i = 0; i < Columns; i++) { result[cRow, cColumn] += this[cRow, i] * other[i, cColumn]; } } } return(result); }