Skip to content

Interpretability, attribution, and explanation audits · Interpretability, explanations, and model-mechanism analysis · EXP012 · Architecture

ProtoPNet: prototypical part network

Classify an image using similarities between its latent patches and learned prototypes, making part-based evidence an explicit part of the computation.

Explained here Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Classify an image using similarities between its latent patches and learned prototypes, making part-based evidence an explicit part of the computation.

Question / hypotheses

Which learned prototype patches support the output, and do their visual exemplars accurately represent the latent comparisons?

Architecture / computational structure

A feature encoder maps images to latent patches. Prototype vectors are compared with every patch; a similarity pooling operation selects strong matches. A final layer combines prototype evidence. Training includes predictive and prototype-structure objectives, followed by projection to suitable training patches.

Assumptions

Latent distances and projected exemplar patches must correspond meaningfully to visual content. Image labels supervise the model, but interpretable part annotations are not required by the original construction. Background shortcuts can still appear in prototypes.

Formula / computational definition

dj(x)=min⁡z∈patches⁡(hθ(x))∥z−pj∥22,sj(x)=log⁡dj(x)+1dj(x)+ϵ,logits=Ws(x).d_j(x)=\min_{z\in\operatorname{patches}(h_\theta(x))}\|z-p_j\|_2^2,\qquad s_j(x)=\log\frac{d_j(x)+1}{d_j(x)+\epsilon},\qquad \mathrm{logits}=Ws(x).

Parameters and notation

Learned: encoder θ, prototypes p and final weights W. Choices include prototype count per class, latent dimension, patch size, clustering/separation weights, projection schedule, ε and last-layer regularization. Prototype count changes both capacity and explanation burden. Here h_theta is the encoder, z a latent patch, p_j the j-th learned prototype, d_j its nearest-patch squared distance, epsilon a positive stabilization constant, s the similarity vector, and W the final class-readout matrix.

Fitting, fine-tuning, and adaptation

Use staged optimization with explicit prototype projection and final-layer fitting as specified by the implementation. After encoder fine-tuning, re-evaluate/projection-update prototypes and their displayed exemplars. Retain independent test data and avoid presenting training exemplars as test evidence.

How to use and calibrate

Report predictive performance and prototype quality separately. Inspect whether projected patches are stable, class-relevant and free of shortcuts. Test spatial localization and counterexamples; a visually recognizable prototype alone does not validate the decision rule.

Example application

Build an image classifier whose explanations compare an observed patch with several stored training exemplars and show their weighted evidence.

Interpretation / pitfalls

Watch out

The nearest latent patch may have a misleading image-space visualization. Prototype reuse, background patterns and negative final-layer weights complicate a simple positive-evidence narrative. This is a trained architecture, not a post-hoc explanation of an arbitrary existing model.

Python and R code examples

Scope: Complete prototype-distance, similarity and linear-head calculation on supplied latent patches; no encoder/prototype training or image projection. Python: NumPy; R: base R.

Validation: Python executed successfully on the supplied fixture; R reviewed but unexecuted. Full implementations and supported operation details are provided in the linked primary/official sources.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Complete prototype-distance, similarity and linear-head calculation on supplied latent patches; no encoder/prototype training or image projection. Python: NumPy; R: base R. · needs numpy

import numpy as np
patch=np.array([[0.,0.],[1.,0.],[0.,2.]]);p=np.array([[.9,.1],[0.,1.8]])
d=((patch[:,None,:]-p[None,:,:])**2).sum(-1).min(0);s=np.log((d+1)/(d+1e-4))
W=np.array([[1.,-.2],[-.2,1.] ]);print(d,s,W@s)
assert np.all(d>=0)
R

Component demo, not a trained model — Complete prototype-distance, similarity and linear-head calculation on supplied latent patches; no encoder/prototype training or image projection. Python: NumPy; R: base R.

patch<-rbind(c(0,0),c(1,0),c(0,2));p<-rbind(c(.9,.1),c(0,1.8))
d<-vapply(1:nrow(p),function(j) min(rowSums(sweep(patch,2,p[j,],"-")^2)),numeric(1))
s<-log((d+1)/(d+1e-4));W<-rbind(c(1,-.2),c(-.2,1));print(d);print(s);print(drop(W%*%s))

Sources

From the CodeSplash reference, entry EXP012.