ShwetaliShimangaud
/
Text-classification-System-for-Medical-abstracts-using-LSTM-CNN-and-Attention
Public
forked from manasbede/COMP550-Final-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
197 lines (181 loc) · 8.17 KB
/
main.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
from models.BiLSTM_Attention import BiLSTM_with_Attention, BiLSTM_with_Attention_WE
from models.BiLSTM_CNN_Attention_Glove import LSTM_CNN_Attention_Glove
from models.bert import BERT
from models.LSTM import LSTM_Model, LSTM_Model_WE
from models.LSTM_Attention import LSTM_with_Attention, LSTM_with_Attention_WE
from models.LSTM_CNN import CNN_LSTM
from sklearn.metrics import accuracy_score
import numpy as np
import pandas as pd
from argparse import Namespace
import os,time
# Dataset : Ohsumed
# Implementing All Models
if __name__ == "__main__":
print(os.getpid())
pid = os.getpid()
file1 = open("tmp.txt", "w")
file1.write(f"{pid}")
file1.close()
print("Waiting for execution...")
time.sleep(60)
train_df=pd.read_csv('Train.csv')
train_df = train_df.sample(frac = 1)
train_texts=train_df['Text'].values
train_labels=train_df['Label'].values
test_df=pd.read_csv('Test.csv')
test_df = test_df.sample(frac = 1)
test_texts=test_df['Text'].values
test_labels=test_df['Label'].values
#Model1
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"BiLSTM with Attention Start: {curr_time}\n")
file1.close()
print("Implementing BiLSTM with Attention Model")
model = BiLSTM_with_Attention()
model.fit(train_texts,train_labels)
accuracy=model.predict(test_texts, test_labels)
print(f'Test accuracy: {accuracy * 100:.2f}%')
model.save()
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"BiLSTM with Attention Stop: {curr_time}\n")
file1.close()
#Model2
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"BiLSTM with Attention and WE Start: {curr_time}\n")
file1.close()
print("Implementing BiLSTM with Attention and Word Embedding Model")
model = BiLSTM_with_Attention_WE()
model.fit(train_texts,train_labels)
accuracy=model.predict(test_texts, test_labels)
print(f'Test accuracy: {accuracy * 100:.2f}%')
model.save()
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"BiLSTM with Attention with WE Stop: {curr_time}\n")
file1.close()
#Model3
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"BiLSTM with CNN,Attention and Glove Word Embedding Start: {curr_time}\n")
file1.close()
print("Implementing BiLSTM with CNN,Attention and Glove Word Embedding Model")
model = LSTM_CNN_Attention_Glove()
model.fit(train_texts, train_labels)
accuracy = model.predict(test_texts, test_labels)
print(f'Test accuracy: {accuracy * 100:.2f}%')
#model.save()
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"BiLSTM with CNN,Attention and Glove Word Embedding Stop: {curr_time}\n")
file1.close()
#Model4
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"BERT model by truncating maximum length of sentence to 512 Start: {curr_time}\n")
file1.close()
print("Implementing BERT model by truncating maximum length of sentence to 512")
model = BERT(512, 8, 10, 2e-5)
train_dataset_loader = model.preprocess(train_texts, train_labels)
test_dataset_loader = model.preprocess(test_texts, test_labels)
model.fit(train_dataset_loader)
predicted_label = model.predict(test_dataset_loader)
accuracy = accuracy_score(np.asarray(test_labels), np.asarray(predicted_label))
print(f'Test accuracy: {accuracy * 100:.2f}%')
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"BERT model by truncating maximum length of sentence to 512 Stop: {curr_time}\n")
file1.close()
#Model5
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"BERT model by splitting one sentence into multiple sentences to maintain maximum length of 512 Start: {curr_time}\n")
file1.close()
print("Implementing BERT model by splitting one sentence into multiple sentences to maintain maximum length of 512")
model = BERT(512, 8, 10, 2e-5)
train_dataset_loader = model.preprocess_with_chunking(train_texts, train_labels)
test_dataset_loader = model.preprocess_with_chunking(test_texts, test_labels)
model.fit(train_dataset_loader)
predicted_label = model.predict(test_dataset_loader)
accuracy = accuracy_score(np.asarray(test_labels), np.asarray(predicted_label))
print(f'Test accuracy: {accuracy * 100:.2f}%')
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"BERT model by splitting one sentence into multiple sentences to maintain maximum length of 512 Stop: {curr_time}\n")
file1.close()
#Model6
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"SimpleLSTM Start: {curr_time}\n")
file1.close()
print("Implementing Simple LSTM Model")
model = LSTM_Model()
model.fit(train_texts,train_labels)
accuracy=model.predict(test_texts, test_labels)
print(f'Test accuracy: {accuracy * 100:.2f}%')
model.save()
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"SimpleLSTM Stop: {curr_time}\n")
file1.close()
#Model7
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"LSTM with WE Start: {curr_time}\n")
file1.close()
print("Implementing LSTM Model with Word Embedding")
model = LSTM_Model_WE()
model.fit(train_texts,train_labels)
accuracy=model.predict(test_texts, test_labels)
print(f'Test accuracy: {accuracy * 100:.2f}%')
model.save()
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"LSTM with WE Stop: {curr_time}\n")
file1.close()
#Model8
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"LSTM with Att Start: {curr_time}\n")
file1.close()
print("Implementing LSTM with Attention Model")
model = LSTM_with_Attention()
model.fit(train_texts,train_labels)
accuracy=model.predict(test_texts, test_labels)
print(f'Test accuracy: {accuracy * 100:.2f}%')
model.save()
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"LSTM with Att Stop: {curr_time}\n")
file1.close()
#Model9
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"LSTM with Att WE Start: {curr_time}\n")
file1.close()
print("Implementing LSTM with Attention and Word Embedding Model")
model = LSTM_with_Attention_WE()
model.fit(train_texts,train_labels)
accuracy=model.predict(test_texts, test_labels)
print(f'Test accuracy: {accuracy * 100:.2f}%')
model.save()
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"LSTM with Att WE Stop: {curr_time}\n")
file1.close()
#Model10
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"CNN LSTM Start: {curr_time}\n")
file1.close()
print("Implementing CNN LSTM Model")
args=Namespace(dataset='ohsumed', dataset_id='lemmatized', num_classes=23, run_id='', embpath='', embsize=300, maxlen=1000, vocabsize=40000, static=False)
new_model = CNN_LSTM(args)
new_model.evaluate()
curr_time = time.strftime("%H:%M:%S", time.localtime())
file1 = open("main_logs.txt", "a") # append mode
file1.write(f"CNN LSTM Stop: {curr_time}\n")
file1.close()