/// <summary> /// This method uses the cross entropy cost function to caculate the losses. /// </summary> /// <param name="AL">The final prediction of the network ranging from 0 to 1.</param> /// <param name="_y">The true label 0 or 1.</param> /// <param name="lambda">The L2 regularization hyper-parameter.</param> /// <param name="theta">Dictionary containing weights and bias'.</param> /// <param name="dims">Number of neurons in each layer of the network.</param> /// <returns>A float value which is the calculated loss as well as its derivative.</returns> public float ComputeCost(MatrixVectors AL, MatrixVectors _y, float lambda, Dictionary <string, MatrixVectors> theta, int[] dims) { if (AL.columns > 1 || _y.columns > 1 || !AL.CompareShape(_y)) { Console.WriteLine("Invalid YShape"); return(0f); } float crossEntropyCost = 0; float regularizedCost = 0; for (int l = 1; l < dims.Length; l++) { regularizedCost += MatrixCalculations.MatrixSummation(MatrixCalculations.Square(theta["W" + l])); } regularizedCost *= lambda / 2; for (int y = 0; y < _y.rows; y++) { float currentAL = AL.MatrixVector[0, y]; float currentY = _y.MatrixVector[0, y]; float currentCost = (float)-(currentY * Math.Log10(currentAL) + (1 - currentY) * Math.Log10(1 - currentAL)); crossEntropyCost += currentCost; } float totalCost = crossEntropyCost + regularizedCost; return(totalCost); }
/// <summary> /// Executes the non-linear ReLu activation function on some linear function Z. /// </summary> /// <param name="Z">The linear function of the weights biases and previous layers activations.</param> /// <returns>A vector containing the non-linear sigmoid activations of the linear function z.</returns> private MatrixVectors Relu(MatrixVectors Z) { MatrixVectors activationsVector = MatrixCalculations.Maximum(Z, 0); return(activationsVector); }