Skip to content

Commit

Permalink
added base classes for gradient update
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkerlin committed Jul 30, 2015
1 parent 80f84c9 commit 1d824bb
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/shogun/optimization/DescendUpdater.h
@@ -0,0 +1,66 @@
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2015 Wu Lin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*
*/

#ifndef CDESCENDUPDATER_H
#define CDESCENDUPDATER_H
#include <shogun/lib/config.h>
#include <shogun/lib/SGVector.h>
#include <shogun/optimization/MinimizerContext.h>
namespace shogun
{
/** @brief This is a base class for descend update.
*
* the base class do the following job:
* Given variable_reference and its negative_descend_direction
* the update_variable function will do
* variable_reference-=update_variable(negative_descend_direction)
*
* Note that an example of a negative_descend_direction is gradient
*
*/
class CDescendUpdater
{
public:
/**update the variable_reference based on the negative_descend_direction
*
* @param variable_reference a reference of the target variable
* @param negative_descend_direction the negative descend direction given the current variable
*/
virtual void update_variable(SGVector<float64_t> variable_reference,
SGVector<float64_t> negative_descend_direction)=0;

virtual void update_context(CMinimizerContext* context)=0;

virtual void load_from_context(CMinimizerContext* context)=0;
};

}
#endif
61 changes: 61 additions & 0 deletions src/shogun/optimization/LearningRate.h
@@ -0,0 +1,61 @@
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2015 Wu Lin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*
*/

#ifndef LEARNINGRATE_H
#define LEARNINGRATE_H
#include <shogun/optimization/MinimizerContext.h>
namespace shogun
{
/** @brief The base class about learning rate for a descent-based minimizer.
*
* This is an interface class used in descent based minimizers.
*
*/
class CLearningRate
{
public:

/** get the learning rate for descent direction
* Note that the learning rate usually is positive
*
* @return the learning rate (A.K.A step size/length)
*/
virtual float64_t get_learning_rate()=0;

virtual void update_context(CMinimizerContext* context)=0;

virtual void load_from_context(CMinimizerContext* context)=0;
};

}

#endif
65 changes: 65 additions & 0 deletions src/shogun/optimization/MinimizerContext.h
@@ -0,0 +1,65 @@
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2015 Wu Lin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*
*/

#ifndef MINIMIZERCONTEXT_H
#define MINIMIZERCONTEXT_H
#include <shogun/lib/config.h>
#include <shogun/lib/SGVector.h>
#include <shogun/base/Parameter.h>
#include <shogun/base/SGObject.h>
namespace shogun
{
class CMinimizerContext: public CSGObject
{
public:
CMinimizerContext()
:CSGObject()
{
init();
}

virtual const char* get_name() const {return "MinimizerContext";}

SGVector<float64_t> m_corrected_direction;
int32_t m_learning_rate_count;
private:
void init()
{
m_learning_rate_count=0;
m_corrected_direction=SGVector<float64_t>();
SG_ADD(&m_corrected_direction, "", "", MS_NOT_AVAILABLE);
SG_ADD(&m_learning_rate_count, "", "", MS_NOT_AVAILABLE);
}

};
}

#endif

0 comments on commit 1d824bb

Please sign in to comment.