openasce.extension.debias.common package¶
Submodules¶
openasce.extension.debias.common.utils module¶
- class openasce.extension.debias.common.utils.DNNLayer(*args, **kwargs)[source]¶
Bases:
LayerBuilding a MLP/DNN Layer.
Layer: dense dnn layer
- inputs:
2d tensor (batch_size, input_dim)
- outputs:
2d tensor (batch_size, output_dim)
Initialize DNNLayer.
- Parameters
hidden_units – list of positive integer, the layer number and units in each layer.
activation – Activation function to use.
l1_reg – float between 0 and 1. L1 regularizer strength applied to the kernel weights matrix.
l2_reg – float between 0 and 1. L2 regularizer strength applied to the kernel weights matrix.
dropout_rate – float in [0,1). Fraction of the units to dropout.
use_bn – bool. Whether use BatchNormalization before activation or not.
apply_final_act – whether to apply act in final layer
seed – A Python integer to use as random seed.
- __doc__ = 'Building a MLP/DNN Layer.\n\n Layer: dense dnn layer\n\n inputs:\n 2d tensor (batch_size, input_dim)\n outputs:\n 2d tensor (batch_size, output_dim)\n '¶
- __init__(hidden_units: Optional[List] = None, activation='relu', l1_reg=0.0001, l2_reg=0.0001, dropout_rate=0, use_bn=False, apply_final_act=True, seed=1024, **kwargs)[source]¶
Initialize DNNLayer.
- Parameters
hidden_units – list of positive integer, the layer number and units in each layer.
activation – Activation function to use.
l1_reg – float between 0 and 1. L1 regularizer strength applied to the kernel weights matrix.
l2_reg – float between 0 and 1. L2 regularizer strength applied to the kernel weights matrix.
dropout_rate – float in [0,1). Fraction of the units to dropout.
use_bn – bool. Whether use BatchNormalization before activation or not.
apply_final_act – whether to apply act in final layer
seed – A Python integer to use as random seed.
- __module__ = 'openasce.extension.debias.common.utils'¶
- build(input_shape)[source]¶
Creates the variables of the layer (for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().
This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).
- Parameters
input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).
- call(inputs, training=None, **kwargs)[source]¶
This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state, including tf.Variable instances and nested Layer instances,
in __init__(), or in the build() method that is
called automatically before call() executes for the first time.
- Parameters
inputs –
Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
NumPy array or Python scalar values in inputs get cast as tensors.
Keras mask metadata is only collected from inputs.
Layers are built (build(input_shape) method) using shape info from inputs only.
input_spec compatibility is only checked against inputs.
Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
The SavedModel input specification is generated using inputs only.
Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
*args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
**kwargs –
Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
- Returns
A tensor or list/tuple of tensors.
- compute_output_shape(input_shape)[source]¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Parameters
input_shape – Shape tuple (tuple of integers) or tf.TensorShape, or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns
A tf.TensorShape instance or structure of tf.TensorShape instances.
- get_config()[source]¶
Returns the config of the layer.
A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.
The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).
Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.
- Returns
Python dictionary.
- class openasce.extension.debias.common.utils.DNNModel(*args, **kwargs)[source]¶
Bases:
ModelBuilding a DNN model.
Model: DNN or MLP.
- inputs:
2d tensor (batch_size, input_dim).
- outputs:
2d tensor (batch_size, output_dim).
Initialize DNNModel.
- Parameters
hidden_units – list, unit in each hidden layer.
act_fn – string, activation function.
l2_reg – float, regularization value.
dropout_rate – float, fraction of the units to dropout.
use_bn – boolean, if True, apply BatchNormalization in each hidden layer.
seed – int, random value for initialization.
- __annotations__ = {}¶
- __doc__ = 'Building a DNN model.\n\n Model: DNN or MLP.\n\n inputs:\n 2d tensor (batch_size, input_dim).\n outputs:\n 2d tensor (batch_size, output_dim).\n '¶
- __init__(hidden_units, act_fn='relu', l1_reg=0.0001, l2_reg=0.0001, dropout_rate=0, use_bn=False, seed=1024, apply_final_act=False, name='DNNModel')[source]¶
Initialize DNNModel.
- Parameters
hidden_units – list, unit in each hidden layer.
act_fn – string, activation function.
l2_reg – float, regularization value.
dropout_rate – float, fraction of the units to dropout.
use_bn – boolean, if True, apply BatchNormalization in each hidden layer.
seed – int, random value for initialization.
- __module__ = 'openasce.extension.debias.common.utils'¶
- class openasce.extension.debias.common.utils.FMLayer(*args, **kwargs)[source]¶
Bases:
LayerBuilding a FM Layer. Model: Factorization Machine.
Paper: Factorization Machine models pairwise (order-2) feature interactions without linear term and bias.
Link: https://www.csie.ntu.edu.tw/~b97053/paper/Rendle2010FM.pdf.
Author: Steffen Rendle
- Input shape
3D tensor with shape:
(batch_size,field_size,embedding_size).
- Output shape
2D tensor with shape:
(batch_size, dim).
- __annotations__ = {}¶
- __doc__ = 'Building a FM Layer.\n Model: Factorization Machine.\n\n Paper: Factorization Machine models pairwise (order-2) feature interactions without linear term and bias.\n\n Link: https://www.csie.ntu.edu.tw/~b97053/paper/Rendle2010FM.pdf.\n\n Author: Steffen Rendle\n\n Input shape\n - 3D tensor with shape: ``(batch_size,field_size,embedding_size)``.\n Output shape\n - 2D tensor with shape: ``(batch_size, dim)``.\n\n '¶
- __module__ = 'openasce.extension.debias.common.utils'¶
- build(input_shape)[source]¶
Creates the variables of the layer (for subclass implementers).
This is a method that implementers of subclasses of Layer or Model can override if they need a state-creation step in-between layer instantiation and layer call. It is invoked automatically before the first execution of call().
This is typically used to create the weights of Layer subclasses (at the discretion of the subclass implementer).
- Parameters
input_shape – Instance of TensorShape, or list of instances of TensorShape if the layer expects a list of inputs (one instance per input).
- call(inputs, **kwargs)[source]¶
Calls the layer on new inputs. :param input_shape: Shape tuple (tuple of integers) or list of shape tuples (one per output tensor of the layer).
- compute_output_shape(input_shape)[source]¶
Computes the output shape of the layer.
This method will cause the layer’s state to be built, if that has not happened before. This requires that the layer will later be used with inputs that match the input shape provided here.
- Parameters
input_shape – Shape tuple (tuple of integers) or tf.TensorShape, or structure of shape tuples / tf.TensorShape instances (one per output tensor of the layer). Shape tuples can include None for free dimensions, instead of an integer.
- Returns
A tf.TensorShape instance or structure of tf.TensorShape instances.
- class openasce.extension.debias.common.utils.MultiTaskDNNModel(*args, **kwargs)[source]¶
Bases:
ModelBuilding a multi task dnn model.
Model: MultiTaskDNN.
- inputs:
2d tensor (batch_size, input_dim).
- outputs:
list odf 2d tensor [(batch_size, output_dim),..].
Initialize MultiTaskDNNModel.
- Parameters
hidden_units – list, unit in each hidden layer.
act_fn – string, activation function.
num_tasks – int, number of the task.
l2_reg – float, regularization value.
dropout_rate – float, fraction of the units to dropout.
use_bn – boolean, if True, apply BatchNormalization in each hidden layer.
seed – int, random value for initialization.
- __annotations__ = {}¶
- __doc__ = 'Building a multi task dnn model.\n\n Model: MultiTaskDNN.\n\n inputs:\n 2d tensor (batch_size, input_dim).\n outputs:\n list odf 2d tensor [(batch_size, output_dim),..].\n '¶
- __init__(hidden_units, num_tasks, task_hidden_units, act_fn='relu', l1_reg=0.0001, l2_reg=0.0001, dropout_rate=0, use_bn=False, seed=1024, apply_final_act=False, task_apply_final_act=False, name='MultiTaskDNNModel')[source]¶
Initialize MultiTaskDNNModel.
- Parameters
hidden_units – list, unit in each hidden layer.
act_fn – string, activation function.
num_tasks – int, number of the task.
l2_reg – float, regularization value.
dropout_rate – float, fraction of the units to dropout.
use_bn – boolean, if True, apply BatchNormalization in each hidden layer.
seed – int, random value for initialization.
- __module__ = 'openasce.extension.debias.common.utils'¶
- openasce.extension.debias.common.utils.mmd_rbf(X, t, p, sig)[source]¶
Computes the l2-RBF MMD for X given t
Paper: Estimating individual treatment effect: generalization bounds and algorithms. Gaussian kernel(RBF): https://en.wikipedia.org/wiki/Radial_basis_function_kernel :param X: embeddings. :param t: 1 or 0. Distinguish the treatment group and the control group. :param p: the proportion of the number of treatment instances / the number of all instances. :param sig: kernel width.