-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet.py
252 lines (170 loc) · 6.17 KB
/
tweet.py
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
# -*- coding: utf-8 -*-
"""Tweet Emotion Recognition.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1o_u3O0JJrtFW_IoRbA9wioGFPvsbtLRK
## Tweet Emotion Recognition: Natural Language Processing with TensorFlow
---
Manuel Arambula Gonzalez
Dataset: [Tweet Emotion Dataset](https://github.com/dair-ai/emotion_dataset)
Guided project [Tweet Emotion Recognition with TensorFlow](https://www.coursera.org/learn/tweet-emotion-tensorflow)
---
## Task 1: Setup and Imports
1. Installing Hugging Face's nlp package
2. Importing libraries
"""
!pip install nlp
# Commented out IPython magic to ensure Python compatibility.
# %matplotlib inline
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import nlp
import random
def show_history(h):
epochs_trained = len(h.history['loss'])
plt.figure(figsize=(16, 6))
plt.subplot(1, 2, 1)
plt.plot(range(0, epochs_trained), h.history.get('accuracy'), label='Training')
plt.plot(range(0, epochs_trained), h.history.get('val_accuracy'), label='Validation')
plt.ylim([0., 1.])
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(range(0, epochs_trained), h.history.get('loss'), label='Training')
plt.plot(range(0, epochs_trained), h.history.get('val_loss'), label='Validation')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
def show_confusion_matrix(y_true, y_pred, classes):
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_true, y_pred, normalize='true')
plt.figure(figsize=(8, 8))
sp = plt.subplot(1, 1, 1)
ctx = sp.matshow(cm)
plt.xticks(list(range(0, 6)), labels=classes)
plt.yticks(list(range(0, 6)), labels=classes)
plt.colorbar(ctx)
plt.show()
print('Using TensorFlow version', tf.__version__)
"""## Task 2: Importing Data
1. Importing the Tweet Emotion dataset
2. Creating train, validation and test sets
3. Extracting tweets and labels from the examples
"""
!pip install datasets
import datasets
dataset = datasets.load_dataset('emotion')
dataset
train = dataset['train']
val = dataset['validation']
test = dataset['test']
def get_tweets(data):
tweets = [x['text'] for x in data]
labels = [x['label'] for x in data]
return tweets, labels
tweets, labels = get_tweets(train)
tweets[0], labels[0]
"""## Task 3: Tokenizer
1. Tokenizing the tweets
"""
from tensorflow.keras.preprocessing.text import Tokenizer
tokenizer = Tokenizer(num_words=10000, oov_token='<UNK>')
tokenizer.fit_on_texts(tweets)
print(tokenizer.texts_to_sequences([tweets[0]]))
"""## Task 4: Padding and Truncating Sequences
1. Checking length of the tweets
2. Creating padded sequences
"""
lengths = [len(t.split(' ')) for t in tweets]
plt.hist(lengths, bins=len(set(lengths)))
plt.show()
from tensorflow.keras.preprocessing.sequence import pad_sequences
def get_sequences(tokenizer, tweets):
sequences = tokenizer.texts_to_sequences(tweets)
padded_sequences = pad_sequences(sequences, truncating='post', maxlen=50, padding='post')
return padded_sequences
padded_train_sequences = get_sequences(tokenizer, tweets)
padded_train_sequences[0]
"""## Task 5: Preparing the Labels
1. Creating classes to index and index to classes dictionaries
2. Converting text labels to numeric labels
"""
classes = set(labels)
print(classes)
plt.hist(labels, bins=11)
plt.show()
classes_to_index = dict((c, i) for i, c in enumerate(classes))
index_to_classes = dict((v, k) for k, v in classes_to_index.items())
classes_to_index
index_to_classes
names_to_ids = lambda labels: np.array([classes_to_index.get(x) for x in labels])
train_labels = names_to_ids(labels)
print(train_labels[0])
"""## Task 6: Creating the Model
1. Creating the model
2. Compiling the model
"""
model = tf.keras.models.Sequential([
tf.keras.layers.Embedding(10000, 16, input_length=50),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(20, return_sequences=True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(20)),
tf.keras.layers.Dense(6, activation='softmax')
])
model.compile(
loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
model.summary()
"""## Task 7: Training the Model
1. Preparing a validation set
2. Training the model
"""
val_tweets, val_labels = get_tweets(val)
val_sequences = get_sequences(tokenizer, val_tweets)
val_labels = names_to_ids(val_labels)
val_tweets[0], val_labels[0]
h = model.fit(
padded_train_sequences, train_labels,
validation_data=(val_sequences, val_labels),
epochs=20,
callbacks=[
tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=2)
]
)
"""## Task 8: Evaluating the Model
1. Visualizing training history
2. Prepraring a test set
3. A look at individual predictions on the test set
4. A look at all predictions on the test set
"""
show_history(h)
test_tweets, test_labels = get_tweets(test)
test_sequences = get_sequences(tokenizer, test_tweets)
test_labels = names_to_ids(test_labels)
_ = model.evaluate(test_sequences, test_labels)
classes_to_index = {'anger': 0, 'joy': 1, 'love': 2, 'surprise': 3, 'fear': 4, 'sadness': 5}
index_to_classes = {0: 'anger', 1: 'joy', 2: 'love', 3: 'surprise', 4: 'fear', 5: 'sadness'}
predicted_emotion = index_to_classes.get(p)
i = random.randint(0, len(test_labels) - 1)
print('Sentence:', test_tweets[i])
print('Emotion:', index_to_classes[test_labels[i]])
# Use predict and argmax to get predicted class
predictions = model.predict(np.expand_dims(test_sequences[i], axis=0))
p = np.argmax(predictions)
print('Predicted Emotion:', index_to_classes.get(p))
# Use predict to get the predicted probabilities for each class
preds = model.predict(test_sequences)
# Get the class with the highest probability
preds = np.argmax(preds, axis=1)
preds.shape, test_labels.shape
# Use predict to get the predicted probabilities for each class
preds = model.predict(test_sequences)
# Get the class with the highest probability
preds = np.argmax(preds, axis=1)
preds.shape, test_labels.shape
show_confusion_matrix(test_labels, preds, list(classes))
"""'anger': 0, 'joy': 1, 'love': 2, 'surprise': 3, 'fear': 4, 'sadness': 5"""