pub struct AttentionVisualizer { /* private fields */ }Expand description
Attention pattern visualizer for transformer models
Implementations§
Source§impl AttentionVisualizer
impl AttentionVisualizer
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new attention visualizer
§Example
use trustformers_debug::AttentionVisualizer;
let visualizer = AttentionVisualizer::new();Sourcepub fn with_config(config: AttentionVisualizerConfig) -> Self
pub fn with_config(config: AttentionVisualizerConfig) -> Self
Create a new attention visualizer with custom configuration
Sourcepub fn set_token_vocab(&mut self, tokens: Vec<String>)
pub fn set_token_vocab(&mut self, tokens: Vec<String>)
Set token vocabulary for labeling
Sourcepub fn register(
&mut self,
layer_name: &str,
weights: Vec<Vec<Vec<f64>>>,
source_tokens: Vec<String>,
target_tokens: Vec<String>,
attention_type: AttentionType,
) -> Result<()>
pub fn register( &mut self, layer_name: &str, weights: Vec<Vec<Vec<f64>>>, source_tokens: Vec<String>, target_tokens: Vec<String>, attention_type: AttentionType, ) -> Result<()>
Register attention weights for a layer
§Arguments
layer_name- Name of the layerweights- Attention weights [num_heads, seq_len, seq_len]source_tokens- Source (query) tokenstarget_tokens- Target (key) tokensattention_type- Type of attention mechanism
§Example
let weights = vec![
vec![vec![0.5, 0.3, 0.2], vec![0.1, 0.6, 0.3], vec![0.2, 0.3, 0.5]]
];
let tokens = vec!["Hello".to_string(), "world".to_string(), "!".to_string()];
visualizer.register(
"layer.0.attention",
weights,
tokens.clone(),
tokens.clone(),
AttentionType::SelfAttention,
).unwrap();Sourcepub fn get_attention(&self, layer_name: &str) -> Option<&AttentionWeights>
pub fn get_attention(&self, layer_name: &str) -> Option<&AttentionWeights>
Get attention weights for a specific layer
Sourcepub fn create_heatmap(
&self,
layer_name: &str,
head: usize,
) -> Result<AttentionHeatmap>
pub fn create_heatmap( &self, layer_name: &str, head: usize, ) -> Result<AttentionHeatmap>
Create a heatmap for a specific head in a layer
Sourcepub fn analyze(&self, layer_name: &str) -> Result<AttentionAnalysis>
pub fn analyze(&self, layer_name: &str) -> Result<AttentionAnalysis>
Analyze attention patterns for a layer
Sourcepub fn plot_heatmap_ascii(
&self,
layer_name: &str,
head: usize,
) -> Result<String>
pub fn plot_heatmap_ascii( &self, layer_name: &str, head: usize, ) -> Result<String>
Plot attention heatmap as ASCII art
Sourcepub fn export_to_json(&self, layer_name: &str, output_path: &Path) -> Result<()>
pub fn export_to_json(&self, layer_name: &str, output_path: &Path) -> Result<()>
Export attention weights to JSON
Sourcepub fn export_to_bertviz(
&self,
layer_name: &str,
output_path: &Path,
) -> Result<()>
pub fn export_to_bertviz( &self, layer_name: &str, output_path: &Path, ) -> Result<()>
Export to BertViz-compatible format (HTML)
Sourcepub fn num_layers(&self) -> usize
pub fn num_layers(&self) -> usize
Get number of stored layers
Trait Implementations§
Source§impl Debug for AttentionVisualizer
impl Debug for AttentionVisualizer
Auto Trait Implementations§
impl Freeze for AttentionVisualizer
impl RefUnwindSafe for AttentionVisualizer
impl Send for AttentionVisualizer
impl Sync for AttentionVisualizer
impl Unpin for AttentionVisualizer
impl UnsafeUnpin for AttentionVisualizer
impl UnwindSafe for AttentionVisualizer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.