{"id":15290473,"url":"https://github.com/kpe/params-flow","last_synced_at":"2025-04-13T10:23:49.557Z","repository":{"id":57450752,"uuid":"180211729","full_name":"kpe/params-flow","owner":"kpe","description":"A TensorFlow Keras coding style for reducing boilerplate code in custom layers and models.","archived":false,"fork":false,"pushed_at":"2021-01-21T17:08:15.000Z","size":137,"stargazers_count":19,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-03-23T09:43:06.624Z","etag":null,"topics":["keras","tensorflow"],"latest_commit_sha":null,"homepage":"https://github.com/kpe/params-flow","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kpe.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-08T18:42:46.000Z","updated_at":"2022-05-23T13:43:49.000Z","dependencies_parsed_at":"2022-09-26T17:31:33.987Z","dependency_job_id":null,"html_url":"https://github.com/kpe/params-flow","commit_stats":null,"previous_names":[],"tags_count":57,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kpe%2Fparams-flow","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kpe%2Fparams-flow/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kpe%2Fparams-flow/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kpe%2Fparams-flow/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kpe","download_url":"https://codeload.github.com/kpe/params-flow/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248696488,"owners_count":21147123,"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":["keras","tensorflow"],"created_at":"2024-09-30T16:08:18.130Z","updated_at":"2025-04-13T10:23:49.536Z","avatar_url":"https://github.com/kpe.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"params-flow\n===========\n\n|Build Status| |Coverage Status| |Version Status| |Python Versions| |Downloads|\n\n`params-flow`_ provides an alternative style for defining your `Keras`_ model\nor layer configuration in order to reduce the boilerplate code related to\npassing and (de)serializing your model/layer configuration arguments.\n\n`params-flow`_ encourages this:\n\n.. code:: python\n\n   import params_flow as pf\n\n   class MyDenseLayer(pf.Layer):      # using params_flow Layer/Model instead of Keras ones\n     class Params(pf.Layer.Params):   # extend one or more base Params configurations\n       num_outputs = None             # declare all configuration arguments\n       activation = \"gelu\"            #   provide or override super() defaults\n                                      # do not define an __init__()\n\n     def build(self, in_shape):\n       self.kernel = self.add_variable(\"kernel\",\n                                       [int(in_shape[-1]),\n                                        self.params.num_outputs])     # access config arguments\n\n\nwhich would be sufficient to pass the right configuration arguments to the\nsuper layer/model, as well as take care of (de)serialization, so you can concentrate\non the ``build()`` or ``call()`` implementations, instead of writing boilerplate\ncode like this:\n\n.. code:: python\n\n    from tf.keras.layers import Layer\n\n    class MyDenseLayer(Layer):\n      def __init__(self,\n                   num_outputs,            # put all of the layer configuration in the constructor\n                   activation = \"gelu\",    #     provide defaults\n                   **kwargs):              # allow base layer configuration to be passed to super\n        self.num_outputs = num_outputs\n        self.activation = activation\n        super().__init__(**kwargs)\n\n      def build(self, in_shape):\n        self.kernel = self.add_variable(\"kernel\",\n                                        [int(in_shape[-1]),\n                                         self.num_outputs])      # access config arguments\n\n      def get_config(self):                # serialize layer configuration, __init__() is the deserializer\n        config = {\n          'num_outputs': self.num_outputs,\n          'activation': self.activation\n        }\n        base_config = super().get_config()\n        return dict(list(base_config.items())) + list(config.items())\n\nNEWS\n----\n - **04.Apr.2020** - refactored to use ``WithParams`` mixin from `kpe/py-params`_. Make\n   sure to use ``_construct()`` instead of ``__init__()`` in your ``Layer`` and ``Model`` subclasses.\n   **Breaking Change** - ``_construct()`` signature has changed, please update\n   your ``Layer`` and ``Model`` subclasses from:\n\n   .. code:: python\n\n       def _construct(self, params: Params):\n           ...\n\n   to:\n\n   .. code:: python\n\n       def _construct(self, **kwargs):\n           super()._construct(**kwargs)\n           params = self.params\n           ...\n\n - **11.Sep.2019** - `LookAhead`_ optimizer wrapper implementation for efficient non eager graph mode execution (TPU) added.\n - **05.Sep.2019** - `LookAhead`_ optimizer implementation as Keras callback added.\n - **04.Sep.2019** - `RAdam`_ optimizer implementation added.\n\nLICENSE\n-------\n\nMIT. See `License File \u003chttps://github.com/kpe/params-flow/blob/master/LICENSE.txt\u003e`_.\n\nInstall\n-------\n\n``params-flow`` is on the Python Package Index (PyPI):\n\n::\n\n    pip install params-flow\n\n\nUsage\n-----\n\n``params-flow`` provides a ``Layer`` and ``Model`` base classes that help\nreducing common boilerplate code in your custom Keras layers and models.\n\nWhen subclassing a Keras ``Model`` or ``Layer``, each configuration parameter\nhas to be provided as an argument in ``__init__()``. Keras relies on both ``__init__()``\nand ``get_config()`` to make a model/layer serializable.\n\nWhile python idiomatic this style of defining your Keras models/layers results\nin a lot of boilerplate code. `params-flow`_ provides an alternative by\nencapsulating all those ``__init__()`` configuration arguments in a dedicated\n``Params`` instance (``Params`` is kind of a \"type-safe\" python dict -\nsee `kpe/py-params`_).\nThe model/layer specific configuration needs to be declared as\na nested ``Model.Params``/``Layer.Params`` subclass, and your model/layer have to\nsubclass ``params_flow.Model``/``params_flow.Layer`` instead of the Keras ones:\n\n.. code:: python\n\n   class BertEmbeddingsLayer(Layer):\n     class Params(PositionEmbeddingLayer.Params):\n       vocab_size              = None\n       token_type_vocab_size   = 2\n       hidden_size             = 768\n       use_position_embeddings = True\n\n   class TransformerEncoderLayer(Layer):\n     class Params(TransformerSelfAttentionLayer.Params,\n                  ProjectionLayer.Params):\n       intermediate_size       = 3072\n       intermediate_activation = \"gelu\"\n\n\n\nthis allows you to declare the model's configuration by simply extending\nthe ``Params`` of the underlying layers:\n\n.. code:: python\n\n  class BertModel(Model):\n    class Params(BertEmbeddingsLayer.Params,\n                 TransformerEncoderLayer.Params):\n      pass\n\n**N.B.** The two code excerpts above are taken from `kpe/bert-for-tf2`_, so check there\nfor the details of a non-trivial `params-flow`_ based implementation (of `BERT`_).\n\nResources\n---------\n\n- `kpe/py-params`_  - A \"type-safe\" dict class for python.\n- `kpe/bert-for-tf2`_ - BERT implementation using the TensorFlow 2 Keras API with the help of `params-flow`_ for reducing some of the common Keras boilerplate code needed when passing parameters to custom layers.\n\n\n\n\n.. |Build Status| image:: https://travis-ci.com/kpe/params-flow.svg?branch=master\n   :target: https://travis-ci.com/kpe/params-flow\n.. |Coverage Status| image:: https://coveralls.io/repos/kpe/params-flow/badge.svg?branch=master\n   :target: https://coveralls.io/r/kpe/params-flow\n.. |Version Status| image:: https://badge.fury.io/py/params-flow.svg\n   :target: https://badge.fury.io/py/params-flow\n.. |Python Versions| image:: https://img.shields.io/pypi/pyversions/params-flow.svg\n.. |Downloads| image:: https://img.shields.io/pypi/dm/params-flow.svg\n\n.. _`kpe/py-params`: https://github.com/kpe/py-params\n.. _`kpe/params-flow`: https://github.com/kpe/params-flow\n.. _`kpe/bert-for-tf2`: https://github.com/kpe/bert-for-tf2\n.. _`params-flow`: https://github.com/kpe/params-flow\n\n.. _`Keras`: https://keras.io\n.. _`BERT`: https://github.com/google-research/bert\n.. _`RAdam`: https://arxiv.org/abs/1908.03265\n.. _`LookAhead`: https://arxiv.org/abs/1907.08610\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkpe%2Fparams-flow","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkpe%2Fparams-flow","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkpe%2Fparams-flow/lists"}