Coder Social home page Coder Social logo

sophia's Introduction

sophia

Implementation of Sophia: Second-order Clipped Stochastic Optimization

Sophia works by pre-conditioning the loss landscape by the Hessian. By pre-conditioning on the local curvature, Sophia can converges ~2x faster than Adam (on GPT-2).

  • Currently implemented Sophia-H optimizer as proposed in the paper.
  • Will implement Sophia-G later.

Algorithm

  • Compute EMA of gradient (m)
  • Compute diagonal hessian via an Estimator
  • Compute EMA of hessian (h)
  • clip(m/h, rho)
  • update param

The clip is necessary in case of a flat dimension where the hessian is close to 0.

The authors of sophia propose 2 estimators for the diagonal of the hessian: Hutchinson and Gauss-Netwon-Bartlett Hence two optimizers:

  • Sophia-H
  • Sophia-G

Hutchinson Algorithm:

class Hutchinson(Estimator):

    def __init__(self) -> None:
        super().__init__()

    """
    
    Computes the Diagonal of Hessian
    
    :param params - model parameters
    :param loss - model loss
    :param batch - mini batch
    
    """

    def compute(self, params: List[Tensor], loss: Tensor, batch: Tensor = null) -> List[Tensor]:

        u: List[Tensor] = list([torch.randn_like(p) for p in params])

        J: Tuple[Tensor] = torch.autograd.grad(loss, params, create_graph=true)  # compute jacobian

        gradu: List[Tensor] = []  # dot products with gradient and noise

        for i, g in enumerate(J):
            gradu.append((g * u[i]).sum())

        hvp: Tuple[Tensor] = torch.autograd.grad(gradu, params,
                                                 retain_graph=true)  # compute hessian vector product

        hessian: List[Tensor] = []

        for i, grad in enumerate(hvp):
            hessian.append(grad * u[i])
        return hessian

Paper

Sophia Paper

sophia's People

Contributors

vmad18 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.