forked from parushgera/lsda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stance_models.py
157 lines (119 loc) · 6.02 KB
/
stance_models.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
import string
import re
import os
import nltk
import pandas as pd
import numpy as np
import json
import tensorflow as tf
from tensorflow import keras
SEED = 1013
np.random.seed(SEED)
#nltk.download('stopwords')
from nltk.tokenize import TweetTokenizer
from nltk.corpus import stopwords, twitter_samples
from stance_utils import *
#from parameters import *
from nltk.stem import PorterStemmer
from sklearn.metrics import classification_report
from sklearn.feature_extraction.text import CountVectorizer
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras import Sequential
#from tensorflow.keras.layers import Dropout,Concatenate,Dense, Embedding, SpatialDropout1D, Flatten, GRU, Bidirectional, Conv1D,MaxPooling1D
from tensorflow.keras.layers import RNN, Dropout,Concatenate,Dense, Embedding,LSTMCell, LSTM, SpatialDropout1D, Flatten, GRU, Bidirectional, Conv1D, Input,MaxPooling1D
from sklearn.model_selection import train_test_split
from tensorflow.keras import Model
from sklearn.model_selection import StratifiedKFold
stemmer = PorterStemmer()
tokenizer = TweetTokenizer(preserve_case=False, strip_handles=True, reduce_len=True)
stopwords_english = stopwords.words('english')
from sklearn.preprocessing import LabelEncoder
import keras.backend as K
from keras.layers import Lambda
import random
import matplotlib.pyplot as plt
def bicond(units,opt, embedding_matrix, x_t, batch_size, sentence_maxlen,num_classes): # Check this model again....
embedded_inputs = tf.nn.embedding_lookup(embedding_matrix, x_t)
print(embedded_inputs.shape)
inputs = embedded_inputs[:batch_size]
h_0 = tf.convert_to_tensor(np.zeros([batch_size, units]).astype(np.float32))
c_0 = tf.convert_to_tensor(np.zeros([batch_size, units]).astype(np.float32))
start_state = [h_0, c_0]
lstm = LSTM(units, return_sequences=True, return_state=True)
fw_output, fw_h_0, fw_c_0 = lstm(inputs,initial_state = [h_0, c_0])
bw_output, bw_h_0, bw_c_0 = lstm(inputs[::-1],initial_state = [h_0, c_0]) # feeding data backwords
inputs2 = Input(shape=(sentence_maxlen), name = 'Input')
embedded_inputs = Embedding(embedding_matrix.shape[0], embedding_matrix.shape[1], weights=[embedding_matrix], name = 'Embedding')(inputs2)
lstm = LSTM(units,activation='tanh',dropout=0.1,name = 'lstm')(embedded_inputs, initial_state = [h_0, fw_c_0])
b_lstm = LSTM(units,activation='tanh',dropout=0.1, go_backwards = True,name = 'back_lstm')(embedded_inputs, initial_state = [h_0, bw_c_0])
cond_out = []
cond_out.append(lstm)
cond_out.append(b_lstm)
concat_output = Concatenate()(cond_out)
flat = Flatten(name = 'Flatten')(concat_output)
output = (Dense(num_classes,activation='softmax',name = 'Dense'))(flat)
model = Model(inputs=inputs2, outputs=output, name = 'bicond')
model.compile(loss = 'categorical_crossentropy', optimizer=opt, metrics = ['accuracy'])
model.summary()
return model
def biLSTM(embedding_matrix, num_classes):
model = Sequential(name = 'biLSTM')
model.add(Embedding(embedding_matrix.shape[0], embedding_matrix.shape[1], weights=[embedding_matrix]))
model.add(Dropout(0.2))
model.add(LSTM(64,return_sequences=True,dropout=0.3))
model.add(Bidirectional(LSTM(64,dropout=0.3)))
#model.add(Flatten())
#add a dropout here
model.add(Dropout(0.5))
model.add(Dense(num_classes,activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = ['accuracy'])
return model
def biLSTMCNN(embedding_matrix, num_classes,sentence_maxlen ):
inputs = Input(shape=(sentence_maxlen,))
embedded_inputs = Embedding(embedding_matrix.shape[0], embedding_matrix.shape[1], weights=[embedding_matrix])(inputs)
embedded_inputs = Dropout(0.2)(embedded_inputs)
lstm = Bidirectional(LSTM(64,return_sequences=True,dropout=0.3))(embedded_inputs)
convs = []
for each_filter_size in [3,4,5]:
#print(rnn.shape)
each_conv = Conv1D(100, each_filter_size, activation='relu')(lstm)
each_conv = MaxPooling1D(sentence_maxlen-each_filter_size+1)(each_conv)
each_conv = Flatten()(each_conv)
#print(each_conv.shape)
convs.append(each_conv)
output = Concatenate()(convs)
output = Dropout(0.5)(output)
output = (Dense(num_classes,activation='softmax'))(output)
model = Model(inputs=inputs, outputs=output)
model.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = ['accuracy'])
return model
def biGRU(embedding_matrix, num_classes):
model = Sequential()
model.add(Embedding(embedding_matrix.shape[0], embedding_matrix.shape[1], weights=[embedding_matrix]))
model.add(Dropout(0.2))
model.add(Bidirectional(GRU(64,return_sequences=True,dropout=0.3)))
model.add(Bidirectional(GRU(64,dropout=0.3)))
model.add(Dropout(0.5))
model.add(Dense(3,activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = ['accuracy'])
return model
def biGRUCNN(embedding_matrix, num_classes):
inputs = Input(shape=(sentence_maxlen,))
embedded_inputs = Embedding(embedding_matrix.shape[0], embedding_matrix.shape[1], weights=[embedding_matrix])(inputs)
embedded_inputs = Dropout(0.2)(embedded_inputs)
rnn = Bidirectional(GRU(64,return_sequences=True,dropout=0.3))(embedded_inputs)
convs = []
for each_filter_size in [3,4,5]:
#print(rnn.shape)
each_conv = Conv1D(100, each_filter_size, activation='relu')(rnn)
each_conv = MaxPooling1D(sentence_maxlen-each_filter_size+1)(each_conv)
each_conv = Flatten()(each_conv)
#print(each_conv.shape)
convs.append(each_conv)
output = Concatenate()(convs)
output = Dropout(0.5)(output)
output = (Dense(3,activation='softmax'))(output)
model = Model(inputs=inputs, outputs=output, name = 'biGRUCNN')
model.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = ['accuracy'])
return model