Intervention Policies

This module provides policies for selecting which concepts to intervene on during inference.

Summary

Policy Classes

UniformPolicy

Uniform intervention policy that assigns equal priority to all concepts.

RandomPolicy

Random intervention policy that generates random values for concept selection.

UncertaintyInterventionPolicy

Uncertainty-based intervention policy using distance from a maximum uncertainty point.

Class Documentation

class UniformPolicy(out_features: int)[source]

Bases: BaseConceptLayer

Uniform intervention policy that assigns equal priority to all concepts.

This policy returns zeros for all concepts, indicating uniform/equal uncertainty or priority across all concepts. Useful as a baseline where no concept is preferred over others.

out_features

Number of output features.

Type:

int

Parameters:

out_features – Number of output concept features.

Example

>>> import torch
>>> from torch_concepts.nn import UniformPolicy
>>>
>>> # Create uniform policy
>>> policy = UniformPolicy(out_features=10)
>>>
>>> # Generate random concept endogenous
>>> endogenous = torch.randn(4, 10)  # batch_size=4, n_concepts=10
>>>
>>> # Apply policy - returns zeros (uniform priority)
>>> scores = policy(endogenous)
>>> print(scores.shape)  # torch.Size([4, 10])
>>> print(torch.all(scores == 0.0))  # True
>>>
>>> # Useful for baseline comparisons
>>> # All concepts have equal intervention priority
>>> print(scores.mean())  # tensor(0.)
>>> print(scores.std())   # tensor(0.)
forward(endogenous: Tensor) Tensor[source]

Generate uniform (zero) intervention scores.

Parameters:

endogenous – Input concept endogenous of shape (batch_size, n_concepts).

Returns:

Zeros tensor of same shape as input.

Return type:

torch.Tensor

training: bool
class RandomPolicy(out_features: int, scale: float = 1.0)[source]

Bases: BaseConceptLayer

Random intervention policy that generates random values for concept selection.

This policy generates random values scaled by a factor, useful for random baseline comparisons in intervention experiments.

out_features

Number of output features.

Type:

int

scale

Scaling factor for random values.

Type:

float

Parameters:
  • out_features – Number of output concept features.

  • scale – Scaling factor for random values (default: 1.0).

Example

>>> import torch
>>> from torch_concepts.nn import RandomPolicy
>>>
>>> # Create random policy
>>> policy = RandomPolicy(out_features=10, scale=2.0)
>>>
>>> # Generate random concept endogenous
>>> endogenous = torch.randn(4, 10)  # batch_size=4, n_concepts=10
>>>
>>> # Apply policy to get random intervention scores
>>> scores = policy(endogenous)
>>> print(scores.shape)  # torch.Size([4, 10])
>>> print(scores.min() >= 0.0)  # True (absolute values)
>>> print(scores.max() <= 2.0)  # True (scaled by 2.0)
>>>
>>> # Each call generates different random values
>>> scores2 = policy(endogenous)
>>> print(torch.equal(scores, scores2))  # False
forward(endogenous: Tensor) Tensor[source]

Generate random intervention scores.

Parameters:

endogenous – Input concept endogenous of shape (batch_size, n_concepts).

Returns:

Random scores of same shape as input, scaled by self.scale.

Return type:

torch.Tensor

training: bool
class UncertaintyInterventionPolicy(out_features: int, max_uncertainty_point: float = 0.0)[source]

Bases: BaseConceptLayer

Uncertainty-based intervention policy using distance from a maximum uncertainty point.

This policy measures uncertainty as the distance of concept endogenous from a maximum uncertainty point. Values closer to this point are considered more uncertain, while values further from this point are considered more certain.

out_features

Number of output features.

Type:

int

max_uncertainty_point

The point where uncertainty is maximum.

Type:

float

Parameters:
  • out_features – Number of output concept features.

  • max_uncertainty_point – The value representing maximum uncertainty (default: 0.0). Values closer to this point are more uncertain, values further away are more certain.

Example

>>> import torch
>>> from torch_concepts.nn import UncertaintyInterventionPolicy
>>>
>>> # Create uncertainty policy with default max uncertainty point (0.0)
>>> policy = UncertaintyInterventionPolicy(out_features=10)
>>>
>>> # Generate concept endogenous with varying confidence
>>> endogenous = torch.tensor([
...     [3.0, -2.5, 0.1, -0.2, 4.0],  # High confidence for 1st, 2nd, 5th
...     [0.5, 0.3, -0.4, 2.0, -1.5]   # Mixed confidence
... ])
>>>
>>> # Apply policy - returns distance from max uncertainty point (certainty scores)
>>> scores = policy(endogenous)
>>> print(scores)
>>> # tensor([[3.0, 2.5, 0.1, 0.2, 4.0],
>>> #         [0.5, 0.3, 0.4, 2.0, 1.5]])
>>>
>>> # Higher scores = higher certainty = lower intervention priority
>>> # For intervention, you'd typically intervene on LOW scores
>>> print(scores[0].argmin())  # tensor(2) - most uncertain concept
>>> print(scores[0].argmax())  # tensor(4) - most certain concept
>>>
>>> # Use custom max uncertainty point (e.g., 0.5 for probabilities)
>>> policy_prob = UncertaintyInterventionPolicy(out_features=5, max_uncertainty_point=0.5)
>>> probs = torch.tensor([[0.1, 0.5, 0.9, 0.45, 0.55]])
>>> certainty = policy_prob(probs)
>>> print(certainty)
>>> # tensor([[0.4, 0.0, 0.4, 0.05, 0.05]])
>>> # Values at 0.5 are most uncertain, values at 0.1 or 0.9 are most certain
forward(endogenous: Tensor) Tensor[source]

Compute certainty scores as distance from maximum uncertainty point.

Parameters:

endogenous – Input concept endogenous of shape (batch_size, n_concepts).

Returns:

Distance from max uncertainty point (certainty scores) of same shape as input.

Higher values indicate higher certainty (further from max uncertainty point). Lower values indicate higher uncertainty (closer to max uncertainty point).

Return type:

torch.Tensor

training: bool