exampleSelfAttention function
void
exampleSelfAttention()
Implementation
void exampleSelfAttention() {
print("\n--- Example 1: SelfAttention Inspection ---");
final embedSize = 8;
final headSize = 4;
final sequenceLength = 3;
final sa = SelfAttention(embedSize, headSize, masked: false);
// Create dummy input sequence (e.g., 3 tokens, each with embedSize features)
final x = List.generate(
sequenceLength,
(i) => ValueVector.fromDoubleList(
List.generate(embedSize, (j) => Random().nextDouble())));
print(
"Input to SelfAttention (first token): ${x[0].values.map((v) => v.data.toStringAsFixed(2)).toList()}");
final output = sa.forward(x);
print(
"Output from SelfAttention (first token): ${output[0].values.map((v) => v.data.toStringAsFixed(2)).toList()}");
print("SelfAttention parameters count: ${sa.parameters().length}");
}