Learn fuzzy logic systems from data without expert knowledge.
This project implements a novel approach to fuzzy logic where the system automatically discovers membership functions and rules as pure numerical patterns—no predefined semantic concepts like "HIGH" or "LOW" required.
Traditional fuzzy logic requires experts to manually define:
- Membership functions ("HIGH temperature = above 30°C")
- Fuzzy rules ("IF temperature is HIGH AND humidity is LOW THEN fan speed is HIGH")
Fuzzy Soft Circuits eliminate this requirement. The system learns:
- Membership Functions: Gaussian curves at optimal positions (no predefined meaning)
- Rule Structures: Which combinations of membership indices activate together
- Rule Relevance: Which discovered patterns actually matter
All through gradient-based optimization on training data.
# Clone the repository
git clone https://github.com/queelius/fuzzy-soft-circuit.git
cd fuzzy-soft-circuit
# Install dependencies
pip install autograd numpy scipy
# Optional: for visualization and benchmarks
pip install matplotlib seaborn scikit-learn pandasfrom fuzzy_soft_circuit import FuzzySoftCircuit, train_fuzzy_circuit
# Create fuzzy controller - no semantic labels!
controller = FuzzySoftCircuit(
n_inputs=2, # Two input dimensions (e.g., temperature, humidity)
n_outputs=1, # One output dimension (e.g., fan speed)
n_memberships=3, # 3 membership functions per input (indices 0, 1, 2)
n_rules=10 # Discover up to 10 rules
)
# Training data - pure numerical patterns
data = [
([0.9, 0.2], [0.8]), # Pattern: high dim0, low dim1 → high output
([0.3, 0.8], [0.3]), # Pattern: low dim0, high dim1 → low output
([0.5, 0.5], [0.5]), # Pattern: medium values → medium output
# ... more examples
]
# Train - discovers patterns automatically
params = train_fuzzy_circuit(controller, data, epochs=1000)
# Make predictions
output = controller.forward([0.85, 0.25], params)
print(f"Prediction: {output}")
# Extract learned rules (optional semantic interpretation)
rules = controller.extract_rules(params)
for i, rule in enumerate(rules):
print(f"Rule {i}: {rule}")Instead of semantic labels, the system uses indices:
membership_0,membership_1,membership_2(not "LOW", "MEDIUM", "HIGH")- System learns that
input_0_membership_2activates around value 0.9 - Humans can interpret post-hoc: "membership_2 seems to represent 'high' values"
1. Fuzzification: Learnable Gaussian membership functions
membership_i(x) = exp(-((x - center_i) / width_i)²)Centers and widths are learned from data.
2. Rule Discovery: Soft AND gates with learnable switches
rule_activation = soft_AND(fuzzy_inputs) * sigmoid(rule_switch)The switch parameter learns whether a rule pattern exists at all.
3. Defuzzification: Weighted combination of rule outputs
output = Σ(rule_activation * rule_consequent) / Σ(rule_activation)Rule 3: input_0_membership_2 (0.8) AND input_1_membership_0 (0.9) → output_0_membership_2 (0.7)
This is a pure numerical pattern. A human might interpret it as:
"When temperature is high (membership_2 ≈ 0.9) and humidity is low (membership_0 ≈ 0.2), set fan speed high (membership_2 ≈ 0.8)"
But the system only knows indices and learned parameters.
- Pure Numerical Learning: No hardcoded semantic concepts
- Automatic Membership Discovery: Learns Gaussian curves at optimal positions
- Rule Pattern Discovery: Finds which membership index combinations matter
- Index-Based Architecture: Works with
membership_0,membership_1, etc. - Post-hoc Interpretation: Humans can assign meaning after training
- End-to-End Differentiable: Uses autograd for gradient-based learning
- Interpretable: Can extract human-readable rules from trained models
- No Expert Knowledge Required: Learns from data, not manual specifications
- Adaptive Memberships: Discovers optimal fuzzy sets for the problem
- Rule Discovery: Finds rules you didn't know existed
- End-to-end Differentiable: Use modern optimization techniques
- Domain-Agnostic: Same code works for any fuzzy control problem
The benchmarks/ directory contains comprehensive experiments comparing Fuzzy Soft Circuits against:
- ANFIS (Adaptive Neuro-Fuzzy Inference System) - classical baseline
- MLP (Multi-Layer Perceptron) - neural network baseline
Across 5 standard datasets with statistical validation.
# Run full benchmark suite
cd benchmarks && python run_experiments.py
# Quick test on reduced dataset
cd benchmarks && python run_quick_test.py
# Generate figures for papers
cd benchmarks && python generate_example_figures.pyfuzzy-soft-circuit/
├── fuzzy_soft_circuit/ # Core implementation
│ ├── fuzzy_core.py # FuzzySoftCircuit class
│ ├── fuzzy_pure.py # Pure numerical training functions
│ └── examples.py # Usage examples
│
├── benchmarks/ # Experimental framework
│ ├── run_experiments.py # Main benchmark script
│ ├── datasets/ # Dataset loaders
│ ├── baselines/ # ANFIS and MLP implementations
│ └── analysis/ # Statistical analysis and visualization
│
├── tests/ # Test suite (pytest)
│ └── test_fuzzy.py # Comprehensive tests
│
├── papers/ # Research paper (LaTeX)
│ └── fuzzy/
│ ├── paper.tex # Paper source
│ └── paper.pdf # Compiled paper
│
└── docs/ # Documentation
Automatic Fuzzy Rule Discovery Through Differentiable Soft Circuits
- Introduces pure numerical approach to fuzzy logic learning
- Demonstrates automatic rule discovery without expert knowledge
- Provides comprehensive benchmarks against classical methods
- Control Systems: Temperature control, motor control, HVAC systems
- Classification: Problems with fuzzy boundaries between classes
- Decision Making: Multi-criteria fuzzy decisions
- Pattern Recognition: Discovering interpretable patterns in data
- Time Series: Fuzzy temporal pattern discovery
# Run tests
pytest tests/ -v
# Run with coverage
pytest tests/ --cov=fuzzy_soft_circuit --cov-report=htmlThe system minimizes mean squared error:
L = Σ ||output(inputs; θ) - target||²
Where θ includes:
- Membership function parameters (centers, widths)
- Rule antecedent weights (feature relevance)
- Rule switches (rule existence)
- Rule consequents (outputs)
- Output combination weights
Everything is differentiable, enabling gradient-based learning through autograd.
We welcome contributions! Areas of interest:
- New defuzzification methods
- Alternative membership function shapes
- Performance optimizations
- Applications to new domains
- Visualization tools
MIT License - see LICENSE file.
@software{fuzzy_soft_circuit,
title = {Fuzzy Soft Circuits: Automatic Fuzzy Rule Discovery},
author = {Alexander Towell},
year = {2024},
url = {https://github.com/queelius/fuzzy-soft-circuit}
}This project is part of the "Soft Circuits" research program on making logic differentiable:
- Soft Circuits: Differentiable Boolean logic circuits
"Fuzzy logic, learned not specified."