{"id":14989230,"url":"https://github.com/twitter/dict_minimize","last_synced_at":"2025-08-21T05:32:39.116Z","repository":{"id":46691106,"uuid":"284868755","full_name":"twitter/dict_minimize","owner":"twitter","description":"Access scipy optimizers from your favorite deep learning framework.","archived":false,"fork":false,"pushed_at":"2023-06-30T22:19:47.000Z","size":71,"stargazers_count":77,"open_issues_count":4,"forks_count":23,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-04-14T06:36:03.607Z","etag":null,"topics":["scipy","scipy-stack"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twitter.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-04T03:35:37.000Z","updated_at":"2024-03-08T16:53:54.000Z","dependencies_parsed_at":"2024-09-11T20:58:04.918Z","dependency_job_id":"e84f625b-1af0-495d-b993-80893adbd55b","html_url":"https://github.com/twitter/dict_minimize","commit_stats":{"total_commits":23,"total_committers":3,"mean_commits":7.666666666666667,"dds":0.4782608695652174,"last_synced_commit":"a3c022c8bb2153fffb715fe274a852acf9e846a0"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fdict_minimize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fdict_minimize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fdict_minimize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fdict_minimize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twitter","download_url":"https://codeload.github.com/twitter/dict_minimize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230494917,"owners_count":18235046,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["scipy","scipy-stack"],"created_at":"2024-09-24T14:17:54.337Z","updated_at":"2024-12-19T20:07:27.534Z","avatar_url":"https://github.com/twitter.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":".. figure:: https://user-images.githubusercontent.com/28273671/90945755-c2935580-e3db-11ea-9ba9-dbb054834b02.png\n   :alt: Rosenbrock\n\n*************************\nThe Dict Minimize Package\n*************************\n\nAccess ``scipy`` optimizers from your favorite deep learning framework.\n\nInstallation\n============\n\nOnly ``Python\u003e=3.6`` is officially supported, but older versions of Python likely work as well.\n\nThe core package itself can be installed with:\n\n.. code-block:: bash\n\n   pip install dict_minimize\n\nTo also get the dependencies for all the supported frameworks (torch, JAX, tensorflow) in the README install with\n\n.. code-block:: bash\n\n   pip install dict_minimize[framework]\n\nSee the `GitHub \u003chttps://github.com/twitter/dict_minimize\u003e`_, `PyPI \u003chttps://pypi.org/project/dict-minimize/\u003e`_, and `Read the Docs \u003chttps://dict-minimize.readthedocs.io/en/latest/\u003e`_.\n\nExample Usage\n=============\n\nIn these examples we optimize a modified `Rosenbrock \u003chttps://en.wikipedia.org/wiki/Rosenbrock_function\u003e`_ function.\nHowever, the arguments have been split into two chunks and stored as two entries in a dictionary.\nThis is to illustrate how this package optimizes *dictionaries* of (tensor) parameters rather then vectors.\nWe also pass in an extra ``shift`` argument to demonstrate how ``minimize`` allows extra constant arguments to be passed into the objective.\n\nPyTorch\n-------\n\n.. code-block:: python\n\n    import torch\n    from dict_minimize.torch_api import minimize\n\n    def rosen_obj(params, shift):\n        \"\"\"Based on augmented Rosenbrock from botorch.\"\"\"\n        X, Y = params[\"x_half_a\"], params[\"x_half_b\"]\n        X = X - shift\n        Y = Y - shift\n        obj = 100 * (X[1] - X[0] ** 2) ** 2 + 100 * (Y[1] - Y[0] ** 2) ** 2\n        return obj\n\n    def d_rosen_obj(params, shift):\n        obj = rosen_obj(params, shift=shift)\n        da, db = torch.autograd.grad(obj, [params[\"x_half_a\"], params[\"x_half_b\"]])\n        d_obj = OrderedDict([(\"x_half_a\", da), (\"x_half_b\", db)])\n        return obj, d_obj\n\n    torch.manual_seed(123)\n\n    n_a = 2\n    n_b = 2\n    shift = -1.0\n\n    params = OrderedDict([(\"x_half_a\", torch.randn((n_a,))), (\"x_half_b\", torch.randn((n_b,)))])\n\n    params = minimize(d_rosen_obj, params, args=(shift,), method=\"L-BFGS-B\", options={\"disp\": True})\n\nTensorFlow\n----------\n\n.. code-block:: python\n\n    import tensorflow as tf\n    from dict_minimize.tensorflow_api import minimize\n\n    def rosen_obj(params, shift):\n        \"\"\"Based on augmented Rosenbrock from botorch.\"\"\"\n        X, Y = params[\"x_half_a\"], params[\"x_half_b\"]\n        X = X - shift\n        Y = Y - shift\n        obj = 100 * (X[1] - X[0] ** 2) ** 2 + 100 * (Y[1] - Y[0] ** 2) ** 2\n        return obj\n\n    def d_rosen_obj(params, shift):\n        with tf.GradientTape(persistent=True) as t:\n            t.watch(params[\"x_half_a\"])\n            t.watch(params[\"x_half_b\"])\n\n            obj = rosen_obj(params, shift=shift)\n\n        da = t.gradient(obj, params[\"x_half_a\"])\n        db = t.gradient(obj, params[\"x_half_b\"])\n        d_obj = OrderedDict([(\"x_half_a\", da), (\"x_half_b\", db)])\n        del t  # Explicitly drop the reference to the tape\n        return obj, d_obj\n\n    tf.random.set_seed(123)\n\n    n_a = 2\n    n_b = 2\n    shift = -1.0\n\n    params = OrderedDict([(\"x_half_a\", tf.random.normal((n_a,))), (\"x_half_b\", tf.random.normal((n_b,)))])\n\n    params = minimize(d_rosen_obj, params, args=(shift,), method=\"L-BFGS-B\", options={\"disp\": True})\n\nNumPy\n-----\n\n.. code-block:: python\n\n    import numpy as np\n    from scipy.optimize import rosen, rosen_der\n    from dict_minimize.numpy_api import minimize\n\n    def rosen_obj(params, shift):\n        val = rosen(params[\"x_half_a\"] - shift) + rosen(params[\"x_half_b\"] - shift)\n\n        dval = OrderedDict(\n            [\n                (\"x_half_a\", rosen_der(params[\"x_half_a\"] - shift)),\n                (\"x_half_b\", rosen_der(params[\"x_half_b\"] - shift)),\n            ]\n        )\n        return val, dval\n\n    np.random.seed(0)\n\n    n_a = 3\n    n_b = 5\n    shift = -1.0\n\n    params = OrderedDict([(\"x_half_a\", np.random.randn(n_a)), (\"x_half_b\", np.random.randn(n_b))])\n\n    params = minimize(rosen_obj, params, args=(shift,), method=\"L-BFGS-B\", options={\"disp\": True})\n\nJAX\n---\n\n.. code-block:: python\n\n    from jax import random, value_and_grad\n    import jax.numpy as np\n    from dict_minimize.jax_api import minimize\n\n    def rosen(x):\n        r = np.sum(100.0 * (x[1:] - x[:-1] ** 2.0) ** 2.0 + (1 - x[:-1]) ** 2.0, axis=0)\n        return r\n\n    def rosen_obj(params, shift):\n        val = rosen(params[\"x_half_a\"] - shift) + rosen(params[\"x_half_b\"] - shift)\n        return val\n\n    n_a = 3\n    n_b = 5\n    shift = -1.0\n\n    # Jax makes it this simple:\n    d_rosen_obj = value_and_grad(rosen_obj, argnums=0)\n\n    # Setup randomness in JAX\n    key = random.PRNGKey(0)\n    key, subkey_a = random.split(key)\n    key, subkey_b = random.split(key)\n\n    params = OrderedDict(\n        [(\"x_half_a\", random.normal(subkey_a, shape=(n_a,))), (\"x_half_b\", random.normal(subkey_b, shape=(n_b,)))]\n    )\n\n    params = minimize(d_rosen_obj, params, args=(shift,), method=\"L-BFGS-B\", options={\"disp\": True})\n\nContributing\n============\n\nThe following instructions have been tested with Python 3.7.4 on Mac OS (10.14.6).\n\nInstall in editable mode\n------------------------\n\nFirst, define the variables for the paths we will use:\n\n.. code-block:: bash\n\n   GIT=/path/to/where/you/put/repos\n   ENVS=/path/to/where/you/put/virtualenvs\n\nThen clone the repo in your git directory ``$GIT``:\n\n.. code-block:: bash\n\n   cd $GIT\n   git clone https://github.com/twitter/dict_minimize.git\n\nInside your virtual environments folder ``$ENVS``, make the environment:\n\n.. code-block:: bash\n\n   cd $ENVS\n   virtualenv dict_minimize --python=python3.7\n   source $ENVS/dict_minimize/bin/activate\n\nNow we can install the pip dependencies. Move back into your git directory and run\n\n.. code-block:: bash\n\n   cd $GIT/dict_minimize\n   pip install -r requirements/base.txt\n   pip install -e .  # Install the package itself\n\nContributor tools\n-----------------\n\nFirst, we need to setup some needed tools:\n\n.. code-block:: bash\n\n   cd $ENVS\n   virtualenv dict_minimize_tools --python=python3.7\n   source $ENVS/dict_minimize_tools/bin/activate\n   pip install -r $GIT/dict_minimize/requirements/tools.txt\n\nTo install the pre-commit hooks for contributing run (in the ``dict_minimize_tools`` environment):\n\n.. code-block:: bash\n\n   cd $GIT/dict_minimize\n   pre-commit install\n\nTo rebuild the requirements, we can run:\n\n.. code-block:: bash\n\n   cd $GIT/dict_minimize\n\n   # Check if there any discrepancies in the .in files\n   pipreqs dict_minimize/core/ --diff requirements/base.in\n   pipreqs dict_minimize/ --diff requirements/frameworks.in\n   pipreqs tests/ --diff requirements/tests.in\n   pipreqs docs/ --diff requirements/docs.in\n\n   # Regenerate the .txt files from .in files\n   pip-compile-multi --no-upgrade\n\nGenerating the documentation\n----------------------------\n\nFirst setup the environment for building with ``Sphinx``:\n\n.. code-block:: bash\n\n   cd $ENVS\n   virtualenv dict_minimize_docs --python=python3.7\n   source $ENVS/dict_minimize_docs/bin/activate\n   pip install -r $GIT/dict_minimize/requirements/docs.txt\n\nThen we can do the build:\n\n.. code-block:: bash\n\n   cd $GIT/dict_minimize/docs\n   make all\n   open _build/html/index.html\n\nDocumentation will be available in all formats in ``Makefile``. Use ``make html`` to only generate the HTML documentation.\n\nRunning the tests\n-----------------\n\nThe tests for this package can be run with:\n\n.. code-block:: bash\n\n   cd $GIT/dict_minimize\n   ./local_test.sh\n\nThe script creates an environment using the requirements found in ``requirements/test.txt``.\nA code coverage report will also be produced in ``$GIT/dict_minimize/htmlcov/index.html``.\n\nDeployment\n----------\n\nThe wheel (tar ball) for deployment as a pip installable package can be built using the script:\n\n.. code-block:: bash\n\n   cd $GIT/dict_minimize/\n   ./build_wheel.sh\n\nThis script will only run if the git repo is clean, i.e., first run ``git clean -x -ff -d``.\n\nLinks\n=====\n\nThe `source \u003chttps://github.com/twitter/dict_minimize\u003e`_ is hosted on GitHub.\n\nThe `documentation \u003chttps://dict-minimize.readthedocs.io/en/latest/\u003e`_ is hosted at Read the Docs.\n\nInstallable from `PyPI \u003chttps://pypi.org/project/dict-minimize/\u003e`_.\n\nLicense\n=======\n\nThis project is licensed under the Apache 2 License - see the LICENSE file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwitter%2Fdict_minimize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwitter%2Fdict_minimize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwitter%2Fdict_minimize/lists"}