← back

Domain Expert Model Fusion

#348 · Machine Learning · Medium

⊣ Solve on deep-ml.com

Problem

Implement domain expert model fusion. Given predictions and confidence scores from multiple domain-specific expert models, combine them using a weighted fusion strategy based on each expert's confidence and domain relevance.

Solution

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
39
40
41
42
43
44
import numpy as np
from typing import Dict, List

def expert_fusion(
    expert_predictions: List[np.ndarray],
    expert_confidences: List[np.ndarray],
    domain_weights: List[float] = None
) -> Dict:
    n_experts = len(expert_predictions)
    n_samples = len(expert_predictions[0])

    if domain_weights is None:
        domain_weights = [1.0] * n_experts

    # Normalize domain weights
    total_dw = sum(domain_weights)
    domain_weights = [w / total_dw for w in domain_weights]

    # Compute combined weights: domain_weight * confidence
    combined_weights = np.zeros((n_samples, n_experts))
    for i in range(n_experts):
        combined_weights[:, i] = domain_weights[i] * expert_confidences[i]

    # Normalize weights per sample
    weight_sums = combined_weights.sum(axis=1, keepdims=True)
    weight_sums = np.maximum(weight_sums, 1e-10)
    normalized_weights = combined_weights / weight_sums

    # Weighted fusion of predictions
    fused = np.zeros(n_samples)
    for i in range(n_experts):
        fused += normalized_weights[:, i] * expert_predictions[i]

    # Compute fusion confidence
    fusion_confidence = np.zeros(n_samples)
    for i in range(n_experts):
        fusion_confidence += normalized_weights[:, i] * expert_confidences[i]

    return {
        "fused_predictions": fused.tolist(),
        "fusion_confidence": fusion_confidence.tolist(),
        "expert_weights": normalized_weights.tolist(),
        "n_experts": n_experts
    }

Explanation

  1. Each expert provides predictions and per-sample confidence scores, along with a global domain relevance weight.
  2. For each sample, the effective weight of expert i is domain_weight_i * confidence_i. These are normalized to sum to 1 per sample.
  3. The fused prediction is the weighted sum of expert predictions using these normalized weights.
  4. Experts that are more confident and more relevant to the domain have greater influence on the final prediction.

Complexity

  • Time: O(n * E) where n is samples and E is number of experts
  • Space: O(n * E) for the weight matrix