-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
after multihead attention and projection from 3D to 2D, implemented a…
…n example for Attention weights which displays the strength of this Transformer key tech
- Loading branch information
1 parent
6fca8d9
commit 57765e1
Showing
5 changed files
with
64 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::attention::scaled_dot_attention::scaled_dot_product; | ||
use crate::attention::softmax::softmax_3d; | ||
use ndarray::{array, s, Array2}; | ||
|
||
pub fn example() { | ||
let words = vec!["The", "cat", "sat", "on", "the", "mat"]; | ||
let q = array![[ | ||
[1.0, 0.8, 0.6, 0.4, 0.2, 0.1], | ||
[0.8, 1.0, 0.9, 0.7, 0.3, 0.2], | ||
[0.6, 0.9, 1.0, 0.8, 0.5, 0.3], | ||
[0.4, 0.7, 0.8, 1.0, 0.7, 0.6], | ||
[0.2, 0.3, 0.5, 0.7, 1.0, 0.9], | ||
[0.1, 0.2, 0.3, 0.6, 0.9, 1.0] | ||
]]; | ||
|
||
let scores = scaled_dot_product(q.clone(), q.clone(), q.clone(), true); | ||
let sm_scores = softmax_3d(&scores); | ||
display_attention_weights(sm_scores.slice(s![0, .., ..]).to_owned(), &words); | ||
} | ||
fn display_attention_weights(scores: Array2<f32>, words: &[&str]) { | ||
println!("Attention Weights (Softmax Scores):\n"); | ||
|
||
// Print column headers | ||
print!("{:<6}", ""); // Empty corner for alignment | ||
for word in words { | ||
print!(" {:<5}", word); | ||
} | ||
println!(); // New line for clarity | ||
|
||
// Iterate through rows and display each with the corresponding word | ||
for (i, row) in scores.outer_iter().enumerate() { | ||
print!("{:<6}", words[i]); // Row label | ||
for &val in row.iter() { | ||
print!("{:<6.3}", val); // Print score with 3 decimal places | ||
} | ||
println!(); // New line after each row | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#![allow(non_snake_case)] | ||
|
||
pub mod example; | ||
mod settings; | ||
|
||
// this lint makes a scene .... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,7 @@ | ||
use ndarray::{array, s, Array2, Array3}; | ||
use Transformer::attention::scaled_dot_attention::scaled_dot_product; | ||
use Transformer::attention::softmax::softmax_3d; | ||
use Transformer::example::example; | ||
|
||
fn main() { | ||
println!("runs successfully!"); | ||
|
||
let a: Array3<f32> = array![[ | ||
[0.1, 0.2, 0.3], | ||
[0.4, 0.5, 0.6], | ||
[0.7, 0.8, 0.9], | ||
[1.0, 1.1, 1.2], | ||
[0.1, 0.2, 0.3], | ||
[1.3, 1.4, 1.5] | ||
]]; | ||
|
||
let scores = scaled_dot_product(a.clone(), a.clone(), a.clone(), true); | ||
let sm_scores = softmax_3d(&scores); | ||
// Words corresponding to the input | ||
let words = ["the", "cat", "sat", "on", "the", "mat"]; | ||
|
||
display_attention_weights(sm_scores.slice(s![0, .., ..]).to_owned(), &words); | ||
} | ||
fn display_attention_weights(scores: Array2<f32>, words: &[&str]) { | ||
println!("Attention Weights (Softmax Scores):\n"); | ||
|
||
// Print column headers | ||
print!("{:<6}", ""); // Empty corner for alignment | ||
for word in words { | ||
print!(" {:<5}", word); | ||
} | ||
println!(); // New line for clarity | ||
|
||
// Iterate through rows and display each with the corresponding word | ||
for (i, row) in scores.outer_iter().enumerate() { | ||
print!("{:<6}", words[i]); // Row label | ||
for &val in row.iter() { | ||
print!("{:<6.3}", val); // Print score with 3 decimal places | ||
} | ||
println!(); // New line after each row | ||
} | ||
example(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters