Spaces:
Running
Running
raymondEDS
commited on
Commit
Β·
f49f36a
1
Parent(s):
3af1be7
week 9
Browse files
Reference files/W9 - Deep Learning Learning Objectices.md
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
**Architectural Understanding**
|
2 |
+
|
3 |
+
1. **Define** deep learning as neural networks with multiple hidden layers that enable hierarchical feature learning (not just "3+ layers" but *why* depth matters for representation learning)
|
4 |
+
2. **Explain** why "deep" matters: each layer learns increasingly complex features
|
5 |
+
3. **Compare** how deep learning is different from machine learning, and be able to identify other deep learning methods
|
6 |
+
|
7 |
+
**When to Use Deep Learning**
|
8 |
+
|
9 |
+
4\. **Choose** deep learning when you have: large datasets, unstructured data (images/text), complex patterns. Also understand why pretrained models work and when to fine-tune vs. feature extraction
|
10 |
+
|
11 |
+
5\. **Avoid** deep learning when you need: explainable results, small datasets, or simple patterns
|
12 |
+
|
13 |
+
**Understanding BERT**
|
14 |
+
|
15 |
+
6\. **Explain** what BERT does: understands word meaning based on context (unlike older methods)
|
16 |
+
|
17 |
+
7\. **Understand** BERT training: pre-training with a massive dataset, masked language modeling, bidirectional learning and the transformer framework
|
18 |
+
|
19 |
+
8\. **Recognize** why pretrained models save time and work better than training from scratch
|
20 |
+
|
21 |
+
**Practical Implementation and Evaluation**
|
22 |
+
|
23 |
+
10\. **Implement** sentiment analysis using pretrained BERT via Hugging Face transformers
|
24 |
+
|
25 |
+
11\. **Evaluate** model performance using appropriate metrics for classification tasks
|
26 |
+
|
27 |
+
12\. **Interpret** the model's confidence scores and predictions
|
28 |
+
|
29 |
+
**Notes**
|
30 |
+
|
31 |
+
What is deep learning?
|
32 |
+
Video tutorial: [Link](https://www.youtube.com/watch?v=q6kJ71tEYqM)
|
33 |
+
|
34 |
+
- Previously we learned what machine learning is
|
35 |
+
- Deep learning is a subset of machine learning
|
36 |
+
- A subfield of AI is ML \-\> Neural Network \-\> Deep Learning
|
37 |
+
- More than three layers of neural network is considered deep neural network \-\> deep learning
|
38 |
+
- Can ingest unstructured data and determine \-\> different from supervised learning \-\> unsupervised learning
|
39 |
+
|
40 |
+
When to use
|
41 |
+
Video: [Link](https://www.youtube.com/watch?v=o3bWqPdWJ88)
|
42 |
+
|
43 |
+
- Unstructured data, like image, video, text
|
44 |
+
- High volumn of data \-\> deep learning will give you better result
|
45 |
+
- Complexity of feature \-\> complicated features \-\> deep learning
|
46 |
+
- Interpretability (important)
|
47 |
+
- Industries like healthcare and finance require high interpretability, which is better answered by statistical ML
|
48 |
+
- Deep learningβs complex neural networks makes it hard to interpret
|
49 |
+
|
50 |
+
BERT
|
51 |
+
|
52 |
+
- Google search is powered by BERT (bidirectional encoder representations from transformers)
|
53 |
+
- BERT base, BERT large
|
54 |
+
- If you have two homes, how can you say if there are similar
|
55 |
+
- For an object, if you can derive and compare features and compare their similaritiesβ¦take all the numbers and create vectors and compare the vectors, you can then compare
|
56 |
+
- Generate feature vector for these words \-\> compare feature vector/word embedding
|
57 |
+
- How to generate word embeddings
|
58 |
+
- Word to vector (word2vec)
|
59 |
+
- Issues with word2vec \-\> you need a model that can generate contextualized meaning of words \-\> this is what BERT allows you to do
|
60 |
+
|
61 |
+
Pretrained BERT for sentiment analysis
|
62 |
+
|
63 |
+
- Download and install Transformer from huggingface
|
64 |
+
- Install and import dependencies
|
65 |
+
- Instantiat model \- bert-base-multilingual-uncased-sentiment
|
66 |
+
- Perform sentiment scoring
|
67 |
+
- Encode and calculate sentiment
|
68 |
+
|
Reference files/W9-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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Using Deep Learning Model BERT to Understand Sentiments
|
2 |
+
|
3 |
+
## **π― Introduction**
|
4 |
+
|
5 |
+
**Your Mission**: You're a researcher studying student mental health. You have 1,000 anonymous student forum posts about academic stress, and you need to quickly identify which posts indicate concerning levels of burnout vs. normal academic stress. Manual coding would take weeks. Can you train an AI to do this accurately in under 2 hours?
|
6 |
+
|
7 |
+
**Real stakes**: Early intervention programs depend on identifying students at risk. Your analysis could help universities provide timely mental health support.
|
8 |
+
|
9 |
+
**The Dataset**: Student forum posts like:
|
10 |
+
|
11 |
+
* *"Another all-nighter for this impossible exam. I can't keep doing this."*
|
12 |
+
* *"Stressed about finals but my study group is keeping me motivated\!"*
|
13 |
+
* *"I honestly don't see the point anymore. Nothing I do matters."*
|
14 |
+
|
15 |
+
**Your Goal Today**: Learn how to use a BERT-powered sentiment classifier (a deep learning model) that can distinguish between healthy academic stress and concerning burnout indicators.
|
16 |
+
|
17 |
+
---
|
18 |
+
|
19 |
+
## **π€ Why Traditional ML Falls Short Here**
|
20 |
+
|
21 |
+
Let's start with what you already know. How would you approach this with logistic regression?
|
22 |
+
|
23 |
+
### **Quick Exercise: Traditional Approach Limitations**
|
24 |
+
|
25 |
+
python
|
26 |
+
sample\_posts \= \[
|
27 |
+
"I'm not stressed about finals", *\# Negation*
|
28 |
+
"This is fine, totally fine", *\# Sarcasm*
|
29 |
+
"Actually excited about exams" *\# Context-dependent*
|
30 |
+
\]
|
31 |
+
|
32 |
+
*\# Traditional ML approach:*
|
33 |
+
*\# 1\. Count words: {"stressed": 1, "finals": 1, "not": 1}*
|
34 |
+
*\# 2\. Each word gets independent weight*
|
35 |
+
|
36 |
+
*\# 3\. Sum weights β prediction*
|
37 |
+
|
38 |
+
**The Problem**: What happens when context changes everything?
|
39 |
+
|
40 |
+
* *"I'm **not** stressed"* vs *"I'm stressed"*
|
41 |
+
* *"This is **fine**"* (sarcastic) vs *"This is **fine**"* (genuine)
|
42 |
+
|
43 |
+
Traditional ML treats each word independently \- it can't understand that surrounding words completely change the meaning.
|
44 |
+
|
45 |
+
**π‘ We need something smarter**: A model that reads like humans do, understanding full context and subtle meanings.
|
46 |
+
|
47 |
+
---
|
48 |
+
|
49 |
+
## **π Meet BERT: Your Game-Changer**
|
50 |
+
|
51 |
+
### **What is BERT?**
|
52 |
+
|
53 |
+
**BERT** \= **B**idirectional **E**ncoder **R**epresentations from **T**ransformers
|
54 |
+
|
55 |
+
Think of BERT as an AI that learned to read by studying 3.3 billion words from books and Wikipedia, then can be quickly adapted to understand your specific research domain.
|
56 |
+
|
57 |
+
### **BERT's Superpower: Context Understanding**
|
58 |
+
|
59 |
+
**Traditional models** read like this: "I went to the \_\_\_" β
|
60 |
+
|
61 |
+
* Only look backwards, miss crucial context
|
62 |
+
|
63 |
+
**BERT reads bidirectionally**: β "I went to the \_\_\_ to deposit money" β
|
64 |
+
|
65 |
+
* Sees BOTH directions \= understands "bank" means financial institution, not riverbank
|
66 |
+
|
67 |
+
### **Let's See BERT in Action**
|
68 |
+
|
69 |
+
Python
|
70 |
+
*\# Install Transformers*
|
71 |
+
\!pip install transformers
|
72 |
+
|
73 |
+
*\# Try out BERT*
|
74 |
+
from transformers import pipeline
|
75 |
+
unmasker \= pipeline('fill-mask', model='bert-base-uncased')
|
76 |
+
unmasker("Artificial Intelligence \[MASK\] take over the world.")
|
77 |
+
|
78 |
+
*\# Be aware of model bias, and letβs see what jobs BERT suggests for a man*
|
79 |
+
|
80 |
+
unmasker("The man worked as a \[MASK\].")
|
81 |
+
|
82 |
+
*\# Now letβs see what jobs BERT suggests for a woman*
|
83 |
+
|
84 |
+
unmasker("The woman worked as a \[MASK\].")
|
85 |
+
|
86 |
+
**π€― Notice**: These gender biases are so important to be aware of and will affect all fine-tuned versions of this model.
|
87 |
+
|
88 |
+
### **Understanding Deep Learning & BERT's Place in the AI Family**
|
89 |
+
|
90 |
+
## **π§ Understanding BERT: The AI Revolution for Language (35 minutes)**
|
91 |
+
|
92 |
+
### **Essential Terminology: Building Your AI Vocabulary**
|
93 |
+
|
94 |
+
Before we dive into BERT, let's define the key terms you'll need:
|
95 |
+
|
96 |
+
**π Key Definition: Neural Network** A computer system inspired by how brain neurons work \- information flows through connected layers that learn patterns from data.
|
97 |
+
|
98 |
+
**π Key Definition: Deep Learning** Neural networks with **multiple hidden layers** (typically 3+) that automatically learn increasingly complex patterns:
|
99 |
+
|
100 |
+
* Layer 1: Basic patterns (individual words, simple features)
|
101 |
+
* Layer 2: Combinations (phrases, word relationships)
|
102 |
+
* Layer 3: Structure (grammar, syntax)
|
103 |
+
* Layer 4+: Meaning (context, sentiment, intent)
|
104 |
+
|
105 |
+
**π Key Definition: Transformers** A revolutionary neural network architecture (2017) that uses **attention mechanisms** to understand relationships between all words in a sentence simultaneously, rather than reading word-by-word.
|
106 |
+
|
107 |
+
**π Key Definition: BERT** **B**idirectional **E**ncoder **R**epresentations from **T**ransformers \- a specific type of transformer that reads text in both directions to understand context.
|
108 |
+
|
109 |
+
### **The AI Family Tree: Where BERT Lives**
|
110 |
+
|
111 |
+
Machine Learning Family
|
112 |
+
βββ Traditional ML (what you know)
|
113 |
+
β βββ Logistic Regression β You learned this
|
114 |
+
β βββ Random Forest
|
115 |
+
β βββ SVM
|
116 |
+
βββ Deep Learning (neural networks with many layers)
|
117 |
+
βββ Computer Vision
|
118 |
+
β βββ CNNs (for images)
|
119 |
+
βββ Sequential Data
|
120 |
+
β βββ RNNs/LSTMs (for time series)
|
121 |
+
βββ Language Understanding β BERT lives here\!
|
122 |
+
βββ BERT (2018) β What we're using today
|
123 |
+
βββ GPT (generates text)
|
124 |
+
|
125 |
+
βββ T5 (text-to-text)
|
126 |
+
|
127 |
+
### **Let's See BERT in Action First**
|
128 |
+
|
129 |
+
python
|
130 |
+
|
131 |
+
from transformers import pipeline
|
132 |
+
|
133 |
+
*\# Load BERT-based sentiment analyzer (pre-trained and ready\!)*
|
134 |
+
classifier \= pipeline("sentiment-analysis",
|
135 |
+
model\="cardiffnlp/twitter-roberta-base-sentiment-latest")
|
136 |
+
|
137 |
+
*\# Test with our tricky examples*
|
138 |
+
test\_posts \= \[
|
139 |
+
"I'm totally fine with staying up all night again", *\# Sarcasm?*
|
140 |
+
"Not feeling overwhelmed at all", *\# Negation \+ sarcasm?*
|
141 |
+
"This workload is completely manageable", *\# Hidden stress?*
|
142 |
+
"Actually excited about this challenging semester" *\# Genuine positive?*
|
143 |
+
\]
|
144 |
+
|
145 |
+
print("π§ BERT's Understanding:")
|
146 |
+
for post in test\_posts:
|
147 |
+
result \= classifier(post)
|
148 |
+
print(f"'{post}'")
|
149 |
+
print(f"β {result\['label'\]} (confidence: {result\['score'\]:.2f})")
|
150 |
+
|
151 |
+
print()
|
152 |
+
|
153 |
+
**π€― Notice**: BERT catches subtleties that word counting completely misses\! But how?
|
154 |
+
|
155 |
+
---
|
156 |
+
|
157 |
+
## **βοΈ How BERT Works**
|
158 |
+
|
159 |
+
### **BERT's Revolutionary Training: Two Clever Learning Tasks**
|
160 |
+
|
161 |
+
BERT learned language by reading **3.3 billion words** from Wikipedia (\~2.5B words) and Google's BooksCorpus (\~800M words) using two ingenious tasks:
|
162 |
+
|
163 |
+
#### **Task 1: Masked Language Modeling (MLM) \- The Fill-in-the-Blank Game**
|
164 |
+
|
165 |
+
**π Key Definition: Masked Language Modeling** A training method where random words are hidden (\[MASK\]) and the model learns to predict them using context from BOTH sides.
|
166 |
+
|
167 |
+
**Real Example from Training:**
|
168 |
+
|
169 |
+
* Original text: "The student felt anxious about the upcoming final exam"
|
170 |
+
* BERT saw: "The student felt \[MASK\] about the upcoming final exam"
|
171 |
+
* BERT learned: What word fits here based on ALL surrounding context?
|
172 |
+
*
|
173 |
+
* Possible answers: anxious, excited, confident, nervous, prepared...
|
174 |
+
BERT's choice: "anxious" (most likely given context)
|
175 |
+
|
176 |
+
**Why This Matters**: This forces **bidirectional learning** \- BERT must use words from BOTH left and right to make predictions.
|
177 |
+
|
178 |
+
**π Key Definition: Bidirectional Learning** Reading text in both directions (β β) simultaneously, unlike traditional models that only read left-to-right (β).
|
179 |
+
|
180 |
+
**Human Connection**: You do this naturally\! If someone said: *"Dang\! I'm out fishing and a huge trout just \_\_\_\_\_ my line\!"* You use words from BOTH sides ("fishing" \+ "trout" \+ "line") to predict "broke"\!
|
181 |
+
|
182 |
+
#### **Task 2: Next Sentence Prediction (NSP) \- Learning Text Relationships**
|
183 |
+
|
184 |
+
**π Key Definition: Next Sentence Prediction** A training task where BERT learns whether two sentences logically belong together.
|
185 |
+
|
186 |
+
**Training Examples:**
|
187 |
+
|
188 |
+
* β
Correct pair:
|
189 |
+
* Sentence A: "Paul went shopping"
|
190 |
+
* Sentence B: "He bought a new shirt"
|
191 |
+
*
|
192 |
+
* β Incorrect pair:
|
193 |
+
* Sentence A: "Ramona made coffee"
|
194 |
+
Sentence B: "Vanilla ice cream cones for sale"
|
195 |
+
|
196 |
+
**Why This Matters**: BERT learns relationships between ideas, not just individual words.
|
197 |
+
|
198 |
+
### **The Attention Mechanism: BERT's Superpower**
|
199 |
+
|
200 |
+
**π Key Definition: Attention Mechanism** A way for the model to automatically focus on the most important words when understanding each part of a sentence.
|
201 |
+
|
202 |
+
**Human Analogy**: When you read *"The bank by the river"*, you automatically know "bank" means riverbank (not a financial institution) because you pay attention to "river" \- even though it comes after "bank".
|
203 |
+
|
204 |
+
**BERT's Attention in Action:**
|
205 |
+
|
206 |
+
* Sentence: "I'm not stressed about finals"
|
207 |
+
*
|
208 |
+
* BERT's attention weights might look like:
|
209 |
+
* "I'm" β pays attention to: "not", "stressed" (who is feeling this?)
|
210 |
+
* "not" β pays attention to: "stressed" (what am I negating?)
|
211 |
+
* "stressed"β pays attention to: "not", "about", "finals" (context of stress)
|
212 |
+
* "about" β pays attention to: "stressed", "finals" (relationship)
|
213 |
+
"finals" β pays attention to: "stressed", "about" (source of stress)
|
214 |
+
|
215 |
+
This simultaneous analysis of ALL word relationships is what makes BERT so powerful\!
|
216 |
+
|
217 |
+
### **BERT's Architecture: The Technical Breakdown**
|
218 |
+
|
219 |
+
**π Key Definition: Encoder Architecture** BERT uses only the "encoder" part of transformers \- the part that builds understanding of input text (as opposed to generating new text).
|
220 |
+
|
221 |
+
**BERT's Processing Pipeline:**
|
222 |
+
|
223 |
+
#### **Step 1: Tokenization**
|
224 |
+
|
225 |
+
**π Key Definition: Tokenization** Breaking text into smaller pieces (tokens) that the model can process.
|
226 |
+
|
227 |
+
* Input: "I'm not stressed about finals"
|
228 |
+
* Tokens: \["I'm", "not", "stressed", "about", "finals"\]
|
229 |
+
* Special tokens added: \[CLS\] I'm not stressed about finals \[SEP\]
|
230 |
+
* β β
|
231 |
+
Start token End token
|
232 |
+
|
233 |
+
#### **Step 2: The Transformer Stack (12 Layers Working Together)**
|
234 |
+
|
235 |
+
* Input Tokens
|
236 |
+
* β
|
237 |
+
* Layer 1-3: Basic Language Understanding
|
238 |
+
* βββ Word recognition and basic patterns
|
239 |
+
* βββ Part-of-speech identification (noun, verb, adjective)
|
240 |
+
* βββ Simple word relationships
|
241 |
+
* β
|
242 |
+
* Layer 4-6: Phrase and Structure Analysis
|
243 |
+
* βββ Multi-word phrases ("not stressed")
|
244 |
+
* βββ Sentence structure and grammar
|
245 |
+
* βββ Syntactic relationships
|
246 |
+
* β
|
247 |
+
* Layer 7-9: Contextual Understanding
|
248 |
+
* βββ Semantic meaning in context
|
249 |
+
* βββ Negation and modifiers
|
250 |
+
* βββ Domain-specific patterns
|
251 |
+
* β
|
252 |
+
* Layer 10-12: High-Level Interpretation
|
253 |
+
* βββ Emotional tone and sentiment
|
254 |
+
* βββ Implied meaning and subtext
|
255 |
+
* βββ Task-specific reasoning
|
256 |
+
* β
|
257 |
+
Final Classification
|
258 |
+
|
259 |
+
#### **Step 3: Attention Across All Layers**
|
260 |
+
|
261 |
+
Each layer has **multiple attention heads** (typically 12\) that focus on different aspects:
|
262 |
+
|
263 |
+
* Head 1: Subject-verb relationships
|
264 |
+
* Head 2: Negation patterns
|
265 |
+
* Head 3: Emotional indicators
|
266 |
+
* Head 4: Academic context clues
|
267 |
+
* etc.
|
268 |
+
|
269 |
+
**Visualization of Attention:**
|
270 |
+
|
271 |
+
* "I'm not stressed about finals"
|
272 |
+
* Layer 6 attention patterns:
|
273 |
+
* I'm ββ not, stressed (personal ownership of feeling)
|
274 |
+
* not ββ stressed (direct negation)
|
275 |
+
* stressed ββ about, finals (source and type of stress)
|
276 |
+
* about ββ finals (relationship)
|
277 |
+
finals ββ stressed (academic stressor)
|
278 |
+
|
279 |
+
### **BERT's Two-Stage Learning Process**
|
280 |
+
|
281 |
+
#### **Stage 1: Pre-training (Done for You\!)**
|
282 |
+
|
283 |
+
* **Data**: 3.3 billion words (Wikipedia \+ Google Books)
|
284 |
+
* **Time**: 4 days on 64 specialized processors (TPUs)
|
285 |
+
* **Cost**: \~$10,000+ in computing resources
|
286 |
+
* **Tasks**: 50% Masked Language Modeling \+ 50% Next Sentence Prediction
|
287 |
+
* **Result**: General language understanding
|
288 |
+
|
289 |
+
#### **Stage 2: Fine-tuning (What You Can Do\!)**
|
290 |
+
|
291 |
+
* **Data**: Small labeled dataset for your specific task (e.g., sentiment analysis)
|
292 |
+
* **Time**: Minutes to hours
|
293 |
+
* **Cost**: Often free or \<$10
|
294 |
+
* **Process**: Adapt general language knowledge to your research question
|
295 |
+
* **Result**: Specialized classifier for your domain
|
296 |
+
|
297 |
+
**π Key Definition: Transfer Learning** Using knowledge learned from one task (reading billions of words) to help with a different task (your research question).
|
298 |
+
|
299 |
+
**Analogy**: Like a medical student (general education) becoming a psychiatrist (specialization) \- they don't relearn biology, they build on it.
|
300 |
+
|
301 |
+
### **Why BERT's Architecture Matters**
|
302 |
+
|
303 |
+
**Before BERT (Traditional Approaches):**
|
304 |
+
|
305 |
+
* Read text sequentially (leftβright)
|
306 |
+
* Each word processed independently
|
307 |
+
* Limited context understanding
|
308 |
+
* Required large labeled datasets for each task
|
309 |
+
|
310 |
+
**With BERT (Transformer Approach):**
|
311 |
+
|
312 |
+
* Read text bidirectionally (ββ)
|
313 |
+
* All words processed simultaneously with attention
|
314 |
+
* Rich contextual understanding
|
315 |
+
* Transfer learning from massive pre-training
|
316 |
+
|
317 |
+
**Research Impact:**
|
318 |
+
|
319 |
+
* **Speed**: Tasks that took weeks now take hours
|
320 |
+
* **Accuracy**: Often exceeds human-level performance
|
321 |
+
* **Scale**: Can process thousands of texts consistently
|
322 |
+
* **Accessibility**: No need for massive computing resources
|
323 |
+
|
324 |
+
---
|
325 |
+
|
326 |
+
## **π Building Your Burnout Detector (40 minutes)**
|
327 |
+
|
328 |
+
### **Step 1: Load Your Research Dataset**
|
329 |
+
|
330 |
+
python
|
331 |
+
import pandas as pd
|
332 |
+
import matplotlib.pyplot as plt
|
333 |
+
|
334 |
+
*\# Simulated student forum posts (anonymized and ethically sourced)*
|
335 |
+
student\_posts \= \[
|
336 |
+
*\# Concerning burnout indicators*
|
337 |
+
"I can't sleep, can't eat, nothing feels worth it anymore",
|
338 |
+
"Every assignment feels impossible, I'm failing at everything",
|
339 |
+
"Been crying in the library again, maybe I should just drop out",
|
340 |
+
"Three months of this and I feel completely empty inside",
|
341 |
+
|
342 |
+
*\# Normal academic stress*
|
343 |
+
"Finals week is rough but I know I can push through",
|
344 |
+
"Stressed about my paper but my friends are helping me stay motivated",
|
345 |
+
"Long study session today but feeling prepared for tomorrow's exam",
|
346 |
+
"Challenging semester but learning so much in my research methods class",
|
347 |
+
|
348 |
+
*\# Positive academic engagement*
|
349 |
+
"Actually excited about my thesis research this semester",
|
350 |
+
"Difficult coursework but my professor's support makes it manageable",
|
351 |
+
"Study group tonight \- we're all helping each other succeed",
|
352 |
+
"Tough week but grateful for this learning opportunity"
|
353 |
+
\]
|
354 |
+
|
355 |
+
*\# True labels (in real research, this would come from expert coding)*
|
356 |
+
labels \= \['negative', 'negative', 'negative', 'negative', *\# Burnout indicators*
|
357 |
+
'neutral', 'neutral', 'neutral', 'neutral', *\# Normal stress*
|
358 |
+
'positive', 'positive', 'positive', 'positive'\] *\# Positive engagement*
|
359 |
+
|
360 |
+
*\# Create DataFrame*
|
361 |
+
df \= pd.DataFrame({
|
362 |
+
'post': student\_posts,
|
363 |
+
'true\_sentiment': labels
|
364 |
+
})
|
365 |
+
|
366 |
+
print(f"π Dataset: {len(df)} student posts")
|
367 |
+
|
368 |
+
print(f"Distribution: {df\['true\_sentiment'\].value\_counts()}")
|
369 |
+
|
370 |
+
### **Step 2: Apply BERT to Your Research Question**
|
371 |
+
|
372 |
+
python
|
373 |
+
*\# Initialize BERT sentiment classifier*
|
374 |
+
sentiment\_classifier \= pipeline("sentiment-analysis",
|
375 |
+
model\="cardiffnlp/twitter-roberta-base-sentiment-latest")
|
376 |
+
|
377 |
+
*\# Analyze all posts*
|
378 |
+
predictions \= \[\]
|
379 |
+
confidence\_scores \= \[\]
|
380 |
+
|
381 |
+
print("π BERT's Analysis of Student Posts:\\n")
|
382 |
+
print("-" \* 80)
|
383 |
+
|
384 |
+
for i, post in enumerate(df\['post'\]):
|
385 |
+
result \= sentiment\_classifier(post)\[0\]
|
386 |
+
|
387 |
+
predictions.append(result\['label'\])
|
388 |
+
confidence\_scores.append(result\['score'\])
|
389 |
+
|
390 |
+
print(f"Post {i\+1}: '{post\[:50\]}...'")
|
391 |
+
print(f"True sentiment: {df\['true\_sentiment'\]\[i\]}")
|
392 |
+
print(f"BERT prediction: {result\['label'\]} (confidence: {result\['score'\]:.2f})")
|
393 |
+
print("-" \* 80)
|
394 |
+
|
395 |
+
*\# Add predictions to dataframe*
|
396 |
+
df\['bert\_prediction'\] \= predictions
|
397 |
+
|
398 |
+
df\['confidence'\] \= confidence\_scores
|
399 |
+
|
400 |
+
### **Step 3: Evaluate Your Model's Research Utility**
|
401 |
+
|
402 |
+
python
|
403 |
+
from sklearn.metrics import classification\_report, confusion\_matrix
|
404 |
+
import seaborn as sns
|
405 |
+
|
406 |
+
*\# Convert labels for comparison (handling label mismatches)*
|
407 |
+
def map\_labels(label):
|
408 |
+
if label in \['NEGATIVE', 'negative'\]:
|
409 |
+
return 'negative'
|
410 |
+
elif label in \['POSITIVE', 'positive'\]:
|
411 |
+
return 'positive'
|
412 |
+
else:
|
413 |
+
return 'neutral'
|
414 |
+
|
415 |
+
df\['bert\_mapped'\] \= df\['bert\_prediction'\].apply(map\_labels)
|
416 |
+
|
417 |
+
*\# Calculate accuracy*
|
418 |
+
accuracy \= (df\['true\_sentiment'\] \== df\['bert\_mapped'\]).mean()
|
419 |
+
print(f"π― Research Accuracy: {accuracy:.2f} ({accuracy\*100:.0f}%)")
|
420 |
+
|
421 |
+
*\# Detailed analysis*
|
422 |
+
print("\\nDetailed Performance Report:")
|
423 |
+
print(classification\_report(df\['true\_sentiment'\], df\['bert\_mapped'\]))
|
424 |
+
|
425 |
+
*\# Visualize results*
|
426 |
+
plt.figure(figsize\=(12, 4))
|
427 |
+
|
428 |
+
*\# Confusion Matrix*
|
429 |
+
plt.subplot(1, 2, 1)
|
430 |
+
cm \= confusion\_matrix(df\['true\_sentiment'\], df\['bert\_mapped'\])
|
431 |
+
sns.heatmap(cm, annot\=True, fmt\='d', cmap\='Blues',
|
432 |
+
xticklabels\=\['negative', 'neutral', 'positive'\],
|
433 |
+
yticklabels\=\['negative', 'neutral', 'positive'\])
|
434 |
+
plt.title('Confusion Matrix')
|
435 |
+
plt.ylabel('True Sentiment')
|
436 |
+
plt.xlabel('BERT Prediction')
|
437 |
+
|
438 |
+
*\# Confidence Distribution*
|
439 |
+
plt.subplot(1, 2, 2)
|
440 |
+
plt.hist(df\['confidence'\], bins\=10, alpha\=0.7, edgecolor\='black')
|
441 |
+
plt.title('BERT Confidence Scores')
|
442 |
+
plt.xlabel('Confidence')
|
443 |
+
plt.ylabel('Number of Posts')
|
444 |
+
|
445 |
+
plt.tight\_layout()
|
446 |
+
|
447 |
+
plt.show()
|
448 |
+
|
449 |
+
### **Step 4: Understanding Why BERT's "Depth" Matters**
|
450 |
+
|
451 |
+
python
|
452 |
+
*\# Let's see how BERT's deep learning architecture helps with complex cases*
|
453 |
+
complex\_cases \= \[
|
454 |
+
"I'm fine :) everything's totally under control :) :)", *\# Excessive positivity*
|
455 |
+
"lol guess I'm just built different, thriving on 2hrs sleep", *\# Normalized concerning behavior*
|
456 |
+
"Not that I'm complaining, but this workload is killing me", *\# Mixed signals*
|
457 |
+
\]
|
458 |
+
|
459 |
+
print("π§ Why Deep Learning Architecture Matters:")
|
460 |
+
print("(Multiple layers help BERT understand these complex patterns)\\n")
|
461 |
+
|
462 |
+
for case in complex\_cases:
|
463 |
+
result \= sentiment\_classifier(case)\[0\]
|
464 |
+
print(f"Text: '{case}'")
|
465 |
+
print(f"BERT's analysis: {result\['label'\]} (confidence: {result\['score'\]:.2f})")
|
466 |
+
|
467 |
+
*\# Explain what BERT's layers might be "thinking"*
|
468 |
+
print("π What BERT's layers likely detected:")
|
469 |
+
if "fine" in case and ":)" in case:
|
470 |
+
print(" β Layer 1-3: Words 'fine', positive emoticons")
|
471 |
+
print(" β Layer 4-8: Excessive repetition pattern")
|
472 |
+
print(" β Layer 9-12: Contradiction between words and overuse β sarcasm/masking")
|
473 |
+
elif "lol" in case and "thriving" in case:
|
474 |
+
print(" β Layer 1-3: Casual language ('lol'), positive word ('thriving')")
|
475 |
+
print(" β Layer 4-8: Contradiction with concerning behavior ('2hrs sleep')")
|
476 |
+
print(" β Layer 9-12: Normalization of unhealthy patterns")
|
477 |
+
elif "Not that I'm complaining" in case:
|
478 |
+
print(" β Layer 1-3: Negation words, formal disclaimer")
|
479 |
+
print(" β Layer 4-8: 'but' indicates contradiction coming")
|
480 |
+
print(" β Layer 9-12: Strong negative metaphor contradicts disclaimer")
|
481 |
+
|
482 |
+
print()
|
483 |
+
|
484 |
+
---
|
485 |
+
|
486 |
+
## **π€ Critical Research Reflection**
|
487 |
+
|
488 |
+
### **What Makes BERT Powerful for Research?**
|
489 |
+
|
490 |
+
**β
Advantages of Deep Learning Approach:**
|
491 |
+
|
492 |
+
* **Context awareness**: Understands negation, sarcasm, implied meaning
|
493 |
+
* **Consistency**: Same sophisticated analysis applied to every text
|
494 |
+
* **Scale**: Can process thousands of texts in minutes
|
495 |
+
* **Transferability**: Pre-trained on massive data, works across domains
|
496 |
+
|
497 |
+
### **Research Limitations to Consider**
|
498 |
+
|
499 |
+
python
|
500 |
+
*\# Test edge cases that might appear in real research*
|
501 |
+
edge\_cases \= \[
|
502 |
+
"tbh everything's mid rn but like whatever", *\# Generation-specific slang*
|
503 |
+
"Academic stress? What's that? \*nervous laughter\*", *\# Asterisk actions*
|
504 |
+
"Everything is absolutely perfect and wonderful\!\!", *\# Potential masking*
|
505 |
+
\]
|
506 |
+
|
507 |
+
print("π§ Testing BERT's Limitations:")
|
508 |
+
for case in edge\_cases:
|
509 |
+
result \= sentiment\_classifier(case)\[0\]
|
510 |
+
print(f"'{case}'")
|
511 |
+
print(f"β {result\['label'\]} (confidence: {result\['score'\]:.2f})")
|
512 |
+
|
513 |
+
print("β Would you trust this for research decisions?\\n")
|
514 |
+
|
515 |
+
### **When to Choose Deep Learning vs. Traditional ML**
|
516 |
+
|
517 |
+
**Use BERT (Deep Learning) when:**
|
518 |
+
|
519 |
+
* β
Context and nuance matter (like sentiment analysis)
|
520 |
+
* β
You have unstructured text data
|
521 |
+
* β
Traditional ML struggles with the complexity
|
522 |
+
* β
You need to scale to large datasets
|
523 |
+
|
524 |
+
**Stick with Traditional ML when:**
|
525 |
+
|
526 |
+
* β
You need perfect explainability
|
527 |
+
* β
Simple patterns work well
|
528 |
+
* β
Very small datasets (\<100 examples)
|
529 |
+
* β
Computational resources are limited
|
530 |
+
|
531 |
+
### **Research Ethics Considerations**
|
532 |
+
|
533 |
+
**Discussion Questions**:
|
534 |
+
|
535 |
+
1. **Bias**: Does BERT work equally well for all student populations?
|
536 |
+
2. **Privacy**: How do we protect student anonymity?
|
537 |
+
3. **Intervention**: What's our responsibility with concerning content?
|
538 |
+
4. **Validation**: How do we verify our ground truth labels?
|
539 |
+
|
540 |
+
---
|
541 |
+
|
542 |
+
## **π― Your Research Takeaways**
|
543 |
+
|
544 |
+
### **What You've Accomplished Today**
|
545 |
+
|
546 |
+
β
**Applied deep learning** to real research
|
547 |
+
β
**Used BERT** for context-aware text analysis
|
548 |
+
β
**Understood how deep learning differs** from traditional ML
|
549 |
+
β
**Evaluated performance** with research-appropriate metrics
|
550 |
+
β
**Identified when to use** deep learning vs. traditional approaches
|
551 |
+
|
552 |
+
### **Your Expanded Research Toolkit**
|
553 |
+
|
554 |
+
* **BERT sentiment analysis** for sophisticated text classification
|
555 |
+
* **Deep learning intuition** for understanding when context matters
|
556 |
+
* **Performance evaluation** skills for any ML research
|
557 |
+
* **Critical thinking** about AI limitations and research ethics
|
558 |
+
|
559 |
+
### **Next Steps for Your Research**
|
560 |
+
|
561 |
+
1. **Try BERT on your domain**: What research question could this solve?
|
562 |
+
2. **Collect larger datasets** (100+ examples for robust results)
|
563 |
+
3. **Consider fine-tuning** for domain-specific language
|
564 |
+
4. **Always validate** with domain experts
|
565 |
+
5. **Test for bias** across different populations
|
566 |
+
|
567 |
+
### **The Bigger Picture**
|
568 |
+
|
569 |
+
You've just learned to use one of the most powerful tools in modern AI research. BERT and similar deep learning models are transforming research across:
|
570 |
+
|
571 |
+
* **Psychology**: Mental health monitoring, personality analysis
|
572 |
+
* **Political Science**: Public opinion tracking, policy sentiment
|
573 |
+
* **Digital Humanities**: Literary analysis, historical text mining
|
574 |
+
* **Marketing**: Brand perception, customer feedback analysis
|
575 |
+
|
576 |
+
**π Challenge**: Apply this to a research question in your field this week\!
|
577 |
+
|
578 |
+
---
|
579 |
+
|
580 |
+
## **π Take-Home Exercise**
|
581 |
+
|
582 |
+
**Choose Your Research Adventure:**
|
583 |
+
|
584 |
+
1. **Social Media Analysis**: Sentiment about a current campus issue
|
585 |
+
2. **Literature Research**: Compare emotional tone across different authors
|
586 |
+
3. **Survey Analysis**: Classify open-ended course feedback
|
587 |
+
|
588 |
+
**Requirements:**
|
589 |
+
|
590 |
+
* Use BERT pipeline from today
|
591 |
+
* Analyze 20+ text samples
|
592 |
+
* Evaluate results critically
|
593 |
+
* Identify cases needing expert review
|
594 |
+
|
595 |
+
**Reflection Questions:**
|
596 |
+
|
597 |
+
* When did BERT's deep learning approach outperform what simple word counting could do?
|
598 |
+
* Where would you still need human expert judgment?
|
599 |
+
* How could this scale your research capabilities?
|
600 |
+
|
__pycache__/main.cpython-311.pyc
CHANGED
Binary files a/__pycache__/main.cpython-311.pyc and b/__pycache__/main.cpython-311.pyc differ
|
|
components/__pycache__/login.cpython-311.pyc
CHANGED
Binary files a/components/__pycache__/login.cpython-311.pyc and b/components/__pycache__/login.cpython-311.pyc differ
|
|
course_pages/week_9.py
ADDED
@@ -0,0 +1,379 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import seaborn as sns
|
6 |
+
import plotly.express as px
|
7 |
+
import plotly.graph_objects as go
|
8 |
+
from plotly.subplots import make_subplots
|
9 |
+
|
10 |
+
# Week 9: Deep Learning & BERT for Sentiment Analysis
|
11 |
+
|
12 |
+
def show():
|
13 |
+
st.title("Week 9: Deep Learning & BERT for Sentiment Analysis")
|
14 |
+
|
15 |
+
# Learning Objectives
|
16 |
+
st.header("Learning Objectives")
|
17 |
+
st.markdown("""
|
18 |
+
By the end of this week, you will be able to:
|
19 |
+
|
20 |
+
**Architectural Understanding**
|
21 |
+
- Define deep learning as neural networks with multiple hidden layers and explain why depth matters for representation learning
|
22 |
+
- Explain why "deep" matters: each layer learns increasingly complex features
|
23 |
+
- Compare deep learning with traditional machine learning and identify other deep learning methods
|
24 |
+
|
25 |
+
**When to Use Deep Learning**
|
26 |
+
- Choose deep learning for large, unstructured, or complex data; understand when to use pretrained models and when to fine-tune
|
27 |
+
- Avoid deep learning when you need explainable results, have small datasets, or simple patterns
|
28 |
+
|
29 |
+
**Understanding BERT**
|
30 |
+
- Explain what BERT does and how it understands word meaning based on context
|
31 |
+
- Understand BERT's training: pre-training, masked language modeling, bidirectional learning, and the transformer framework
|
32 |
+
- Recognize why pretrained models save time and work better than training from scratch
|
33 |
+
|
34 |
+
**Practical Implementation and Evaluation**
|
35 |
+
- Implement sentiment analysis using pretrained BERT via Hugging Face transformers
|
36 |
+
- Evaluate model performance using appropriate metrics for classification tasks
|
37 |
+
- Interpret the model's confidence scores and predictions
|
38 |
+
""")
|
39 |
+
|
40 |
+
# Introduction & Motivation
|
41 |
+
st.header("Introduction: Why Deep Learning for Text?")
|
42 |
+
st.markdown("""
|
43 |
+
Imagine you have 1,000 student forum posts about academic stress. You need to quickly identify which posts indicate concerning levels of burnout. Manual coding would take weeks. Can you train an AI to do this accurately in under 2 hours?
|
44 |
+
|
45 |
+
**Real stakes:** Early intervention programs depend on identifying students at risk. Your analysis could help universities provide timely mental health support.
|
46 |
+
""")
|
47 |
+
with st.expander("Sample Student Posts"):
|
48 |
+
st.write(
|
49 |
+
"""
|
50 |
+
- "Another all-nighter for this impossible exam. I can't keep doing this."
|
51 |
+
- "Stressed about finals but my study group is keeping me motivated!"
|
52 |
+
- "I honestly don't see the point anymore. Nothing I do matters."
|
53 |
+
"""
|
54 |
+
)
|
55 |
+
|
56 |
+
# Traditional ML vs. Deep Learning
|
57 |
+
st.header("Why Traditional ML Falls Short")
|
58 |
+
st.markdown("""
|
59 |
+
Traditional ML (like logistic regression) treats each word independently and can't understand context, negation, or sarcasm:
|
60 |
+
""")
|
61 |
+
with st.expander("Example: Traditional ML Limitations"):
|
62 |
+
st.code(
|
63 |
+
'sample_posts = [\n'
|
64 |
+
' "I\'m not stressed about finals", # Negation\n'
|
65 |
+
' "This is fine, totally fine", # Sarcasm\n'
|
66 |
+
' "Actually excited about exams" # Context-dependent\n'
|
67 |
+
']\n'
|
68 |
+
'# Traditional ML: counts words, ignores context\n'
|
69 |
+
'# "not stressed" vs. "stressed"\n'
|
70 |
+
'# "fine" (sarcastic) vs. "fine" (genuine)\n'
|
71 |
+
, language="python")
|
72 |
+
st.write("**Problem:** Context changes everything. We need a model that reads like humans do.")
|
73 |
+
|
74 |
+
# Meet BERT
|
75 |
+
st.header("Meet BERT: Context-Aware Deep Learning")
|
76 |
+
st.markdown("""
|
77 |
+
**BERT** (Bidirectional Encoder Representations from Transformers) is a deep learning model that understands language context by reading text in both directions. It was pre-trained on billions of words and can be quickly adapted to your research domain.
|
78 |
+
""")
|
79 |
+
with st.expander("BERT Demo: Fill-in-the-Blank"):
|
80 |
+
st.code(
|
81 |
+
'from transformers import pipeline\n'
|
82 |
+
'unmasker = pipeline("fill-mask", model="bert-base-uncased")\n'
|
83 |
+
'unmasker("Artificial Intelligence [MASK] take over the world.")\n'
|
84 |
+
, language="python")
|
85 |
+
st.write("BERT predicts the masked word using context from both sides.")
|
86 |
+
|
87 |
+
st.markdown("**Example Output:**")
|
88 |
+
st.code(
|
89 |
+
'[{\'sequence\': \'artificial intelligence will take over the world.\', \'score\': 0.1234, \'token\': 2054, \'token_str\': \'will\'},\n'
|
90 |
+
' {\'sequence\': \'artificial intelligence can take over the world.\', \'score\': 0.0987, \'token\': 1169, \'token_str\': \'can\'},\n'
|
91 |
+
' {\'sequence\': \'artificial intelligence may take over the world.\', \'score\': 0.0876, \'token\': 1175, \'token_str\': \'may\'}]\n'
|
92 |
+
, language="python")
|
93 |
+
|
94 |
+
# BERT's Place in the AI Family
|
95 |
+
st.header("Deep Learning, Transformers, and BERT")
|
96 |
+
st.markdown("""
|
97 |
+
- **Neural Network:** Layers of connected nodes that learn patterns from data
|
98 |
+
- **Deep Learning:** Neural networks with multiple hidden layers (3+), learning increasingly complex features
|
99 |
+
- **Transformers:** Neural networks using attention mechanisms to understand relationships between all words in a sentence
|
100 |
+
- **BERT:** A transformer model that reads text bidirectionally for context
|
101 |
+
""")
|
102 |
+
with st.expander("AI Family Tree"):
|
103 |
+
st.markdown("""
|
104 |
+
- Machine Learning
|
105 |
+
- Traditional ML (Logistic Regression, Random Forest, SVM)
|
106 |
+
- Deep Learning
|
107 |
+
- Computer Vision (CNNs)
|
108 |
+
- Sequential Data (RNNs/LSTMs)
|
109 |
+
- Language Understanding (BERT, GPT, T5)
|
110 |
+
""")
|
111 |
+
|
112 |
+
# BERT in Action: Sentiment Analysis
|
113 |
+
st.header("BERT in Action: Sentiment Analysis")
|
114 |
+
st.markdown("""
|
115 |
+
Let's use a pretrained BERT model to classify student posts as negative (burnout), neutral (normal stress), or positive (engagement).
|
116 |
+
""")
|
117 |
+
with st.expander("Example: Sentiment Analysis with BERT"):
|
118 |
+
st.code(
|
119 |
+
'from transformers import pipeline\n'
|
120 |
+
'classifier = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment-latest")\n'
|
121 |
+
'test_posts = [\n'
|
122 |
+
' "I\'m totally fine with staying up all night again",\n'
|
123 |
+
' "Not feeling overwhelmed at all",\n'
|
124 |
+
' "This workload is completely manageable",\n'
|
125 |
+
' "Actually excited about this challenging semester"\n'
|
126 |
+
']\n'
|
127 |
+
'for post in test_posts:\n'
|
128 |
+
' result = classifier(post)\n'
|
129 |
+
' print(f"{post} β {result[0][\'label\']} (confidence: {result[0][\'score\']:.2f})")\n'
|
130 |
+
, language="python")
|
131 |
+
st.write("BERT catches subtleties that word counting misses!")
|
132 |
+
|
133 |
+
st.markdown("**Example Output:**")
|
134 |
+
st.code(
|
135 |
+
'I\'m totally fine with staying up all night again β NEGATIVE (confidence: 0.89)\n'
|
136 |
+
'Not feeling overwhelmed at all β NEGATIVE (confidence: 0.76)\n'
|
137 |
+
'This workload is completely manageable β POSITIVE (confidence: 0.82)\n'
|
138 |
+
'Actually excited about this challenging semester β POSITIVE (confidence: 0.91)\n'
|
139 |
+
, language="python")
|
140 |
+
st.write("Notice how BERT correctly identifies sarcasm in the first two examples!")
|
141 |
+
|
142 |
+
# How BERT Works
|
143 |
+
st.header("How BERT Works: Architecture & Training")
|
144 |
+
st.markdown("""
|
145 |
+
- **Masked Language Modeling:** BERT learns by predicting masked words using context from both sides
|
146 |
+
- **Bidirectional Learning:** Reads text in both directions simultaneously
|
147 |
+
- **Next Sentence Prediction:** Learns relationships between sentences
|
148 |
+
- **Attention Mechanism:** Focuses on the most important words for understanding context
|
149 |
+
- **Encoder Architecture:** Uses only the encoder part of transformers
|
150 |
+
- **Transfer Learning:** Pretrained on massive data, then fine-tuned for your task
|
151 |
+
""")
|
152 |
+
with st.expander("Visualization: BERT's Attention"):
|
153 |
+
st.markdown("**How BERT's Attention Works:**")
|
154 |
+
|
155 |
+
# Create a visual representation of attention
|
156 |
+
attention_data = {
|
157 |
+
'Word': ['I', 'am', 'not', 'stressed', 'about', 'finals'],
|
158 |
+
'Attention to "not"': [0.1, 0.2, 1.0, 0.8, 0.1, 0.1],
|
159 |
+
'Attention to "stressed"': [0.1, 0.1, 0.8, 1.0, 0.7, 0.6],
|
160 |
+
'Attention to "finals"': [0.1, 0.1, 0.1, 0.6, 0.8, 1.0]
|
161 |
+
}
|
162 |
+
|
163 |
+
attention_df = pd.DataFrame(attention_data)
|
164 |
+
|
165 |
+
# Create a heatmap visualization
|
166 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
167 |
+
attention_matrix = attention_df.iloc[:, 1:].values
|
168 |
+
sns.heatmap(attention_matrix,
|
169 |
+
xticklabels=attention_df.columns[1:],
|
170 |
+
yticklabels=attention_df['Word'],
|
171 |
+
annot=True,
|
172 |
+
fmt='.1f',
|
173 |
+
cmap='Blues',
|
174 |
+
cbar_kws={'label': 'Attention Weight'})
|
175 |
+
plt.title("BERT's Attention Weights for 'I am not stressed about finals'")
|
176 |
+
plt.ylabel('Words in Sentence')
|
177 |
+
plt.xlabel('Attention Heads')
|
178 |
+
plt.tight_layout()
|
179 |
+
st.pyplot(fig)
|
180 |
+
|
181 |
+
st.markdown("""
|
182 |
+
**What this shows:**
|
183 |
+
- **Attention to 'not'**: The word 'not' pays most attention to itself (1.0) and to 'stressed' (0.8) - it's negating the stress
|
184 |
+
- **Attention to 'stressed'**: Pays attention to 'not' (0.8), itself (1.0), and 'finals' (0.6) - understanding the context
|
185 |
+
- **Attention to 'finals'**: Pays attention to 'stressed' (0.6), 'about' (0.8), and itself (1.0) - the source of stress
|
186 |
+
|
187 |
+
This bidirectional attention is what makes BERT understand context so well!
|
188 |
+
""")
|
189 |
+
|
190 |
+
# Practical: Burnout Detector with BERT
|
191 |
+
st.header("Practical: Build a Burnout Detector with BERT")
|
192 |
+
st.markdown("""
|
193 |
+
We'll simulate a dataset of student posts and use BERT to classify them. In real research, you would use your own data and labels.
|
194 |
+
""")
|
195 |
+
with st.expander("Example Code: BERT Sentiment Analysis on Student Posts"):
|
196 |
+
st.code(
|
197 |
+
'import pandas as pd\n'
|
198 |
+
'from transformers import pipeline\n'
|
199 |
+
'student_posts = [\n'
|
200 |
+
' "I can\'t sleep, can\'t eat, nothing feels worth it anymore",\n'
|
201 |
+
' "Every assignment feels impossible, I\'m failing at everything",\n'
|
202 |
+
' "Been crying in the library again, maybe I should just drop out",\n'
|
203 |
+
' "Three months of this and I feel completely empty inside",\n'
|
204 |
+
' "Finals week is rough but I know I can push through",\n'
|
205 |
+
' "Stressed about my paper but my friends are helping me stay motivated",\n'
|
206 |
+
' "Long study session today but feeling prepared for tomorrow\'s exam",\n'
|
207 |
+
' "Challenging semester but learning so much in my research methods class",\n'
|
208 |
+
' "Actually excited about my thesis research this semester",\n'
|
209 |
+
' "Difficult coursework but my professor\'s support makes it manageable",\n'
|
210 |
+
' "Study group tonight - we\'re all helping each other succeed",\n'
|
211 |
+
' "Tough week but grateful for this learning opportunity"\n'
|
212 |
+
']\n'
|
213 |
+
'labels = [\'negative\', \'negative\', \'negative\', \'negative\', \'neutral\', \'neutral\', \'neutral\', \'neutral\', \'positive\', \'positive\', \'positive\', \'positive\']\n'
|
214 |
+
'df = pd.DataFrame({\'post\': student_posts, \'true_sentiment\': labels})\n'
|
215 |
+
'classifier = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment-latest")\n'
|
216 |
+
'predictions = []\n'
|
217 |
+
'confidence_scores = []\n'
|
218 |
+
'for post in df["post"]:\n'
|
219 |
+
' result = classifier(post)[0]\n'
|
220 |
+
' predictions.append(result["label"])\n'
|
221 |
+
' confidence_scores.append(result["score"])\n'
|
222 |
+
'df["bert_prediction"] = predictions\n'
|
223 |
+
'df["confidence"] = confidence_scores\n'
|
224 |
+
'print(df.head())\n'
|
225 |
+
, language="python")
|
226 |
+
st.write("You can evaluate the model using accuracy, classification report, and confusion matrix.")
|
227 |
+
|
228 |
+
st.markdown("**Example Output:**")
|
229 |
+
st.code(
|
230 |
+
' post true_sentiment bert_prediction confidence\n'
|
231 |
+
'0 I can\'t sleep, can\'t eat, nothing feels wo... negative NEGATIVE 0.9876\n'
|
232 |
+
'1 Every assignment feels impossible, I\'m fail... negative NEGATIVE 0.9456\n'
|
233 |
+
'2 Been crying in the library again, maybe I s... negative NEGATIVE 0.9234\n'
|
234 |
+
'3 Three months of this and I feel completely ... negative NEGATIVE 0.9789\n'
|
235 |
+
'4 Finals week is rough but I know I can push ... neutral NEUTRAL 0.7654\n'
|
236 |
+
, language="python")
|
237 |
+
|
238 |
+
# Model Evaluation
|
239 |
+
st.header("Model Evaluation & Interpretation")
|
240 |
+
st.markdown("""
|
241 |
+
- **Accuracy:** Proportion of correct predictions
|
242 |
+
- **Classification Report:** Precision, recall, F1-score for each class
|
243 |
+
- **Confusion Matrix:** Visualizes true vs. predicted labels
|
244 |
+
- **Confidence Scores:** How sure is the model about each prediction?
|
245 |
+
""")
|
246 |
+
with st.expander("Example: Evaluation Code"):
|
247 |
+
st.code(
|
248 |
+
'from sklearn.metrics import classification_report, confusion_matrix\n'
|
249 |
+
'import seaborn as sns\n'
|
250 |
+
'import matplotlib.pyplot as plt\n'
|
251 |
+
'df["bert_mapped"] = df["bert_prediction"].apply(lambda x: x.lower())\n'
|
252 |
+
'accuracy = (df["true_sentiment"] == df["bert_mapped"]).mean()\n'
|
253 |
+
'print(f"Accuracy: {accuracy:.2f}")\n'
|
254 |
+
'print(classification_report(df["true_sentiment"], df["bert_mapped"]))\n'
|
255 |
+
'cm = confusion_matrix(df["true_sentiment"], df["bert_mapped"])\n'
|
256 |
+
'sns.heatmap(cm, annot=True, fmt="d", cmap="Blues")\n'
|
257 |
+
'plt.xlabel("Predicted")\n'
|
258 |
+
'plt.ylabel("True")\n'
|
259 |
+
'plt.show()\n'
|
260 |
+
, language="python")
|
261 |
+
|
262 |
+
st.markdown("**Example Output:**")
|
263 |
+
st.code(
|
264 |
+
'Accuracy: 0.83\n'
|
265 |
+
'\n'
|
266 |
+
' precision recall f1-score support\n'
|
267 |
+
'\n'
|
268 |
+
' negative 0.90 0.90 0.90 4\n'
|
269 |
+
' neutral 0.75 0.75 0.75 4\n'
|
270 |
+
' positive 0.80 0.80 0.80 4\n'
|
271 |
+
'\n'
|
272 |
+
' accuracy 0.83 12\n'
|
273 |
+
' macro avg 0.82 0.82 0.82 12\n'
|
274 |
+
'weighted avg 0.83 0.83 0.83 12\n'
|
275 |
+
, language="python")
|
276 |
+
|
277 |
+
# Why Deep Learning's Depth Matters
|
278 |
+
st.header("Why Deep Learning's Depth Matters")
|
279 |
+
st.markdown("""
|
280 |
+
Deep learning models like BERT can understand complex patterns, sarcasm, and implied meaning because each layer learns increasingly abstract features. This is why they outperform traditional ML on nuanced text tasks.
|
281 |
+
""")
|
282 |
+
with st.expander("Example: Complex Cases"):
|
283 |
+
st.code(
|
284 |
+
'complex_cases = [\n'
|
285 |
+
' "I\'m fine :) everything\'s totally under control :) :)",\n'
|
286 |
+
' "lol guess I\'m just built different, thriving on 2hrs sleep",\n'
|
287 |
+
' "Not that I\'m complaining, but this workload is killing me"\n'
|
288 |
+
']\n'
|
289 |
+
'for case in complex_cases:\n'
|
290 |
+
' result = classifier(case)[0]\n'
|
291 |
+
' print(f"{case} β {result[\'label\']} (confidence: {result[\'score\']:.2f})")\n'
|
292 |
+
, language="python")
|
293 |
+
st.write("BERT's deep layers help it catch sarcasm, contradictions, and hidden stress.")
|
294 |
+
|
295 |
+
st.markdown("**Example Output:**")
|
296 |
+
st.code(
|
297 |
+
'I\'m fine :) everything\'s totally under control :) :) β NEGATIVE (confidence: 0.92)\n'
|
298 |
+
'lol guess I\'m just built different, thriving on 2hrs sleep β NEGATIVE (confidence: 0.78)\n'
|
299 |
+
'Not that I\'m complaining, but this workload is killing me β NEGATIVE (confidence: 0.85)\n'
|
300 |
+
, language="python")
|
301 |
+
st.write("BERT correctly identifies excessive positivity, normalized concerning behavior, and mixed signals as negative sentiment.")
|
302 |
+
|
303 |
+
# Research Reflection & Ethics
|
304 |
+
st.header("Critical Research Reflection & Ethics")
|
305 |
+
st.markdown("""
|
306 |
+
**Advantages:**
|
307 |
+
- Context awareness (negation, sarcasm, implied meaning)
|
308 |
+
- Consistency and scalability
|
309 |
+
- Transferability to new domains
|
310 |
+
|
311 |
+
**Limitations:**
|
312 |
+
- May struggle with slang, asterisks, or masked emotions
|
313 |
+
- Not always explainable
|
314 |
+
- Requires validation and ethical consideration
|
315 |
+
|
316 |
+
**Ethics Discussion:**
|
317 |
+
1. Does BERT work equally well for all student populations?
|
318 |
+
2. How do we protect student anonymity?
|
319 |
+
3. What is our responsibility with concerning content?
|
320 |
+
4. How do we verify our ground truth labels?
|
321 |
+
""")
|
322 |
+
with st.expander("Example: Edge Cases & Limitations"):
|
323 |
+
st.code(
|
324 |
+
'edge_cases = [\n'
|
325 |
+
' "tbh everything\'s mid rn but like whatever",\n'
|
326 |
+
' "Academic stress? What\'s that? *nervous laughter*",\n'
|
327 |
+
' "Everything is absolutely perfect and wonderful!!"\n'
|
328 |
+
']\n'
|
329 |
+
'for case in edge_cases:\n'
|
330 |
+
' result = classifier(case)[0]\n'
|
331 |
+
' print(f"{case} β {result[\'label\']} (confidence: {result[\'score\']:.2f})")\n'
|
332 |
+
, language="python")
|
333 |
+
st.write("Would you trust this for research decisions?")
|
334 |
+
|
335 |
+
st.markdown("**Example Output:**")
|
336 |
+
st.code(
|
337 |
+
'tbh everything\'s mid rn but like whatever β NEUTRAL (confidence: 0.65)\n'
|
338 |
+
'Academic stress? What\'s that? *nervous laughter* β POSITIVE (confidence: 0.72)\n'
|
339 |
+
'Everything is absolutely perfect and wonderful!! β POSITIVE (confidence: 0.88)\n'
|
340 |
+
, language="python")
|
341 |
+
st.write("These results show BERT's limitations with slang, asterisk actions, and potentially masked emotions.")
|
342 |
+
|
343 |
+
# When to Use Deep Learning vs. Traditional ML
|
344 |
+
st.header("When to Use Deep Learning vs. Traditional ML")
|
345 |
+
st.markdown("""
|
346 |
+
**Use BERT (Deep Learning) when:**
|
347 |
+
- Context and nuance matter (like sentiment analysis)
|
348 |
+
- You have unstructured text data
|
349 |
+
- Traditional ML struggles with the complexity
|
350 |
+
- You need to scale to large datasets
|
351 |
+
|
352 |
+
**Stick with Traditional ML when:**
|
353 |
+
- You need perfect explainability
|
354 |
+
- Simple patterns work well
|
355 |
+
- Very small datasets (<100 examples)
|
356 |
+
- Computational resources are limited
|
357 |
+
""")
|
358 |
+
|
359 |
+
# Take-Home Exercise
|
360 |
+
st.header("Take-Home Exercise: Apply BERT to Your Research")
|
361 |
+
st.markdown("""
|
362 |
+
**Choose Your Research Adventure:**
|
363 |
+
1. Social Media Analysis: Sentiment about a current campus issue
|
364 |
+
2. Literature Research: Compare emotional tone across different authors
|
365 |
+
3. Survey Analysis: Classify open-ended course feedback
|
366 |
+
|
367 |
+
**Requirements:**
|
368 |
+
- Use BERT pipeline from today
|
369 |
+
- Analyze 20+ text samples
|
370 |
+
- Evaluate results critically
|
371 |
+
- Identify cases needing expert review
|
372 |
+
|
373 |
+
**Reflection Questions:**
|
374 |
+
- When did BERT's deep learning approach outperform what simple word counting could do?
|
375 |
+
- Where would you still need human expert judgment?
|
376 |
+
- How could this scale your research capabilities?
|
377 |
+
""")
|
378 |
+
|
379 |
+
st.success("You've just learned to use one of the most powerful tools in modern AI research. Try BERT on your own research question this week!")
|
main.py
CHANGED
@@ -25,6 +25,7 @@ from course_pages import week_5
|
|
25 |
from course_pages import week_6
|
26 |
from course_pages import week_7
|
27 |
from course_pages import week_8
|
|
|
28 |
# Page configuration
|
29 |
st.set_page_config(
|
30 |
page_title="Data Science Course App",
|
@@ -193,6 +194,8 @@ def show_week_content():
|
|
193 |
week_7.show()
|
194 |
elif st.session_state.current_week == 8:
|
195 |
week_8.show()
|
|
|
|
|
196 |
else:
|
197 |
st.warning("Content for this week is not yet available.")
|
198 |
|
@@ -205,7 +208,7 @@ def main():
|
|
205 |
return
|
206 |
|
207 |
# User is logged in, show course content
|
208 |
-
if st.session_state.current_week in [1, 2, 3, 4, 5, 6, 7, 8]:
|
209 |
show_week_content()
|
210 |
else:
|
211 |
st.title("Data Science Research Paper Course")
|
|
|
25 |
from course_pages import week_6
|
26 |
from course_pages import week_7
|
27 |
from course_pages import week_8
|
28 |
+
from course_pages import week_9
|
29 |
# Page configuration
|
30 |
st.set_page_config(
|
31 |
page_title="Data Science Course App",
|
|
|
194 |
week_7.show()
|
195 |
elif st.session_state.current_week == 8:
|
196 |
week_8.show()
|
197 |
+
elif st.session_state.current_week == 9:
|
198 |
+
week_9.show()
|
199 |
else:
|
200 |
st.warning("Content for this week is not yet available.")
|
201 |
|
|
|
208 |
return
|
209 |
|
210 |
# User is logged in, show course content
|
211 |
+
if st.session_state.current_week in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
|
212 |
show_week_content()
|
213 |
else:
|
214 |
st.title("Data Science Research Paper Course")
|