-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
150 lines (121 loc) · 5.82 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
# -*- coding: utf-8 -*-
import os
import re
import random
import warnings
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import tensorflow as tf
import csv
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer, TfidfVectorizer
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Dense, LSTM, Dropout, Input, GlobalMaxPooling1D, Conv1D, MaxPooling1D, Flatten, Activation, GRU, Bidirectional
from gensim.models import Word2Vec
from matplotlib.ticker import MaxNLocator
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import datamining.ultis as ultis
tf.random.set_seed(1)
random.seed(2)
np.random.seed(3)
df = pd.read_csv('labeledTrainData_1w.tsv')
max_length = 150
embedding_dim = 250
X_train_word, y_train, X_test_word, y_test = ultis.get_train_test(df, 'sentence2')
w2v_model = Word2Vec(X_train_word, min_count=1, size=embedding_dim, iter=10, sg=1)
X_train, X_test, tokenizer = ultis.text_tokenizer(X_train_word, X_test_word, max_length)
embedding_matrix, vocab_size = ultis.get_embedding_matrix(tokenizer, w2v_model, embedding_dim)
# model_type = ('LSTM', 'GRU', 'Bidirectional LSTM', 'Bidirectional GRU')
model_type = 'GRU'
if model_type == 'LSTM':
#LSTM
lstm_model = Sequential()
embedding_layer = Embedding(vocab_size, embedding_dim, weights=[embedding_matrix], input_length=max_length, trainable=False)
lstm_model.add(embedding_layer)
lstm_model.add(Dropout(0.5))
lstm_model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
lstm_model.add(Dense(1, activation='sigmoid'))
lstm_model.summary()
lstm_model.compile(
loss='binary_crossentropy',
optimizer="adam",
metrics=['accuracy']
)
lstm_record = lstm_model.fit(X_train, y_train, batch_size=1024, epochs=30, validation_split=0.2, verbose=1, shuffle=True)
scores = lstm_model.predict(X_test, verbose=2)
y_pred=np.where(scores>0.5,1,0)
print(classification_report(y_test, y_pred,digits=4))
ultis.plot_acc(lstm_record.history['accuracy'], lstm_record.history['val_accuracy'], 'LSTM Accuracy')
ultis.plot_loss(lstm_record.history['loss'], lstm_record.history['val_loss'], 'LSTM Loss')
pred = lstm_model.predict(X_test)
fpr, tpr, best_thresholds = ultis.get_best_thresholds(lstm_model, pred, y_test)
origin_acc = ultis.compute_acc(pred, y_test, 0.5)
improve_acc = ultis.compute_acc(pred, y_test, best_thresholds)
ultis.plot_roc_curve(fpr, tpr)
print(origin_acc,improve_acc,best_thresholds)
elif model_type == 'GRU':
#GRU
GRU_model = Sequential()
embedding_layer = Embedding(vocab_size, embedding_dim, weights=[embedding_matrix], input_length=max_length, trainable=False)
GRU_model.add(embedding_layer)
GRU_model.add(Dropout(0.5))
GRU_model.add(GRU(100, dropout=0.2, recurrent_dropout=0.2))
GRU_model.add(Dense(1, activation='sigmoid'))
GRU_model.summary()
GRU_model.compile(
loss='binary_crossentropy',
optimizer="adam",
metrics=['accuracy']
)
GRU_record = GRU_model.fit(X_train, y_train, batch_size=1024, epochs=30, validation_split=0.2, verbose=1)
scores = GRU_model.predict(X_test, verbose=2)
y_pred=np.where(scores>0.5,1,0)
cm=confusion_matrix(y_pred,y_test)
print(classification_report(y_test, y_pred,digits=4))
ultis.plot_acc(GRU_record.history['accuracy'], GRU_record.history['val_accuracy'], 'GRU Accuracy')
ultis.plot_loss(GRU_record.history['loss'], GRU_record.history['val_loss'], 'GRU Loss')
elif model_type == 'Bidirectional LSTM':
#Bidirectional LSTM
Blstm_model = Sequential()
embedding_layer = Embedding(vocab_size, embedding_dim, weights=[embedding_matrix], input_length=max_length, trainable=False)
Blstm_model.add(embedding_layer)
Blstm_model.add(Dropout(0.5))
Blstm_model.add(Bidirectional(LSTM(100, dropout=0.2, recurrent_dropout=0.2)))
Blstm_model.add(Dense(1, activation='sigmoid'))
Blstm_model.summary()
Blstm_model.compile(
loss='binary_crossentropy',
optimizer="adam",
metrics=['accuracy']
)
Blstm_record = Blstm_model.fit(X_train, y_train, batch_size=1024, epochs=30, validation_split=0.2, verbose=1)
scores = Blstm_model.predict(X_test, verbose=2)
y_pred=np.where(scores>0.5,1,0)
cm=confusion_matrix(y_pred,y_test)
print(classification_report(y_test, y_pred,digits=4))
ultis.plot_acc(Blstm_record.history['accuracy'], Blstm_record.history['val_accuracy'], 'Bidirectional LSTM Accuracy')
ultis.plot_loss(Blstm_record.history['loss'], Blstm_record.history['val_loss'], 'Bidirectional LSTM Loss')
elif model_type == 'Bidirectional GRU':
#Bidirectional GRU
BGRU_model = Sequential()
embedding_layer = Embedding(vocab_size, embedding_dim, weights=[embedding_matrix], input_length=max_length, trainable=False)
BGRU_model.add(embedding_layer)
BGRU_model.add(Dropout(0.5))
BGRU_model.add(Bidirectional(GRU(100, dropout=0.2, recurrent_dropout=0.2)))
BGRU_model.add(Dense(1, activation='sigmoid'))
BGRU_model.summary()
BGRU_model.compile(
loss='binary_crossentropy',
optimizer="adam",
metrics=['accuracy']
)
BGRU_record = BGRU_model.fit(X_train, y_train, batch_size=1024, epochs=30, validation_split=0.2, verbose=1)
scores = BGRU_model.predict(X_test, verbose=2)
y_pred=np.where(scores>0.5,1,0)
cm=confusion_matrix(y_pred,y_test)
print(classification_report(y_test, y_pred,digits=4))
ultis.plot_acc(BGRU_record.history['accuracy'], BGRU_record.history['val_accuracy'], 'Bidirectional GRU Accuracy')
ultis.plot_loss(BGRU_record.history['loss'], BGRU_record.history['val_loss'], 'Bidirectional GRU Loss')