Skip to content

declearn.optimizer.modules.EWMAModule

Bases: OptiModule

Exponentially Weighted Moving Average module.

This module impements the following algorithm:

Init(beta):
    state = 0
Step(grads):
    state = beta*state + (1-beta)*grads
    grads = state

In other words, gradients are corrected by an exponentially- decaying moving-average of past gradients.

Source code in declearn/optimizer/modules/_momentum.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
class EWMAModule(OptiModule):
    """Exponentially Weighted Moving Average module.

    This module impements the following algorithm:

        Init(beta):
            state = 0
        Step(grads):
            state = beta*state + (1-beta)*grads
            grads = state

    In other words, gradients are corrected by an exponentially-
    decaying moving-average of past gradients.
    """

    name: ClassVar[str] = "ewma"

    def __init__(
        self,
        beta: float = 0.9,
    ) -> None:
        """Instantiate the EWMA gradients-adaptation module.

        Parameters
        ----------
        beta: float, default=0.9
            Coefficient parameterizing the (exponentially-
            decaying) moving average of input gradients.
        """
        if not isinstance(beta, float):
            raise TypeError("'beta' should be of type float.")
        if not 0 <= beta < 1:
            raise ValueError("'beta' value should be in [0, 1[.")
        self.beta = beta
        self.state = 0.0  # type: Union[Vector, float]

    def get_config(
        self,
    ) -> Dict[str, Any]:
        return {"beta": self.beta}

    def run(
        self,
        gradients: Vector,
    ) -> Vector:
        self.state = (self.beta * self.state) + ((1 - self.beta) * gradients)
        return self.state

    def get_state(
        self,
    ) -> Dict[str, Any]:
        return {"state": self.state}

    def set_state(
        self,
        state: Dict[str, Any],
    ) -> None:
        if "state" not in state:
            raise KeyError("Missing required state variable 'state'.")
        self.state = state["state"]

__init__(beta=0.9)

Instantiate the EWMA gradients-adaptation module.

Parameters:

Name Type Description Default
beta float

Coefficient parameterizing the (exponentially- decaying) moving average of input gradients.

0.9
Source code in declearn/optimizer/modules/_momentum.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def __init__(
    self,
    beta: float = 0.9,
) -> None:
    """Instantiate the EWMA gradients-adaptation module.

    Parameters
    ----------
    beta: float, default=0.9
        Coefficient parameterizing the (exponentially-
        decaying) moving average of input gradients.
    """
    if not isinstance(beta, float):
        raise TypeError("'beta' should be of type float.")
    if not 0 <= beta < 1:
        raise ValueError("'beta' value should be in [0, 1[.")
    self.beta = beta
    self.state = 0.0  # type: Union[Vector, float]