๐ BusinessRulesLayer
๐ BusinessRulesLayer
๐ฏ Overview
The BusinessRulesLayer applies configurable business rules to neural network outputs, enabling the combination of learned patterns with explicit domain knowledge. This layer is particularly useful for anomaly detection and data validation where business rules can provide additional constraints.
This layer supports both numerical and categorical features with various comparison operators, making it flexible for different types of business rule validation.
๐ How It Works
The BusinessRulesLayer processes data through configurable business rules:
- Rule Definition: Defines business rules for numerical or categorical features
- Rule Evaluation: Evaluates each rule against the input data
- Anomaly Detection: Identifies data that violates business rules
- Weight Learning: Optionally learns weights for soft rule enforcement
- Output Generation: Produces anomaly detection results
graph TD
A[Input Features] --> B[Rule Evaluation]
B --> C[Numerical Rules]
B --> D[Categorical Rules]
C --> E[Comparison Operators: >, <]
D --> F[Set Operators: ==, in, !=, not in]
E --> G[Rule Violations]
F --> G
G --> H[Anomaly Detection]
H --> I[Business Anomaly Output]
J[Learnable Weights] --> G
style A fill:#e6f3ff,stroke:#4a86e8
style I fill:#e8f5e9,stroke:#66bb6a
style B fill:#fff9e6,stroke:#ffb74d
style C fill:#f3e5f5,stroke:#9c27b0
style D fill:#f3e5f5,stroke:#9c27b0
style G fill:#e1f5fe,stroke:#03a9f4
๐ก Why Use This Layer?
| Challenge | Traditional Approach | BusinessRulesLayer's Solution |
|---|---|---|
| Domain Knowledge | Separate rule validation | ๐ฏ Integrated business rules in neural networks |
| Anomaly Detection | Statistical methods only | โก Rule-based anomaly detection |
| Data Validation | Manual validation | ๐ง Automatic validation with business rules |
| Interpretability | Black box models | ๐ Interpretable rule-based validation |
๐ Use Cases
- Anomaly Detection: Detecting data that violates business rules
- Data Validation: Validating data against business constraints
- Domain Knowledge: Incorporating domain expertise into models
- Quality Control: Ensuring data quality with business rules
- Compliance: Enforcing business compliance rules
๐ Quick Start
Basic Usage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Categorical Rules
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
In a Sequential Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
In a Functional Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Advanced Configuration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
๐ API Reference
kerasfactory.layers.BusinessRulesLayer
This module implements a BusinessRulesLayer that allows applying configurable business rules to neural network outputs. This enables combining learned patterns with explicit domain knowledge.
Classes
BusinessRulesLayer
1 2 3 4 5 6 7 8 9 | |
Evaluates business-defined rules for anomaly detection.
This layer applies user-defined business rules to detect anomalies. Rules can be defined for both numerical and categorical features.
For numerical features
- Comparison operators: '>' and '<'
- Example: [(">", 0), ("<", 100)] for range validation
For categorical features
- Set operators: '==', 'in', '!=', 'not in'
- Example: [("in", ["red", "green", "blue"])] for valid categories
Attributes:
| Name | Type | Description |
|---|---|---|
rules |
List of rule tuples (operator, value). |
|
feature_type |
Type of feature ('numerical' or 'categorical'). |
Example
1 2 3 4 5 6 7 8 9 10 11 12 | |
Initializes the layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rules |
list[Rule]
|
List of rule tuples (operator, value). |
required |
feature_type |
str
|
Type of feature ('numerical' or 'categorical'). |
required |
trainable_weights |
bool
|
Whether to use trainable weights for soft rule enforcement. Default is True. |
True
|
weight_initializer |
str | Initializer
|
Initializer for rule weights. Default is 'ones'. |
'ones'
|
name |
str | None
|
Optional name for the layer. |
None
|
**kwargs |
Any
|
Additional layer arguments. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If feature_type is invalid or rules have invalid operators. |
Source code in kerasfactory/layers/BusinessRulesLayer.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
Functions
1 2 3 | |
Compute the output shape of the layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_shape |
tuple[int | None, int]
|
Input shape tuple. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, tuple[int | None, int]]
|
Dictionary mapping output names to their shapes. |
Source code in kerasfactory/layers/BusinessRulesLayer.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
๐ง Parameters Deep Dive
rules (list)
- Purpose: List of business rules to apply
- Format: List of tuples (operator, value)
- Examples: [(">", 0), ("<", 100)] for numerical, [("in", ["red", "green"])] for categorical
- Impact: Defines the business constraints to enforce
- Recommendation: Define rules based on domain knowledge
feature_type (str)
- Purpose: Type of feature being validated
- Options: "numerical" or "categorical"
- Impact: Determines which operators are available
- Recommendation: Use "numerical" for continuous data, "categorical" for discrete data
trainable_weights (bool)
- Purpose: Whether to use trainable weights for soft rule enforcement
- Default: True
- Impact: Allows learning optimal rule weights
- Recommendation: Use True for most applications
weight_initializer (str or initializer)
- Purpose: Initializer for rule weights
- Default: "ones"
- Impact: Affects initial rule importance
- Recommendation: Use "ones" for equal initial importance
๐ Performance Characteristics
- Speed: โกโกโกโก Very fast - simple rule evaluation
- Memory: ๐พ Low memory usage - minimal additional parameters
- Accuracy: ๐ฏ๐ฏ๐ฏ๐ฏ Excellent for rule-based validation
- Best For: Data validation and anomaly detection with business rules
๐จ Examples
Example 1: Financial Data Validation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
Example 2: Categorical Data Validation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
Example 3: Rule Analysis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
๐ก Tips & Best Practices
- Rule Definition: Define rules based on domain knowledge
- Feature Types: Use appropriate feature types (numerical vs categorical)
- Trainable Weights: Use trainable weights for soft rule enforcement
- Rule Complexity: Start with simple rules, add complexity as needed
- Validation: Test rules on known good and bad data
- Integration: Combine with other layers for comprehensive validation
โ ๏ธ Common Pitfalls
- Feature Types: Must match the actual data type
- Rule Operators: Use correct operators for each feature type
- Rule Values: Ensure rule values are appropriate for the data
- Memory Usage: Rules are evaluated for each sample
- Gradient Flow: Rules may not be differentiable
๐ Related Layers
- NumericalAnomalyDetection - Numerical anomaly detection
- CategoricalAnomalyDetectionLayer - Categorical anomaly detection
- FeatureCutout - Feature regularization
- StochasticDepth - Stochastic depth regularization
๐ Further Reading
- Business Rules - Business rule concepts
- Anomaly Detection - Anomaly detection techniques
- Data Validation - Data validation concepts
- KerasFactory Layer Explorer - Browse all available layers
- Feature Engineering Tutorial - Complete guide to feature engineering