Package: params-flow
params-flowprovides an alternative style for defining yourKerasmodel
or layer configuration in order to reduce the boilerplate code related to
passing and (de)serializing your model/layer configuration arguments.params-flowencourages this:importparams_flowaspfclassMyDenseLayer(pf.Layer):# using params_flow Layer/Model instead of Keras onesclassParams(pf.Layer.Params):# extend one or more base Params configurationsnum_outputs=None# declare all configuration argumentsactivation="gelu"#   provide or override super() defaults# do not define an __init__()defbuild(self,in_shape):self.kernel=self.add_variable("kernel",[int(in_shape[-1]),self.params.num_outputs])# access config argumentswhich would be sufficient to pass the right configuration arguments to the
super layer/model, as well as take care of (de)serialization, so you can concentrate
on thebuild()orcall()implementations, instead of writing boilerplate
code like this:fromtf.keras.layersimportLayerclassMyDenseLayer(Layer):def__init__(self,num_outputs,# put all of the layer configuration in the constructoractivation="gelu",#     provide defaults**kwargs):# allow base layer configuration to be passed to superself.num_outputs=num_outputsself.activation=activationsuper().__init__(**kwargs)defbuild(self,in_shape):self.kernel=self.add_variable("kernel",[int(in_shape[-1]),self.num_outputs])# access config argumentsdefget_config(self):# serialize layer configuration, __init__() is the deserializerconfig={'num_outputs':self.num_outputs,'activation':self.activation}base_config=super().get_config()returndict(list(base_config.items()))+list(config.items())NEWS04.Apr.2020- refactored to useWithParamsmixin fromkpe/py-params. Make
sure to use_construct()instead of__init__()in yourLayerandModelsubclasses.Breaking Change-_construct()signature has changed, please update
yourLayerandModelsubclasses from:def_construct(self,params:Params):...to:def_construct(self,**kwargs):super()._construct(**kwargs)params=self.params...11.Sep.2019-LookAheadoptimizer wrapper implementation for efficient non eager graph mode execution (TPU) added.05.Sep.2019-LookAheadoptimizer implementation as Keras callback added.04.Sep.2019-RAdamoptimizer implementation added.LICENSEMIT. SeeLicense File.Installparams-flowis on the Python Package Index (PyPI):pip install params-flowUsageparams-flowprovides aLayerandModelbase classes that help
reducing common boilerplate code in your custom Keras layers and models.When subclassing a KerasModelorLayer, each configuration parameter
has to be provided as an argument in__init__(). Keras relies on both__init__()andget_config()to make a model/layer serializable.While python idiomatic this style of defining your Keras models/layers results
in a lot of boilerplate code.params-flowprovides an alternative by
encapsulating all those__init__()configuration arguments in a dedicatedParamsinstance (Paramsis kind of a “type-safe” python dict -
seekpe/py-params).
The model/layer specific configuration needs to be declared as
a nestedModel.Params/Layer.Paramssubclass, and your model/layer have to
subclassparams_flow.Model/params_flow.Layerinstead of the Keras ones:classBertEmbeddingsLayer(Layer):classParams(PositionEmbeddingLayer.Params):vocab_size=Nonetoken_type_vocab_size=2hidden_size=768use_position_embeddings=TrueclassTransformerEncoderLayer(Layer):classParams(TransformerSelfAttentionLayer.Params,ProjectionLayer.Params):intermediate_size=3072intermediate_activation="gelu"this allows you to declare the model’s configuration by simply extending
theParamsof the underlying layers:classBertModel(Model):classParams(BertEmbeddingsLayer.Params,TransformerEncoderLayer.Params):passN.B.The two code excerpts above are taken fromkpe/bert-for-tf2, so check there
for the details of a non-trivialparams-flowbased implementation (ofBERT).Resourceskpe/py-params- A “type-safe” dict class for python.kpe/bert-for-tf2- BERT implementation using the TensorFlow 2 Keras API with the help ofparams-flowfor reducing some of the common Keras boilerplate code needed when passing parameters to custom layers.