SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2013 Roman Votyakov 00008 * Copyright (C) 2012 Jacob Walker 00009 */ 00010 00011 #include <shogun/evaluation/GradientEvaluation.h> 00012 #include <shogun/evaluation/GradientResult.h> 00013 00014 using namespace shogun; 00015 00016 CGradientEvaluation::CGradientEvaluation() : CMachineEvaluation() 00017 { 00018 init(); 00019 } 00020 00021 CGradientEvaluation::CGradientEvaluation(CMachine* machine, CFeatures* features, 00022 CLabels* labels, CEvaluation* evaluation_crit, bool autolock) : 00023 CMachineEvaluation(machine, features, labels, NULL, evaluation_crit, autolock) 00024 { 00025 init(); 00026 } 00027 00028 void CGradientEvaluation::init() 00029 { 00030 m_diff=NULL; 00031 m_parameter_dictionary=NULL; 00032 00033 SG_ADD((CSGObject**)&m_diff, "differentiable_function", "Differentiable " 00034 "function", MS_AVAILABLE); 00035 } 00036 00037 CGradientEvaluation::~CGradientEvaluation() 00038 { 00039 SG_UNREF(m_diff); 00040 SG_UNREF(m_parameter_dictionary); 00041 } 00042 00043 void CGradientEvaluation::update_parameter_dictionary() 00044 { 00045 SG_UNREF(m_parameter_dictionary); 00046 00047 m_parameter_dictionary=new CMap<TParameter*, CSGObject*>(); 00048 m_diff->build_gradient_parameter_dictionary(m_parameter_dictionary); 00049 SG_REF(m_parameter_dictionary); 00050 } 00051 00052 CEvaluationResult* CGradientEvaluation::evaluate() 00053 { 00054 if (update_parameter_hash()) 00055 update_parameter_dictionary(); 00056 00057 // create gradient result object 00058 CGradientResult* result=new CGradientResult(); 00059 SG_REF(result); 00060 00061 // set function value 00062 result->set_value(m_diff->get_value()); 00063 00064 CMap<TParameter*, SGVector<float64_t> >* gradient=m_diff->get_gradient( 00065 m_parameter_dictionary); 00066 00067 // set gradient and parameter dictionary 00068 result->set_gradient(gradient); 00069 result->set_paramter_dictionary(m_parameter_dictionary); 00070 00071 SG_UNREF(gradient); 00072 00073 return result; 00074 }