-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtra_Credits_2.py
212 lines (167 loc) · 8.33 KB
/
Extra_Credits_2.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
# Installing packages used.
import re
import nltk
import gensim
import gensim.downloader as downloader
from sklearn.metrics import f1_score, accuracy_score
from sklearn.neural_network import MLPClassifier
from gensim.models import Word2Vec
nltk.download('stopwords')
nltk.download('punkt')
import numpy as np
import pandas as pd
import pickle
# Load the pre-trained model
w2v_model = downloader.load("word2vec-google-news-300")
w2v_model.save_word2vec_format("word2vec-google-news-300.bin", binary=True)
# Normalizing the dataset
def normalize_text(processed_text, w2v_model=None):
# Convert all text into lowercase
processed_text = processed_text.lower()
# Remove all non-word and non-space characters
processed_text = re.sub(r"[^\w\s]", "", processed_text)
# Tokenize the text into individual words
processed_text = nltk.word_tokenize(processed_text)
# Removing stop words
stopwords = nltk.corpus.stopwords.words('english')
processed_text = [t for t in processed_text if t not in stopwords]
if w2v_model is not None:
embeddings = [w2v_model[token] for token in processed_text if token in w2v_model.key_to_index]
else:
embeddings = []
max_length = 300
if not embeddings:
embeddings.append(np.zeros(max_length))
# Pad embeddings with zeros to ensure they all have the same length
padding_length = max_length - len(embeddings)
if padding_length > 0:
embeddings += [np.zeros_like(embeddings[0])] * padding_length
elif padding_length < 0:
embeddings = embeddings[:max_length]
return np.array(embeddings).mean(axis=0)
def update_embeddings(path_to_train_file):
# Load training data
train_data = pd.read_csv(path_to_train_file, usecols=['comment_text'])
# Tokenize the text into individual words
tokenized_train_data = [nltk.word_tokenize(text.lower()) for text in train_data['comment_text']]
# Load pre-trained word2vec model
w2v_model = gensim.models.KeyedVectors.load_word2vec_format('word2vec-google-news-300.bin', binary=True)
# Train the Word2Vec model on the corpus
model = gensim.models.Word2Vec(
tokenized_train_data,
vector_size=300,
min_count=1,
epochs=5,
workers=4,
window=5,
sg=0,
sample=0.001,
hs=1
)
# Fine-tune the model's weights with the pre-trained model's weights
model.build_vocab([list(w2v_model.key_to_index.keys())], update=True)
model.intersect_word2vec_format('word2vec-google-news-300.bin', binary=True, lockf=1.0)
# Save the fine-tuned embeddings
model.wv.save_word2vec_format('fine_tuned_embeddings.bin', binary=True)
return 'fine_tuned_embeddings_pretrained.bin'
update_embeddings("train.csv")
def train_MLP_model(path_to_train_file, num_layers, embeddings_file):
if embeddings_file == "word2vec-google-news-300.bin":
x = True
else:
x = False
# Load the embeddings
w2v_model = gensim.models.KeyedVectors.load_word2vec_format(embeddings_file, binary=True)
# Load the training data
train_data = pd.read_csv(path_to_train_file, usecols=['comment_text', 'toxic'])
# Normalize the text and extract embeddings
train_embeddings = [normalize_text(text, w2v_model) for text in train_data['comment_text']]
# Convert labels to integers
train_labels = train_data['toxic'].astype(int)
# Initialize MLP classifier
hidden_layer_sizes = [128] + [64] * (num_layers - 1)
model = MLPClassifier(hidden_layer_sizes=hidden_layer_sizes, max_iter=10, verbose=True, solver="adam",
learning_rate_init=0.005)
# Train MLP classifier
model.fit(train_embeddings, train_labels)
# Calculate accuracy on training data
train_predictions = model.predict(train_embeddings)
train_accuracy = accuracy_score(train_labels, train_predictions)
print(f'Training Accuracy: {train_accuracy:.4f}')
# Save the trained model
if x == True:
with open(f"MLP_model_{num_layers}layers.pkl", "wb") as f:
pickle.dump(model, f)
else:
with open(f"MLP_model_{num_layers}layers_finetuned.pkl", "wb") as f:
pickle.dump(model, f)
#train_MLP_model("train.csv", num_layers=1, embeddings_file="word2vec-google-news-300.bin")
#train_MLP_model("train.csv", num_layers=2, embeddings_file="word2vec-google-news-300.bin")
#train_MLP_model("train.csv", num_layers=3, embeddings_file="word2vec-google-news-300.bin")
#train_MLP_model("train.csv", num_layers=2, embeddings_file="fine_tuned_embeddings.bin")
#train_MLP_model("train.csv", num_layers=1, embeddings_file="fine_tuned_embeddings.bin")
#train_MLP_model("train.csv", num_layers=3, embeddings_file="fine_tuned_embeddings.bin")
def create_test_data():
test_data = pd.read_csv('test.csv', usecols=['id', 'comment_text'])
test_label_data = pd.read_csv('test_labels.csv', usecols=['id', 'toxic'])
test_data = pd.merge(test_data, test_label_data, on='id', how='inner')
test_label_data = None
test_data = test_data[(test_data['toxic'] == 0) | (test_data['toxic'] == 1)].reset_index(drop=True)
test_data.to_csv("EC_test_data.csv")
def evaluate_model(model_path, embeddings_file, test_file):
create_test_data()
# Load the embeddings
w2v_model = gensim.models.KeyedVectors.load_word2vec_format(embeddings_file, binary=True)
# Load the test data
test_data = pd.read_csv(test_file, usecols=['comment_text', 'toxic'])
# Normalize the text and extract embeddings
test_embeddings = [normalize_text(text, w2v_model) for text in test_data['comment_text']]
# Convert labels to integers
test_labels = test_data['toxic'].astype(int)
# Load the trained model
with open(model_path, "rb") as f:
model = pickle.load(f)
# Predict labels for test data using the trained model
test_predictions = model.predict(test_embeddings)
# Calculate F1 score and accuracy on test data
f1 = f1_score(test_labels, test_predictions)
acc = accuracy_score(test_labels, test_predictions)
# Print results
print(f"Test F1 score: {f1:.4f}")
print(f"Test accuracy: {acc:.4f}")
return f1, acc
# Evaluate the MLP model trained on pre-trained embeddings
#pretrained_model_path = "MLP_model_2layers.pkl"
#pretrained_embeddings_file = "word2vec-google-news-300.bin"
#test_file = "EC_test_data.csv"
#pretrained_f1, pretrained_acc = evaluate_model(pretrained_model_path, pretrained_embeddings_file, test_file)
# Evaluate the MLP model trained on fine-tuned embeddings
#fintuned_model_path = "MLP_model_2layers_finetuned.pkl"
#fintuned_embeddings_file = "fine_tuned_embeddings.bin"
#test_file = "EC_test_data.csv"
#fintuned_f1, fintuned_acc = evaluate_model(fintuned_model_path, fintuned_embeddings_file, test_file)
# Evaluate the MLP model trained on pre-trained embeddings
#pretrained_model_path = "MLP_model_1layers.pkl"
#pretrained_embeddings_file = "word2vec-google-news-300.bin"
#test_file = "EC_test_data.csv"
#pretrained_f1, pretrained_acc = evaluate_model(pretrained_model_path, pretrained_embeddings_file, test_file)
# Evaluate the MLP model trained on fine-tuned embeddings
#fintuned_model_path = "MLP_model_1layers_finetuned.pkl"
#fintuned_embeddings_file = "fine_tuned_embeddings.bin"
#test_file = "EC_test_data.csv"
#fintuned_f1, fintuned_acc = evaluate_model(fintuned_model_path, fintuned_embeddings_file, test_file)
# Evaluate the MLP model trained on pre-trained embeddings
#pretrained_model_path = "MLP_model_3layers.pkl"
#pretrained_embeddings_file = "word2vec-google-news-300.bin"
#test_file = "EC_test_data.csv"
#pretrained_f1, pretrained_acc = evaluate_model(pretrained_model_path, pretrained_embeddings_file, test_file)
# Evaluate the MLP model trained on fine-tuned embeddings
#fintuned_model_path = "MLP_model_3layers_finetuned.pkl"
#fintuned_embeddings_file = "fine_tuned_embeddings.bin"
#test_file = "EC_test_data.csv"
#fintuned_f1, fintuned_acc = evaluate_model(fintuned_model_path, fintuned_embeddings_file, test_file)
# Print the results
#print("Pre-trained MLP model F1 score:", pretrained_f1)
#print("Pre-trained MLP model accuracy:", pretrained_acc)
#print("Fine-tuned MLP model F1 score:", fintuned_f1)
#print("Fine-tuned MLP model accuracy:", fintuned_acc)