Platt's binary SVM Probabilistic Output: an improvement from Lin et al.
| 1968 | |
| 1969 | // Platt's binary SVM Probabilistic Output: an improvement from Lin et al. |
| 1970 | static void |
| 1971 | sigmoid_train( |
| 1972 | int l, const double* dec_values, const double* labels, double& A, double& B) |
| 1973 | { |
| 1974 | double prior1 = 0, prior0 = 0; |
| 1975 | |
| 1976 | for (int i = 0; i < l; i++) |
| 1977 | if (labels[i] > 0) |
| 1978 | prior1 += 1; |
| 1979 | else |
| 1980 | prior0 += 1; |
| 1981 | |
| 1982 | const int max_iter = 100; // Maximal number of iterations |
| 1983 | |
| 1984 | const double min_step = 1e-10; // Minimal step taken in line search |
| 1985 | |
| 1986 | const double sigma = 1e-12; // For numerically strict PD of Hessian |
| 1987 | |
| 1988 | const double eps = 1e-5; |
| 1989 | |
| 1990 | const double hiTarget = (prior1 + 1.0) / (prior1 + 2.0); |
| 1991 | |
| 1992 | const double loTarget = 1 / (prior0 + 2.0); |
| 1993 | |
| 1994 | double* t = Malloc(double, l); |
| 1995 | |
| 1996 | // Initial Point and Initial Fun Value |
| 1997 | A = 0.0; |
| 1998 | |
| 1999 | B = std::log((prior0 + 1.0) / (prior1 + 1.0)); |
| 2000 | |
| 2001 | double fval = 0.0; |
| 2002 | |
| 2003 | for (int i = 0; i < l; i++) { |
| 2004 | if (labels[i] > 0) |
| 2005 | t[i] = hiTarget; |
| 2006 | else |
| 2007 | t[i] = loTarget; |
| 2008 | |
| 2009 | double fApB = dec_values[i] * A + B; |
| 2010 | |
| 2011 | if (fApB >= 0) |
| 2012 | fval += t[i] * fApB + std::log(1 + std::exp(-fApB)); |
| 2013 | else |
| 2014 | fval += (t[i] - 1) * fApB + std::log(1 + std::exp(fApB)); |
| 2015 | } |
| 2016 | |
| 2017 | int iter = 0; |
| 2018 | for (; iter < max_iter; iter++) { |
| 2019 | // Update Gradient and Hessian (use H' = H + sigma I) |
| 2020 | double h11 = sigma; // numerically ensures strict PD |
| 2021 | double h22 = sigma; |
| 2022 | double h21 = 0.0; |
| 2023 | double g1 = 0.0; |
| 2024 | double g2 = 0.0; |
| 2025 | |
| 2026 | for (int i = 0; i < l; i++) { |
| 2027 | double fApB = dec_values[i] * A + B; |
no test coverage detected