Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 62c64cd
  • /
  • grad_check.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:a564b6e95fd3a46bb7838af43a1df58c2c41d64c
directory badge
swh:1:dir:62c64cda2683cec7cf7848aba8feef132c43deee

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
grad_check.py
import numpy as np
from py_diff_stokes_flow.common.common import print_error, print_ok

# var_filter
def check_gradients(loss_and_grad, x0, eps=1e-6, rtol=1e-2, atol=1e-4, verbose=True,
    loss_only=None, grad_only=None, skip_var=None):
    if grad_only is None:
        _, grad_analytic = loss_and_grad(x0)
    else:
        grad_analytic = grad_only(x0)

    grads_equal = True
    n = x0.size
    for i in range(n):
        if skip_var is not None and skip_var(i): continue
        x_pos = np.copy(x0)
        x_neg = np.copy(x0)
        x_pos[i] += eps
        x_neg[i] -= eps
        if loss_only is None:
            loss_pos, _ = loss_and_grad(x_pos)
            loss_neg, _ = loss_and_grad(x_neg)
        else:
            loss_pos = loss_only(x_pos)
            loss_neg = loss_only(x_neg)
        grad_numeric = (loss_pos - loss_neg) / 2 / eps
        if not np.isclose(grad_analytic[i], grad_numeric, rtol=rtol, atol=atol):
            grads_equal = False
            if verbose:
                print_error('Variable {}: analytic {}, numeric {}'.format(i, grad_analytic[i], grad_numeric))
            else:
                return grads_equal
        elif verbose:
            print_ok('Variable {} seems good: analytic {}, numeric {}'.format(i, grad_analytic[i], grad_numeric))

    return grads_equal

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API