pulsatrix 1.1.0
Loading...
Searching...
No Matches
pulsatrix::LRP Class Reference

Whole-model LRP: runs the forward pass, seeds relevance at the chosen output(s), and propagates it to the input through every module's own propagate_relevance() rule. More...

#include <lrp.hpp>

Public Member Functions

 LRP (LRPRuleConfig config=LRPRuleConfig{})
 Uniform rule: every module applies config.
 
 LRP (LRPComposite composite, std::string name="custom")
 Per-layer rules from a composite; name is reported as "composite:<name>".
 
Attribution explain (ExplainerContext &ctx, const Tensor &input, int64_t target_index, DeviceBackend *backend) const
 Explains target_index for every row, OutputValue seed.
 
Attribution explain (ExplainerContext &ctx, const Tensor &input, const LRPTarget &target, DeviceBackend *backend) const
 Explains the given target(s) / contrast(s).
 
Tensor seed_relevance (ExplainerContext &ctx, const Tensor &input, const LRPTarget &target, DeviceBackend *backend, float *relevance_out_sum=nullptr) const
 The first half of explain(): runs the forward pass and builds the relevance it seeds at the output, shape (N, num_classes). With rule_configs(), this lets a caller run the relevance pass itself, for example in pieces (ExplainerContext::relevance_pass_through).
 
std::vector< LRPRuleConfig > rule_configs (const ExplainerContext &ctx) const
 The rule each of ctx's modules applies, in forward order, as explain() uses them.
 
const LRPRuleConfig & config () const
 The uniform config (default-constructed when this LRP uses a composite).
 
const LRPComposite & composite () const
 The composite, or an empty function for a uniform-rule LRP.
 

Static Public Member Functions

static const ExplainerCapabilities & capabilities ()
 What it answers and needs: its ExplainerCatalog() entry.
 
static LRP epsilon_plus (float epsilon=1e-6f)
 Zennit EpsilonPlus preset (lrp_composite::epsilon_plus).
 
static LRP epsilon_gamma_box (float low, float high, float gamma=0.25f, float epsilon=1e-6f)
 Zennit EpsilonGammaBox preset (lrp_composite::epsilon_gamma_box).
 
static LRP epsilon_alpha2_beta1 (float epsilon=1e-6f)
 Zennit EpsilonAlpha2Beta1 preset (lrp_composite::epsilon_alpha2_beta1).
 

Detailed Description

Whole-model LRP: runs the forward pass, seeds relevance at the chosen output(s), and propagates it to the input through every module's own propagate_relevance() rule.

Note
The per-layer rules are the ones each Module implements (see each module's propagate_relevance() doc); either one config is passed to all of them, or an LRPComposite chooses one per module. A module asked for a rule it does not implement throws (ExplainerContext::relevance_pass) – there is no silent fallback to epsilon. With LRPRuleConfig::epsilon_bias_in_denominator set, bias terms absorb relevance, and the AttnLRP softmax / attention rules do not conserve exactly, so sum(values) matches the seeded total only for conservative stacks. The Attribution's metadata reports both sums.
Device-generic: the seed is assembled on the host (one device->host copy of the network output when seeding with output values) and uploaded through backend, which must be the backend the network's output lives on.

Constructor & Destructor Documentation

◆ LRP() [1/2]

pulsatrix::LRP::LRP ( LRPRuleConfig  config = LRPRuleConfig{})
inlineexplicit

Uniform rule: every module applies config.

◆ LRP() [2/2]

pulsatrix::LRP::LRP ( LRPComposite  composite,
std::string  name = "custom" 
)
inlineexplicit

Per-layer rules from a composite; name is reported as "composite:<name>".

Exceptions
std::invalid_argumentif composite is empty.

Member Function Documentation

◆ capabilities()

static const ExplainerCapabilities & pulsatrix::LRP::capabilities ( )
inlinestatic

What it answers and needs: its ExplainerCatalog() entry.

◆ composite()

const LRPComposite & pulsatrix::LRP::composite ( ) const
inline

The composite, or an empty function for a uniform-rule LRP.

◆ config()

const LRPRuleConfig & pulsatrix::LRP::config ( ) const
inline

The uniform config (default-constructed when this LRP uses a composite).

◆ epsilon_alpha2_beta1()

static LRP pulsatrix::LRP::epsilon_alpha2_beta1 ( float  epsilon = 1e-6f)
inlinestatic

Zennit EpsilonAlpha2Beta1 preset (lrp_composite::epsilon_alpha2_beta1).

◆ epsilon_gamma_box()

static LRP pulsatrix::LRP::epsilon_gamma_box ( float  low,
float  high,
float  gamma = 0.25f,
float  epsilon = 1e-6f 
)
inlinestatic

Zennit EpsilonGammaBox preset (lrp_composite::epsilon_gamma_box).

◆ epsilon_plus()

static LRP pulsatrix::LRP::epsilon_plus ( float  epsilon = 1e-6f)
inlinestatic

Zennit EpsilonPlus preset (lrp_composite::epsilon_plus).

◆ explain() [1/2]

Attribution pulsatrix::LRP::explain ( ExplainerContext &  ctx,
const Tensor &  input,
const LRPTarget &  target,
DeviceBackend *  backend 
) const
inline

Explains the given target(s) / contrast(s).

Returns
Attribution{"lrp", relevance (input's shape), metadata}: rule (the uniform rule's name, or "composite:<name>"), rules (each module's rule, comma-separated, forward order), epsilon (the uniform config's), seed, targets, contrasts, relevance_out_sum, relevance_in_sum.
Exceptions
std::invalid_argumentif the network output isn't rank-2, a target/contrast list has neither 1 nor N entries, an index is out of range, or a row's contrast equals its target (that seed is all zeros and explains nothing), a module does not implement its rule, or a rule's parameters are invalid.
std::logic_errorif the context's last forward pass was patched (relevance_pass()).

◆ explain() [2/2]

Attribution pulsatrix::LRP::explain ( ExplainerContext &  ctx,
const Tensor &  input,
int64_t  target_index,
DeviceBackend *  backend 
) const
inline

Explains target_index for every row, OutputValue seed.

◆ rule_configs()

std::vector< LRPRuleConfig > pulsatrix::LRP::rule_configs ( const ExplainerContext &  ctx) const
inline

The rule each of ctx's modules applies, in forward order, as explain() uses them.

◆ seed_relevance()

Tensor pulsatrix::LRP::seed_relevance ( ExplainerContext &  ctx,
const Tensor &  input,
const LRPTarget &  target,
DeviceBackend *  backend,
float *  relevance_out_sum = nullptr 
) const
inline

The first half of explain(): runs the forward pass and builds the relevance it seeds at the output, shape (N, num_classes). With rule_configs(), this lets a caller run the relevance pass itself, for example in pieces (ExplainerContext::relevance_pass_through).

Parameters
relevance_out_sumIf not null, set to the seed's total.
Exceptions
std::invalid_argumentas explain() does for the output and the target.

The documentation for this class was generated from the following file: