forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizer.h
100 lines (88 loc) · 2.7 KB
/
optimizer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*!
* Copyright (c) 2015 by Contributors
* \file optimizer.h
* \brief Operator interface of mxnet.
* \author Junyuan Xie
*/
#ifndef MXNET_OPTIMIZER_H_
#define MXNET_OPTIMIZER_H_
#include <dmlc/base.h>
#include <dmlc/logging.h>
#include <dmlc/registry.h>
#include <mshadow/tensor.h>
#include <string>
#include <vector>
#include <utility>
#include "./base.h"
#include "./resource.h"
#if DMLC_USE_CXX11
#include <mxnet/ndarray.h>
#endif
namespace mxnet {
#if !DMLC_USE_CXX11
class NDArray;
#endif
class Optimizer {
public:
/*!
* \brief virtual destructor
*/
virtual ~Optimizer() {}
/*!
* \brief Initialize the Optimizer by setting the parameters
* This function need to be called before all other functions.
* \param kwargs the keyword arguments parameters
*/
virtual void Init(const std::vector<std::pair<std::string, std::string> >& kwargs) = 0;
/*!
* \brief Create aux state for weigth with index
* \param index the unique index for the weight.
* \param weight the NDArray to associate created state to.
*/
virtual void CreateState(const int index, const NDArray *weight) = 0;
/*!
* \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 for this update.
* \param wd weight decay for this update.
*/
virtual void Update(const int index, NDArray *weight,
const NDArray *grad, const float lr, const float wd) = 0;
/*!
* \brief create Optimizer
* \param type_name the type string of the Optimizer
* \return a new constructed Optimizer
*/
static Optimizer *Create(const char* type_name);
};
#if DMLC_USE_CXX11
/*! \brief typedef the factory function of Optimizer */
typedef std::function<Optimizer *()> OptimizerFactory;
/*!
* \brief Registry entry for Optimizer factory functions.
*/
struct OptimizerReg
: public dmlc::FunctionRegEntryBase<OptimizerReg,
OptimizerFactory> {
};
//--------------------------------------------------------------
// The following part are API Registration of Optimizers
//--------------------------------------------------------------
/*!
* \brief Macro to register Optimizer
*
* \code
* // example of registering a SGD optimizer
* MXNET_REGISTER_OPTIMIZER(_SGD, SGDOptimizer)
* .describe("Stochastic Gradient Decent optimizer");
*
* \endcode
*/
#define MXNET_REGISTER_OPTIMIZER(name, OptimizerType) \
DMLC_REGISTRY_REGISTER(::mxnet::OptimizerReg, OptimizerReg, name) \
.set_body([]() { return new OptimizerType(); })
#endif // DMLC_USE_CXX11
} // namespace mxnet
#endif // MXNET_OPTIMIZER_H_