DataSharp/Data/Analysis/SupportVectorMachine.cs
/// <summary>
/// Returns the index of the 2nd lagrange multiplier to optimize in an iteration.
/// </summary>
/// <param name="i">The index of the 1st lagrange multiplier to optimize.</param>
/// <param name="errorI">Error on i-th sample with current parameters.</param>
/// <param name="errorJ">Error on j-th sample with current parameters.</param>
/// <returns>the index of the 2nd lagrange multiplier to optimize in an iteration.</returns>
Returns the index of the 2nd lagrange multiplier to optimize in an iteration.
Returns the index of the 2nd lagrange multiplier to optimize in an iteration .
private int Select2ndMultiplier(int i, double errorI, out double errorJ)
        {
            int multiplierIndex = -1;
            double maxDeltaError = 0;
            errorJ = 0;
            errorCache[i, 0] = 1;
            errorCache[i, 1] = errorI;
            LinkedList<int> validErrorCacheIndices = GetNonZeroIndices();
            if (validErrorCacheIndices.Count > 1)
            {
                foreach (int k in validErrorCacheIndices)
                {
                    if (k == i)
                        continue;
                    double errorK = CalculateError(k);
                    double deltaError = Math.Abs(errorI - errorK);
                    if (deltaError > maxDeltaError)
                    {
                        maxDeltaError = deltaError;
                        multiplierIndex = k;
                        errorJ = errorK;
                    }
                }
                if (multiplierIndex != -1)
                    return multiplierIndex;
                else
                {
                    multiplierIndex = Select2ndMultiplierRandom(i);
                    errorJ = CalculateError(multiplierIndex);
                    return alphas.Count - 1;
                }
            }
            else
            {
                multiplierIndex = Select2ndMultiplierRandom(i);
                errorJ = CalculateError(multiplierIndex);
                return multiplierIndex;
            }
        }
private int Select2ndMultiplier ( int i , double errorI , out double errorJ ) { int multiplierIndex = - 1 ; double maxDeltaError = 0 ; errorJ = 0 ; errorCache [ i , 0 ] = 1 ; errorCache [ i , 1 ] = errorI ; LinkedList < int > validErrorCacheIndices = GetNonZeroIndices ( ) ; if ( validErrorCacheIndices . Count > 1 ) { foreach ( int k in validErrorCacheIndices ) { if ( k == i ) continue ; double errorK = CalculateError ( k ) ; double deltaError = Math . Abs ( errorI - errorK ) ; if ( deltaError > maxDeltaError ) { maxDeltaError = deltaError ; multiplierIndex = k ; errorJ = errorK ; } } if ( multiplierIndex != - 1 ) return multiplierIndex ; else { multiplierIndex = Select2ndMultiplierRandom ( i ) ; errorJ = CalculateError ( multiplierIndex ) ; return alphas . Count - 1 ; } } else { multiplierIndex = Select2ndMultiplierRandom ( i ) ; errorJ = CalculateError ( multiplierIndex ) ; return multiplierIndex ; } }
Returns the index of the 2nd lagrange multiplier to optimize in an iteration.
Returns the index of the 2nd lagrange multiplier to optimize in an iteration .
// Initialize the index and errors. // Update error cache. // Get the indices of lagrange multipliers which is valid. // Try to find another lagrange multiplier to optimize if the number of valid lagrange multipliers is larger than 1. // Otherwise, select a random lagrange multiplier different from i to optimize. // Tranverse through all valid indices. // The indices of 2 lagrange multipliers to optimize must be un-equal. // Calculate error and delta-error on k-th sample. // Keep tracking the potential lagrange multiplier with maximum delta error. // Return the index of the 2nd lagrange multiplier if find one. // Otherwise, select a random lagrange multiplier different from i to optimize. //return multiplierIndex;
the index of the 2nd lagrange multiplier to optimize in an iteration.
the index of the 2nd lagrange multiplier to optimize in an iteration .
i
int
The index of the 1st lagrange multiplier to optimize.
The index of the 1st lagrange multiplier to optimize .
errorI
double
Error on i-th sample with current parameters.
Error on i - th sample with current parameters .
double
out
errorJ
Error on j-th sample with current parameters.
Error on j - th sample with current parameters .