## File: simplify_linear_decision_function.h

package info (click to toggle)
mldemos 0.5.1-3
 `123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110` ``````// Copyright (C) 2010 Davis E. King (davis@dlib.net) // License: Boost Software License See LICENSE.txt for the full license. #ifndef DLIB_SIMPLIFY_LINEAR_DECiSION_FUNCTION_H__ #define DLIB_SIMPLIFY_LINEAR_DECiSION_FUNCTION_H__ #include "simplify_linear_decision_function_abstract.h" #include "../algs.h" #include "function.h" #include "sparse_kernel.h" #include "kernel.h" #include #include namespace dlib { // ---------------------------------------------------------------------------------------- template < typename T > decision_function > simplify_linear_decision_function ( const decision_function >& df ) { // don't do anything if we don't have to if (df.basis_vectors.size() <= 1) return df; decision_function > new_df; new_df.b = df.b; new_df.basis_vectors.set_size(1); new_df.alpha.set_size(1); new_df.alpha(0) = 1; // now compute the weighted sum of all the sparse basis_vectors in df typedef typename T::value_type pair_type; typedef typename pair_type::first_type key_type; typedef typename pair_type::second_type value_type; std::map accum; for (long i = 0; i < df.basis_vectors.size(); ++i) { typename T::const_iterator j = df.basis_vectors(i).begin(); const typename T::const_iterator end = df.basis_vectors(i).end(); for (; j != end; ++j) { accum[j->first] += df.alpha(i) * (j->second); } } new_df.basis_vectors(0) = T(accum.begin(), accum.end()); return new_df; } // ---------------------------------------------------------------------------------------- template < typename T > decision_function > simplify_linear_decision_function ( const decision_function >& df ) { // don't do anything if we don't have to if (df.basis_vectors.size() <= 1) return df; decision_function > new_df; new_df.b = df.b; new_df.basis_vectors.set_size(1); new_df.alpha.set_size(1); new_df.alpha(0) = 1; // now compute the weighted sum of all the basis_vectors in df new_df.basis_vectors(0) = 0; for (long i = 0; i < df.basis_vectors.size(); ++i) { new_df.basis_vectors(0) += df.alpha(i) * df.basis_vectors(i); } return new_df; } // ---------------------------------------------------------------------------------------- template < typename T > decision_function > simplify_linear_decision_function ( const normalized_function >, vector_normalizer >& df ) { decision_function > new_df = simplify_linear_decision_function(df.function); // now incorporate the normalization stuff into new_df new_df.basis_vectors(0) = pointwise_multiply(new_df.basis_vectors(0), df.normalizer.std_devs()); new_df.b += dot(new_df.basis_vectors(0), df.normalizer.means()); return new_df; } // ---------------------------------------------------------------------------------------- } #endif // DLIB_SIMPLIFY_LINEAR_DECiSION_FUNCTION_H__ ``````