Files
incubator-mxnet/cpp-package/include/mxnet-cpp/optimizer.h
T
Chris Olivier a5abd79c0b Caffe without the patch, cpp-package fixed also with Caffe plugin (#5573)
* PEP8 indentation fix

* Remove executable flag

* Remove executable flags

* Fixing cpp-package including problems with caffe converter

* Fix warnings

* Remove need for caffe patch for caffe plugin

* Ignore cmake paths, remove rebuildable op.h for cpp-package

* cpp-package examples fixed. Makefile and CMake of op.h and examples added.

* cpp-package examples fixed. Makefile and CMake of op.h and examples added.

* Fix source file

* Better example.mk

* Turn off caffe by default

* lint fixes

* Trying to figure out how to fix submodules

* nohub problem on travis so force retry

* Edited test to just run make instead of obsolete make example

* Fix cpp-package op.h bootstrapping with CMake

* Build tweaking cpp-package

* Lint fixes

* op.h generator

* FIx cpp-package for latest merge from master

* Fix lint

* static link

* link whole static lib

* Trigger another build attempt

* Trigger another build attempt

* Add caffe plugin support (disabled until dependencies can be added to build machine)

* Add cufft library

* win32 cufft library

* don't link unit tests as static

* Add include for rebuildable protobuf header caffe.pb.h
2017-03-30 20:13:36 -07:00

123 lines
3.2 KiB
C++

/*!
* Copyright (c) 2016 by Contributors
* \file optimizer.h
* \brief definition of optimizer
* \author Chuntao Hong, Zhang Chen
*/
#ifndef CPP_PACKAGE_INCLUDE_MXNET_CPP_OPTIMIZER_H_
#define CPP_PACKAGE_INCLUDE_MXNET_CPP_OPTIMIZER_H_
#include <map>
#include <vector>
#include <string>
#include <memory>
#include <functional>
#include "mxnet-cpp/base.h"
#include "dmlc/logging.h"
#include "mxnet-cpp/ndarray.h"
#include "mxnet-cpp/op_map.h"
namespace mxnet {
namespace cpp {
/*!
* \brief Optimizer interface
*/
class Optimizer {
public:
/*!
* \brief get optimizer type
* \return string of optimizer type
*/
virtual std::string GetType() const = 0;
/*!
* \brief destructor
*/
virtual ~Optimizer();
/*!
* \brief set config parameters
* \param name name of the config parameter
* \param value value of the config parameter
* \return reference of self
*/
template <typename T>
Optimizer *SetParam(const std::string &name, const T &value) {
std::string value_str;
std::stringstream ss;
ss << value;
ss >> value_str;
params_[name] = value_str;
return this;
}
/*!
* \brief Update a weight with gradient.
* \param index the unique index for the weight.
* \param weight the weight to update.
* \param grad gradient for the weight.
* \param lr learning rate.
* \param wd weight decay.
*/
void Update(int index, NDArray weight, NDArray grad, mx_float lr,
mx_float wd);
/*!
* \brief Update a weight with gradient.
* \param index the unique index for the weight.
* \param weight the weight to update.
* \param grad gradient for the weight.
*/
virtual void Update(int index, NDArray weight, NDArray grad) = 0;
// TODO(zhangcheng-qinyinghua)
// implement Update a list of arrays, maybe in the form of map
// void Update(int index, std::vector<NDArray> weights, std::vector<NDArray>
// grad, mx_float lr);
/*!
* \brief Serialize the optimizer parameters to a string.
* \return serialization
*/
std::string Serialize() const;
protected:
std::map<std::string, std::string> params_;
static OpMap*& op_map();
const std::vector<const char*> GetParamKeys_() const;
const std::vector<const char*> GetParamValues_() const;
};
typedef std::function<Optimizer*()> OptimizerCreator;
class OptimizerRegistry {
public:
static Optimizer* Find(const std::string& name);
static int __REGISTER__(const std::string& name, OptimizerCreator creator);
private:
static std::map<std::string, OptimizerCreator>& cmap();
OptimizerRegistry() = delete;
~OptimizerRegistry() = delete;
};
#define MXNETCPP_REGISTER_OPTIMIZER(Name, OptimizerType) \
static int __make_ ## OptimizerType ## _ ## Name ## __ = \
OptimizerRegistry::__REGISTER__(#Name, [](){return new OptimizerType();})
class SGDOptimizer : public Optimizer {
public:
SGDOptimizer();
virtual std::string GetType() const;
virtual void Update(int index, NDArray weight, NDArray grad);
private:
virtual ~SGDOptimizer();
virtual void CreateState_(int index, NDArray weight);
std::map<int, NDArray*> states_;
AtomicSymbolCreator update_handle_;
AtomicSymbolCreator mom_update_handle_;
};
} // namespace cpp
} // namespace mxnet
#endif // CPP_PACKAGE_INCLUDE_MXNET_CPP_OPTIMIZER_H_