-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
153 lines (137 loc) · 6.71 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
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 22 14:04:02 2017
@author: shiro
"""
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten, Lambda, Reshape
from keras.layers.embeddings import Embedding
from keras import backend as K
from keras.layers import Conv1D, Dropout, LSTM, MaxPooling1D, BatchNormalization,GlobalMaxPool1D
def create_CBOW(input_dim, max_length, embedding, output_dim=50, n_class=3):
print('\n\n ## CBOW MODEL ## \n')
model = Sequential()
if embedding == 'None':
model.add(Embedding(input_dim+1, output_dim , input_length=max_length))
else:
print('loading embedding in CBOW')
model.add(Embedding(input_dim+1, output_dim ,weights=[embedding], input_length=max_length))
model.add(Lambda(lambda x : K.sum(x,axis=1), output_shape=(output_dim,)))
model.add(Dense(n_class, activation='softmax', name='softmax'))
print(model.summary())
return model
def create_CBOWNN(input_dim, max_length, embedding, output_dim=50, n_class=3):
model = Sequential()
if embedding == 'None':
model.add(Embedding(input_dim+1, output_dim , input_length=max_length))
else:
print('loading embedding in CBOW')
model.add(Embedding(input_dim+1, output_dim ,weights=[embedding], input_length=max_length))
model.add(Lambda(lambda x : K.sum(x,axis=1), output_shape=(output_dim,)))
model.add(Dense(1024, activation='relu', name='fc1'))#, kernel_regularizer=l2(0.001)))
model.add(Dropout(0.5))
model.add(Dense(1024, activation='relu', name='fc2'))#, kernel_regularizer=l2(0.001)))
model.add(Dropout(0.5))
model.add(Dense(n_class, activation='softmax', name='softmax'))
# compile the model
#model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'])
# summarize the model
print(model.summary())
return model
def create_simpleModel(input_dim, max_length, embedding, output_dim=50, n_class=3):
model = Sequential()
if embedding == None:
model.add(Embedding(input_dim+1, output_dim , input_length=max_length))
else:
print('loading embedding in simpleModel')
model.add(Embedding(input_dim+1, output_dim ,weights=[embedding], input_length=max_length))
model.add(Conv1D(128, 5, activation='relu')) # (length, input_dim = output dim of the embedding)
model.add(BatchNormalization())
model.add(MaxPooling1D(2))
#model.add(Conv1D(128, 5, activation='relu'))
#model.add(BatchNormalization())
#model.add(MaxPooling1D(2))
#model.add(Conv1D(128, 5, activation='relu'))
#model.add(BatchNormalization())
#model.add(MaxPooling1D(5))
model.add(Flatten())
#model.add(Dropout(0.2))
#model.add(Dense(512,activation='relu'))
#model.add(Dropout(0.5))
model.add(Dense(1024,activation='relu'))
#↕model.add(Dropout(0.5))
model.add(Dense(n_class, activation='softmax'))
# compile the model
#model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'])
# summarize the model
print(model.summary())
return model
def create_convModel(input_dim, max_length, embedding, output_dim=50, n_class=3):
model = Sequential()
if embedding == 'None':
model.add(Embedding(input_dim+1, output_dim , input_length=max_length))
else:
print('loading embedding in convModel')
model.add(Embedding(input_dim+1, output_dim ,weights=[embedding], input_length=max_length))
model.add(Conv1D(512, 3, activation='elu')) # (length, input_dim = output dim of the embedding)
model.add(BatchNormalization())
model.add(GlobalMaxPool1D())
model.add(Dense(1024,activation='relu'))#, kernel_regularizer=l2(0.01)))
model.add(Dropout(0.5))
model.add(Dense(n_class, activation='softmax'))
# compile the model
#model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'])
# summarize the model
print(model.summary())
return model
def create_convModel2(input_dim, max_length, embedding, output_dim=50, n_class=3):
model = Sequential()
if embedding == 'None':
model.add(Embedding(input_dim+1, output_dim , input_length=max_length))
else:
print('loading embedding in convModel')
model.add(Embedding(input_dim+1, output_dim ,weights=[embedding], input_length=max_length))
model.add(Conv1D(512, 3, activation='elu', name='conv1')) # (length, input_dim = output dim of the embedding)
model.add(BatchNormalization())
model.add(MaxPooling1D(2))
model.add(Conv1D(512, 3, activation='elu', name='conv2')) # (length, input_dim = output dim of the embedding)
model.add(BatchNormalization())
model.add(MaxPooling1D(2))
model.add(GlobalMaxPool1D())
model.add(Dense(1024,activation='relu'))#, kernel_regularizer=l2(0.01)))
model.add(Dropout(0.5))
model.add(Dense(n_class, activation='softmax'))
# compile the model
#model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'])
# summarize the model
print(model.summary())
return model
def create_lstm(input_dim, max_length, embedding, output_dim=10, n_class=3):
model = Sequential()
if embedding == 'None':
model.add(Embedding(input_dim+1, output_dim , input_length=max_length))
else:
print('loading embedding in LSTM')
model.add(Embedding(input_dim+1, output_dim ,weights=[embedding], input_length=max_length))
#model.add(Flatten())
#model.add(Conv1D(filters=128, kernel_size=3, padding='same', activation='relu'))
#model.add(BatchNormalization())
#model.add(Conv1D(filters=64, kernel_size=3, padding='same', activation='relu'))
#model.add(Conv1D(filters=128, kernel_size=3, padding='same', activation='relu'))
#model.add(Conv1D(64, 3, border_mode='same')) # (length, input_dim = output dim of the embedding)
#model.add(Conv1D(128, 3, border_mode='same'))
#model.add(Conv1D(256, 3, border_mode='same'))
#model.add(Conv1D(512, 3, border_mode='same'))
#model.add(MaxPooling1D(pool_size=2))
#model.add(Flatten())
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))#, activation = 'relu', recurrent_activation='relu'))
#model.add(Dense(512, activation='relu'))
#model.add(Dropout(0.5))
model.add(Dense(n_class, activation='softmax'))
#model.add(Dense(n_class, activation='softmax'))
# compile the model
#model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# summarize the model
print(model.summary())
return model