Caffe2 - C++ API
A deep learning, cross platform ML framework
adagrad_op.cc
1 
17 #include "adagrad_op.h"
18 
19 namespace caffe2 {
20 
21 REGISTER_CPU_OPERATOR(Adagrad, AdagradOp<float, CPUContext>);
22 OPERATOR_SCHEMA(Adagrad)
23  .NumInputs(4)
24  .NumOutputs(2)
25  .AllowInplace({{0, 0}, {1, 1}})
26  .SetDoc(R"DOC(
27 
28 Computes the AdaGrad update for an input gradient and accumulated
29 history. Concretely, given inputs (param, grad, moment, learning_rate),
30 computes
31 
32  new_moment = moment + square(grad)
33  new_grad = learning_rate * grad / (sqrt(new_moment) + epsilon)
34  new_param = param + new_grad
35 and returns (new_param, new_moment).
36 
37 )DOC")
38  .Input(0, "param", "Parameters to be updated")
39  .Input(1, "moment", "Moment history")
40  .Input(2, "grad", "Gradient computed")
41  .Input(3, "lr", "learning rate")
42  .Output(0, "output_param", "Updated parameters")
43  .Output(1, "output_moment", "Updated moment")
44  .Arg("epsilon", "Default 1e-5")
45  .Arg(
46  "decay",
47  "Default 1. If it is in (0, 1), the gradient square sum "
48  "is decayed by this factor.");
49 
50 REGISTER_CPU_OPERATOR(SparseAdagrad, SparseAdagradOp<float, CPUContext>);
51 OPERATOR_SCHEMA(SparseAdagrad)
52  .NumInputs(5)
53  .NumOutputs(2)
54  .EnforceOneToOneInplace()
55  .SetDoc(R"DOC(
56 
57 Given inputs (param, moment, indices, grad, lr), runs the dense AdaGrad
58 update on (param, grad, moment[indices], lr), and returns (new_param,
59 new_moment) as in the dense case.
60 
61 )DOC")
62  .Input(0, "param", "Parameters to be updated")
63  .Input(1, "moment", "Moment history")
64  .Input(2, "indices", "Sparse indices")
65  .Input(3, "grad", "Gradient computed")
66  .Input(4, "lr", "learning rate")
67  .Output(0, "output_param", "Updated parameters")
68  .Output(1, "output_moment_1", "Updated moment")
69  .Arg("epsilon", "Default 1e-5");
70 
71 REGISTER_CPU_OPERATOR(
72  RowWiseSparseAdagrad,
73  RowWiseSparseAdagradOp<float, CPUContext>);
74 OPERATOR_SCHEMA(RowWiseSparseAdagrad)
75  .NumInputs(5)
76  .NumOutputs(2)
77  .EnforceOneToOneInplace()
78  .SetDoc(R"DOC(
79 
80 Given inputs (param, moment, indices, grad, lr), runs a modified sparse Adagrad
81 update on (param, grad, moment[indices], lr), and returns (new_param,
82 new_momwnr), where moment is a 1D tensor with length equal to the number of
83 rows in param: shape(moment) == shape(param)[0]. Each element of moment is
84 applied to an entire row of param, and the new moment is calculated by adding
85 the average squared sum of gradients across each row. Note that indices must
86 also be a 1D tensor indexing into the rows of param.
87 
88 )DOC")
89  .Input(0, "param", "Parameters to be updated")
90  .Input(1, "moment", "Moment history")
91  .Input(2, "indices", "Sparse indices")
92  .Input(3, "grad", "Gradient computed")
93  .Input(4, "lr", "learning rate")
94  .Output(0, "output_param", "Updated parameters")
95  .Output(1, "output_moment_1", "Updated moment")
96  .Arg("epsilon", "Default 1e-5");
97 
98 SHOULD_NOT_DO_GRADIENT(Adagrad);
99 SHOULD_NOT_DO_GRADIENT(SparseAdagrad);
100 SHOULD_NOT_DO_GRADIENT(RowWiseSparseAdagrad);
101 }
Copyright (c) 2016-present, Facebook, Inc.