-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsimple_explainability_example.py
More file actions
326 lines (270 loc) · 12.5 KB
/
simple_explainability_example.py
File metadata and controls
326 lines (270 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""
Simple Explainability Example with ASCII Visualization
"""
import os
import sys
import warnings
import numpy as np
import torch
from pytorch_lightning import seed_everything
from torchTextClassifiers import ModelConfig, TrainingConfig, torchTextClassifiers
from torchTextClassifiers.tokenizers import WordPieceTokenizer
from torchTextClassifiers.utilities.plot_explainability import (
map_attributions_to_char,
map_attributions_to_word,
)
def main():
# Set seed for reproducibility
SEED = 42
# Set environment variables for full reproducibility
os.environ['PYTHONHASHSEED'] = str(SEED)
os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8'
# Use PyTorch Lightning's seed_everything for comprehensive seeding
seed_everything(SEED, workers=True)
# Make PyTorch operations deterministic
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.use_deterministic_algorithms(True, warn_only=True)
# Suppress PyTorch Lightning warnings for cleaner output
warnings.filterwarnings(
'ignore',
message='.*',
category=UserWarning,
module='pytorch_lightning'
)
print("🔍 Simple Explainability Example")
# Enhanced training data with more diverse examples
X_train = np.array([
# Positive examples
"I love this product",
"Great quality and excellent service",
"Amazing design and fantastic performance",
"Outstanding value for money",
"Excellent customer support team",
"Love the innovative features",
"Perfect solution for my needs",
"Highly recommend this item",
"Superb build quality",
"Wonderful experience overall",
"Great value and fast delivery",
"Excellent product with amazing results",
"Love this fantastic design",
"Perfect quality and great price",
"Amazing customer service experience",
# Negative examples
"This is terrible quality",
"Poor design and cheap materials",
"Awful experience with this product",
"Terrible customer service response",
"Completely disappointing purchase",
"Poor quality and overpriced item",
"Awful build quality issues",
"Terrible value for money",
"Disappointing performance results",
"Poor service and bad experience",
"Awful design and cheap feel",
"Terrible product with many issues",
"Disappointing quality and poor value",
"Bad experience with customer support",
"Poor construction and awful materials"
])
y_train = np.array([
# Positive labels (1)
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
# Negative labels (0)
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
])
X_val = np.array([
"Good product with decent quality",
"Bad quality and poor service",
"Excellent value and great design",
"Terrible experience and awful quality"
])
y_val = np.array([1, 0, 1, 0])
# Create and train tokenizer
print("\n🏗️ Creating and training WordPiece tokenizer...")
tokenizer = WordPieceTokenizer(vocab_size=5000, output_dim=128)
training_corpus = X_train.tolist()
tokenizer.train(training_corpus)
print("✅ Tokenizer trained successfully!")
# Create model configuration
print("\n🔧 Creating model configuration...")
model_config = ModelConfig(
embedding_dim=50,
num_classes=2
)
# Create classifier
print("\n🔨 Creating classifier...")
classifier = torchTextClassifiers(
tokenizer=tokenizer,
model_config=model_config
)
print("✅ Classifier created successfully!")
# Train the model
print("\n🎯 Training model...")
training_config = TrainingConfig(
num_epochs=25,
batch_size=8,
lr=1e-3,
patience_early_stopping=5,
num_workers=0,
trainer_params={'deterministic': True}
)
classifier.train(
X_train, y_train,
training_config=training_config,
X_val=X_val, y_val=y_val,
verbose=True
)
print("✅ Training completed!")
# Test examples with different sentiments
test_texts = [
"This product is amazing!",
"Poor quality and terrible service",
"Great value for money",
"Completely disappointing and awful experience",
"Love this excellent design"
]
print(f"\n🔍 Testing explainability on {len(test_texts)} examples:")
print("=" * 60)
for i, test_text in enumerate(test_texts, 1):
print(f"\n📝 Example {i}:")
print(f"Text: '{test_text}'")
# Get prediction with explainability
try:
result = classifier.predict(np.array([test_text]), top_k=1, explain=True)
# Extract prediction
prediction = result["prediction"][0][0].item()
confidence = result["confidence"][0][0].item()
print(f"Prediction: {'Positive' if prediction == 1 else 'Negative'} (confidence: {confidence:.4f})")
# Extract attributions and mapping info
attributions = result["attributions"][0][0] # shape: (seq_len,)
offset_mapping = result["offset_mapping"][0] # List of (start, end) tuples
word_ids = result["word_ids"][0] # List of word IDs for each token
# Map token-level attributions to character-level (for ASCII visualization)
char_attributions = map_attributions_to_char(
attributions.unsqueeze(0), # Add batch dimension: (1, seq_len)
offset_mapping,
test_text
)[0] # Get first result
print("\n📊 Character-Level Contribution Visualization:")
print("-" * 60)
# Create a simple ASCII visualization by character
max_attr = max(char_attributions) if len(char_attributions) > 0 else 1
bar_width = 40
# Group characters into words for better readability
words = test_text.split()
char_idx = 0
for word in words:
word_len = len(word)
# Get attributions for this word
word_attrs = char_attributions[char_idx:char_idx + word_len]
if len(word_attrs) > 0:
avg_attr = sum(word_attrs) / len(word_attrs)
bar_length = int((avg_attr / max_attr) * bar_width) if max_attr > 0 else 0
bar = "█" * bar_length
print(f"{word:>15} | {bar:<40} {avg_attr:.4f}")
char_idx += word_len + 1 # +1 for space
print("-" * 60)
# Show top contributing word
char_idx = 0
word_scores = []
for word in words:
word_len = len(word)
word_attrs = char_attributions[char_idx:char_idx + word_len]
if len(word_attrs) > 0:
word_scores.append((word, sum(word_attrs) / len(word_attrs)))
char_idx += word_len + 1
if word_scores:
top_word, top_score = max(word_scores, key=lambda x: x[1])
print(f"💡 Most influential word: '{top_word}' (avg score: {top_score:.4f})")
except Exception as e:
print(f"⚠️ Explainability failed: {e}")
import traceback
traceback.print_exc()
# Analysis completed for this example
print(f"✅ Analysis completed for example {i}")
print(f"\n🎉 Explainability analysis completed for {len(test_texts)} examples!")
# Interactive section for user input (only if --interactive flag is provided)
if "--interactive" in sys.argv:
print("\n" + "="*60)
print("🎯 Interactive Explainability Mode")
print("="*60)
print("Enter your own text to see predictions and explanations!")
print("Type 'quit' or 'exit' to end the session.\n")
while True:
try:
user_text = input("💬 Enter text: ").strip()
if user_text.lower() in ['quit', 'exit', 'q']:
print("👋 Thanks for using the explainability tool!")
break
if not user_text:
print("⚠️ Please enter some text.")
continue
print(f"\n🔍 Analyzing: '{user_text}'")
# Get prediction with explainability
try:
result = classifier.predict(np.array([user_text]), top_k=1, explain=True)
# Extract prediction
prediction = result["prediction"][0][0].item()
confidence = result["confidence"][0][0].item()
sentiment = "Positive" if prediction == 1 else "Negative"
print(f"🎯 Prediction: {sentiment} (confidence: {confidence:.4f})")
# Extract attributions and mapping info
attributions = result["attributions"][0][0] # shape: (seq_len,)
offset_mapping = result["offset_mapping"][0] # List of (start, end) tuples
word_ids = result["word_ids"][0] # List of word IDs for each token
# Map token-level attributions to character-level (for ASCII visualization)
char_attributions = map_attributions_to_char(
attributions.unsqueeze(0), # Add batch dimension: (1, seq_len)
offset_mapping,
user_text
)[0] # Get first result
print("\n📊 Character-Level Contribution Visualization:")
print("-" * 60)
# Create a simple ASCII visualization by character
max_attr = max(char_attributions) if len(char_attributions) > 0 else 1
bar_width = 40
# Group characters into words for better readability
words = user_text.split()
char_idx = 0
for word in words:
word_len = len(word)
# Get attributions for this word
word_attrs = char_attributions[char_idx:char_idx + word_len]
if len(word_attrs) > 0:
avg_attr = sum(word_attrs) / len(word_attrs)
bar_length = int((avg_attr / max_attr) * bar_width) if max_attr > 0 else 0
bar = "█" * bar_length
print(f"{word:>15} | {bar:<40} {avg_attr:.4f}")
char_idx += word_len + 1 # +1 for space
print("-" * 60)
# Show interpretation
char_idx = 0
word_scores = []
for word in words:
word_len = len(word)
word_attrs = char_attributions[char_idx:char_idx + word_len]
if len(word_attrs) > 0:
word_scores.append((word, sum(word_attrs) / len(word_attrs)))
char_idx += word_len + 1
if word_scores:
top_word, top_score = max(word_scores, key=lambda x: x[1])
print(f"💡 Most influential word: '{top_word}' (avg score: {top_score:.4f})")
except Exception as e:
print(f"⚠️ Explainability failed: {e}")
print("🔍 Prediction available, but detailed explanation unavailable.")
import traceback
traceback.print_exc()
print("\n" + "-"*50)
except KeyboardInterrupt:
print("\n👋 Session interrupted. Goodbye!")
break
except Exception as e:
print(f"⚠️ Error: {e}")
continue
else:
print("\n💡 Tip: Use --interactive flag to enter interactive mode for custom text analysis!")
print(" Example: uv run python examples/simple_explainability_example.py --interactive")
if __name__ == "__main__":
main()