-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
165 lines (134 loc) · 4.8 KB
/
model.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
import spacy
import keras
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.layers import Flatten, Conv1D, MaxPooling1D, GlobalAveragePooling1D, LSTM, merge
from keras.layers.embeddings import Embedding
from keras.layers.normalization import BatchNormalization
from keras.constraints import maxnorm
from keras.regularizers import l2
from keras import backend as K
def get_embeddings(vocab):
max_rank = max(lex.rank+1 for lex in vocab if lex.has_vector)
vectors = np.ndarray((max_rank+1, vocab.vectors_length), dtype='float32')
for lex in vocab:
if lex.has_vector:
vectors[lex.rank + 1] = lex.vector
return vectors
vocab_nlp = spacy.load('en_core_web_lg', parser=False, tagger=False, entity=False)
print('Preparing embeddings...')
embeddings = get_embeddings(vocab_nlp.vocab)
def build_model_twitter(max_length=100,
nb_filters=64,
kernel_size=3,
pool_size=2,
regularization=0.01,
weight_constraint=2.,
dropout_prob=0.4,
clear_session=True):
if clear_session:
K.clear_session()
model = Sequential()
model.add(Embedding(
embeddings.shape[0],
embeddings.shape[1],
input_length=max_length,
trainable=False,
weights=[embeddings]))
model.add(Conv1D(nb_filters, kernel_size, activation='relu'))
model.add(Conv1D(nb_filters, kernel_size, activation='relu'))
model.add(MaxPooling1D(pool_size))
model.add(Dropout(dropout_prob))
model.add(Conv1D(nb_filters * 2, kernel_size, activation='relu'))
model.add(Conv1D(nb_filters * 2, kernel_size, activation='relu'))
model.add(MaxPooling1D(pool_size))
model.add(Dropout(dropout_prob))
model.add(GlobalAveragePooling1D())
model.add(Dense(1,
kernel_regularizer=l2(regularization),
kernel_constraint=maxnorm(weight_constraint),
activation='sigmoid'))
model.compile(
loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
return model
def build_model_twitter2(max_length=100,
nb_filters=64,
kernel_size=3,
pool_size=2,
regularization=0.01,
weight_constraint=2.,
dropout_prob=0.4,
clear_session=True):
if clear_session:
K.clear_session()
model = Sequential()
model.add(Embedding(
embeddings.shape[0],
embeddings.shape[1],
input_length=max_length,
trainable=False,
weights=[embeddings]))
model.add(Conv1D(nb_filters, kernel_size, activation='relu'))
model.add(MaxPooling1D(pool_size))
model.add(Dropout(dropout_prob))
model.add(Conv1D(nb_filters * 2, kernel_size, activation='relu'))
model.add(MaxPooling1D(pool_size))
model.add(Dropout(dropout_prob))
model.add(Conv1D(nb_filters * 4, kernel_size, activation='relu'))
model.add(MaxPooling1D(pool_size))
model.add(Dropout(dropout_prob))
model.add(GlobalAveragePooling1D())
model.add(Dense(1,
kernel_regularizer=l2(regularization),
kernel_constraint=maxnorm(weight_constraint),
activation='sigmoid'))
model.compile(
loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
return model
def build_model_twitter3(max_length=100,
nb_filters=64,
kernel_size=3,
pool_size=2,
regularization=0.01,
weight_constraint=2.,
dropout_prob=0.4,
clear_session=True):
if clear_session:
K.clear_session()
model = Sequential()
model.add(Embedding(
embeddings.shape[0],
embeddings.shape[1],
input_length=max_length,
trainable=False,
weights=[embeddings]))
model.add(Conv1D(nb_filters, kernel_size))
#model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size))
model.add(Activation('relu'))
model.add(Dropout(dropout_prob))
model.add(Conv1D(nb_filters * 2, kernel_size))
#model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size))
model.add(Activation('relu'))
model.add(Dropout(dropout_prob))
model.add(Conv1D(nb_filters * 4, kernel_size))
#model.add(BatchNormalization())
model.add(MaxPooling1D(pool_size))
model.add(Activation('relu'))
model.add(Dropout(dropout_prob))
model.add(GlobalAveragePooling1D())
model.add(Dense(1,
kernel_regularizer=l2(regularization),
kernel_constraint=maxnorm(weight_constraint),
activation='sigmoid'))
model.compile(
loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
return model