import collections

from . import base


__all__ = ['Adam']


class Adam(base.Optimizer):
    """
    Example
    -------

        #!python
        >>> import creme
        >>> from sklearn import datasets
        >>> from sklearn import metrics

        >>> X_y = creme.stream.iter_sklearn_dataset(
        ...     load_dataset=datasets.load_breast_cancer,
        ...     shuffle=True,
        ...     random_state=42
        ... )
        >>> optimiser = creme.optim.Adam()
        >>> model = creme.pipeline.Pipeline([
        ...     ('scale', creme.preprocessing.StandardScaler()),
        ...     ('learn', creme.linear_model.LogisticRegression(optimiser))
        ... ])
        >>> metric = metrics.roc_auc_score

        >>> creme.model_selection.online_score(X_y, model, metric)
        0.984217...

    """

    def __init__(self, lr=0.1, beta_1=0.9, beta_2=0.999, eps=1e-8):
        super().__init__(lr)
        self.beta_1 = beta_1
        self.beta_2 = beta_2
        self.eps = eps
        self.m = collections.defaultdict(lambda: 0.)
        self.v = collections.defaultdict(lambda: 0.)

    def update_weights_with_gradient(self, w, g):

        for i, gi in g.items():
            self.m[i] = self.beta_1 * self.m[i] + (1 - self.beta_1) * gi
            self.v[i] = self.beta_2 * self.v[i] + (1 - self.beta_2) * gi ** 2
            m = self.m[i] / (1 - self.beta_1 ** (self.t + 1))
            v = self.v[i] / (1 - self.beta_2 ** (self.t + 1))
            w[i] -= self.learning_rate * m / (v ** 0.5 + self.eps)

        return w
