This repository has been archived by the owner on Jul 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
models.py
100 lines (85 loc) · 3.92 KB
/
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
import numpy as np
from keras.models import Sequential,Model
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers import Input
from keras.layers import Merge
from keras.utils import np_utils
from keras.layers import Conv1D, MaxPooling1D, Embedding
from keras.layers import Input, Embedding, LSTM, Dense, merge
class CNNModel:
def getModel(self, params_obj, weight=None ):
if params_obj.use_two_channels:
print "Two channels - static and non-static"
inp = Input(shape=(params_obj.inp_length,), dtype='int32')
embeddings_layer = Embedding(
params_obj.vocab_size+1, # due to mask_zero
params_obj.embeddings_dim,
input_length=params_obj.inp_length,
weights=[weight],
trainable=False
)(inp)
embeddings_layer_t = Embedding(
params_obj.vocab_size+1, # due to mask_zero
params_obj.embeddings_dim,
input_length=params_obj.inp_length,
weights=[weight],
trainable=True
)(inp)
#Convolution
#inp = Input(shape=(params_obj.inp_length, params_obj.embeddings_dim))
convolution_features_list = []
for filter_size,pool_length,num_filters in zip(params_obj.filter_sizes, params_obj.filter_pool_lengths, params_obj.filter_sizes):
conv_layer = Conv1D(nb_filter=num_filters, filter_length=filter_size, activation='relu')(embeddings_layer)
pool_layer = MaxPooling1D(pool_length=pool_length)(conv_layer)
flatten = Flatten()(pool_layer)
convolution_features_list.append(flatten)
for filter_size,pool_length,num_filters in zip(params_obj.filter_sizes, params_obj.filter_pool_lengths, params_obj.filter_sizes):
conv_layer = Conv1D(nb_filter=num_filters, filter_length=filter_size, activation='relu')(embeddings_layer_t)
pool_layer = MaxPooling1D(pool_length=pool_length)(conv_layer)
flatten = Flatten()(pool_layer)
convolution_features_list.append(flatten)
out1 = Merge(mode='concat')(convolution_features_list)
network = Model(input=inp, output=out1)
# Model
model = Sequential()
model.add(network)
#Add dense layer to complete the model
model.add(Dense(params_obj.dense_layer_size,init='uniform',activation='relu'))
model.add(Dropout(params_obj.dropout_val))
model.add( Dense(params_obj.num_classes, init='uniform', activation='softmax') )
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
else:
# Embeddings
if weight==None or params_obj.use_pretrained_embeddings==False:
weight=np.array(params_obj.vocab_size+1, params_obj.embeddings_dim).astype('float32')
embeddings_layer = Embedding(
params_obj.vocab_size+1, # due to mask_zero
params_obj.embeddings_dim,
input_length=params_obj.inp_length,
weights=[weight],
trainable=params_obj.train_embedding
)
#Convolution
inp = Input(shape=(params_obj.inp_length, params_obj.embeddings_dim))
convolution_features_list = []
for filter_size,pool_length,num_filters in zip(params_obj.filter_sizes, params_obj.filter_pool_lengths, params_obj.filter_sizes):
conv_layer = Conv1D(nb_filter=num_filters, filter_length=filter_size, activation='relu')(inp)
pool_layer = MaxPooling1D(pool_length=pool_length)(conv_layer)
flatten = Flatten()(pool_layer)
convolution_features_list.append(flatten)
out = Merge(mode='concat')(convolution_features_list)
network = Model(input=inp, output=out)
# Model
model = Sequential()
model.add(embeddings_layer)
model.add(Dropout(params_obj.dropout_val, input_shape=(params_obj.vocab_size+1, params_obj.embeddings_dim)))
model.add(network)
#Add dense layer to complete the model
model.add(Dense(params_obj.dense_layer_size,init='uniform',activation='relu'))
model.add(Dropout(params_obj.dropout_val))
model.add( Dense(params_obj.num_classes, init='uniform', activation='softmax') )
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model