runn.keras.layers.mono_dense
Constrained Monotonic Neural Networks.
This Python library implements Constrained Monotonic Neural Networks as described in:
Davor Runje, Sharath M. Shankaranarayana, “Constrained Monotonic Neural Networks”, in Proceedings of the 40th International Conference on Machine Learning, 2023. Github: https://github.com/airtai/monotonic-nn/tree/main
Version: 0.3.4 (https://github.com/airtai/monotonic-nn/releases/tag/0.3.4)
This file has been modified from the original version by the authors of RUNN to be compatible with the RUNN library.
T = TypeVar('T')
module-attribute
#
MonoDense(units, *, activation=None, monotonicity_indicator=1, is_convex=False, is_concave=False, activation_weights=(7.0, 7.0, 2.0), **kwargs)
#
Bases: Dense
Monotonic counterpart of the regular Dense Layer of tf.keras
Constructs a new MonoDense instance.
PARAMETER | DESCRIPTION |
---|---|
units |
Positive integer, dimensionality of the output space.
TYPE:
|
activation |
Activation function to use, it is assumed to be convex monotonically increasing function such as "relu" or "elu"
TYPE:
|
monotonicity_indicator |
Vector to indicate which of the inputs are monotonically increasing or monotonically decreasing or non-monotonic. Has value 1 for monotonically increasing, -1 for monotonically decreasing and 0 for non-monotonic.
TYPE:
|
is_convex |
convex if set to True
TYPE:
|
is_concave |
concave if set to True
TYPE:
|
activation_weights |
relative weights for each type of activation, the default is (1.0, 1.0, 1.0). Ignored if is_convex or is_concave is set to True |
**kwargs |
passed as kwargs to the constructor of
TYPE:
|
Raise
ValueError: - if both is_concave and is_convex are set to True, or - if any component of activation_weights is negative or there is not exactly three components
Source code in runn/keras/layers/mono_dense.py
activation_weights = activation_weights
instance-attribute
#
is_concave = is_concave
instance-attribute
#
is_convex = is_convex
instance-attribute
#
monotonicity_indicator = monotonicity_indicator
instance-attribute
#
org_activation = activation
instance-attribute
#
units = units
instance-attribute
#
apply_activations(x, *, units, convex_activation, concave_activation, saturated_activation, is_convex=False, is_concave=False, activation_weights=(7.0, 7.0, 2.0))
#
Source code in runn/keras/layers/mono_dense.py
apply_monotonicity_indicator_to_kernel(kernel, monotonicity_indicator)
#
Source code in runn/keras/layers/mono_dense.py
build(input_shape, *args, **kwargs)
#
Build
PARAMETER | DESCRIPTION |
---|---|
input_shape |
input tensor
TYPE:
|
args |
positional arguments passed to Dense.build()
TYPE:
|
kwargs |
keyword arguments passed to Dense.build()
TYPE:
|
Source code in runn/keras/layers/mono_dense.py
call(inputs)
#
Call
PARAMETER | DESCRIPTION |
---|---|
inputs |
input tensor of shape (batch_size, ..., x_length)
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorLike
|
N-D tensor with shape: |
Source code in runn/keras/layers/mono_dense.py
create_type_1(inputs, *, units, final_units, activation, n_layers, final_activation=None, monotonicity_indicator=1, is_convex=False, is_concave=False, dropout=None)
classmethod
#
Builds Type-1 monotonic network
Type-1 architecture corresponds to the standard MLP type of neural network architecture used in general, where each of the input features is concatenated to form one single input feature vector $\mathbf{x}$ and fed into the network, with the only difference being that instead of standard fully connected or dense layers, we employ monotonic dense units throughout. For the first (or input layer) layer, the indicator vector $\mathbf{t}$, is used to identify the monotonicity property of the input feature with respect to the output. Specifically, $\mathbf{t}$ is set to $1$ for those components in the input feature vector that are monotonically increasing and is set to $-1$ for those components that are monotonically decreasing and set to $0$ if the feature is non-monotonic. For the subsequent hidden layers, monotonic dense units with the indicator vector $\mathbf{t}$ always being set to $1$ are used in order to preserve monotonicity. Finally, depending on whether the problem at hand is a regression problem or a classification problem (or even a multi-task problem), an appropriate activation function (such as linear activation or sigmoid or softmax) to obtain the final output.
PARAMETER | DESCRIPTION |
---|---|
inputs |
input tensor or a dictionary of tensors
TYPE:
|
units |
number of units in hidden layers
TYPE:
|
final_units |
number of units in the output layer
TYPE:
|
activation |
the base activation function
TYPE:
|
n_layers |
total number of layers (hidden layers plus the output layer)
TYPE:
|
final_activation |
the activation function of the final layer (typicall softmax, sigmoid or linear). If set to None (default value), then the linear activation is used.
TYPE:
|
monotonicity_indicator |
if an instance of dictionary, then maps names of input feature to their monotonicity indicator (-1 for monotonically decreasing, 1 for monotonically increasing and 0 otherwise). If int, then all input features are set to the same monotinicity indicator. |
is_convex |
set to True if a particular input feature is convex
TYPE:
|
is_concave |
set to True if a particular inputs feature is concave
TYPE:
|
dropout |
dropout rate. If set to float greater than 0, Dropout layers are inserted after hidden layers.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorLike
|
Output tensor |
Source code in runn/keras/layers/mono_dense.py
create_type_2(inputs, *, input_units=None, units, final_units, activation, n_layers, final_activation=None, monotonicity_indicator=1, is_convex=False, is_concave=False, dropout=None)
classmethod
#
Builds Type-2 monotonic network
Type-2 architecture is another example of a neural network architecture that can be built employing proposed monotonic dense blocks. The difference when compared to the architecture described above lies in the way input features are fed into the hidden layers of neural network architecture. Instead of concatenating the features directly, this architecture provides flexibility to employ any form of complex feature extractors for the non-monotonic features and use the extracted feature vectors as inputs. Another difference is that each monotonic input is passed through separate monotonic dense units. This provides an advantage since depending on whether the input is completely concave or convex or both, we can adjust the activation selection vector $\mathbf{s}$ appropriately along with an appropriate value for the indicator vector $\mathbf{t}$. Thus, each of the monotonic input features has a separate monotonic dense layer associated with it. Thus as the major difference to the above-mentioned architecture, we concatenate the feature vectors instead of concatenating the inputs directly. The subsequent parts of the network are similar to the architecture described above wherein for the rest of the hidden monotonic dense units, the indicator vector $\mathbf{t}$ is always set to $1$ to preserve monotonicity.
PARAMETER | DESCRIPTION |
---|---|
inputs |
input tensor or a dictionary of tensors
TYPE:
|
input_units |
used to preprocess features before entering the common mono block
TYPE:
|
units |
number of units in hidden layers
TYPE:
|
final_units |
number of units in the output layer
TYPE:
|
activation |
the base activation function
TYPE:
|
n_layers |
total number of layers (hidden layers plus the output layer)
TYPE:
|
final_activation |
the activation function of the final layer (typicall softmax, sigmoid or linear). If set to None (default value), then the linear activation is used.
TYPE:
|
monotonicity_indicator |
if an instance of dictionary, then maps names of input feature to their monotonicity indicator (-1 for monotonically decreasing, 1 for monotonically increasing and 0 otherwise). If int, then all input features are set to the same monotinicity indicator. |
is_convex |
set to True if a particular input feature is convex
TYPE:
|
is_concave |
set to True if a particular inputs feature is concave
TYPE:
|
dropout |
dropout rate. If set to float greater than 0, Dropout layers are inserted after hidden layers.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
TensorLike
|
Output tensor |
Source code in runn/keras/layers/mono_dense.py
get_activation_functions(activation=None)
cached
#
Source code in runn/keras/layers/mono_dense.py
get_config()
#
Get config is used for saving the model