/
siamese_network.py
246 lines (158 loc) · 6.39 KB
/
siamese_network.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from __future__ import print_function
import numpy as np
from keras.models import Sequential
from keras.layers.core import (Dense, Dropout,
Activation, Flatten)
from keras.layers.embeddings import Embedding
from keras.layers.convolutional import Convolution1D, MaxPooling1D
from keras.callbacks import ModelCheckpoint, EarlyStopping
import theano
import cPickle
from keras.regularizers import l2, l1, l1l2
from convert_review import build_design_matrix
import keras.backend as K
import modelParameters
import os
import datetime
from activations import DistanceMetric
DEVSPLIT = 14
USEWORDS = True
if USEWORDS:
VocabSize = modelParameters.VocabSize_w
maxReviewLen = modelParameters.MaxLen_w
skipTop = modelParameters.skip_top
else:
VocabSize = modelParameters.VocabSize_c
maxReviewLen = modelParameters.MaxLen_c
skipTop = 0
def contrastiveLoss(Xl,Xr,y):
return y*K.l2_normalize(Xl,Xr) + (1-y)*K.max(10,10-K.l2_normalize(Xl,Xr))
batch_size = 80
num_filters1 = 1300
filter_length1 = 2
stride_len1 = 1
pool_len1 = 2
num_filters2 = 800
filter_length2 = 3
stride_len2 = 1
pool_len2 = 2
num_filters3 = 500
filter_length3 = 4
stride_len3 = 1
pool_len3 = 2
num_filters4 = 300
filter_length4 = 5
stride_len4 = 1
pool_len4 = 2
embedding_dims = 200
hidden_dims = 100
num_epochs = 5
def train_siamese_model():
print( 'Loading data...' )
((X_train, y_train), (X_test, y_test)) = build_design_matrix( VocabSize,
use_words = USEWORDS,
skip_top = skipTop,
dev_split = DEVSPLIT )
print( len( X_train ), 'train sequences' )
print( len( X_test ), 'test sequences' )
print( 'X_train shape:', X_train.shape )
print( 'X_test shape:', X_test.shape )
print( 'Build model...' )
#LEFT and RIGHT branches of siamese network
modelL = Sequential( )
modelR = Sequential( )
# we start off with an efficient embedding layer which maps
# our vocab indices into embedding_dims dimensions
modelL.add( Embedding( VocabSize, embedding_dims, input_length = maxReviewLen ) )
modelR.add( Embedding( VocabSize, embedding_dims, input_length = maxReviewLen ) )
modelL.add( Dropout( 0.10 ) )
modelR.add( Dropout( 0.10 ) )
###init changed from uniform to glorot_norm
modelL.add( Convolution1D( nb_filter = num_filters1,
filter_length = filter_length1,
border_mode = 'valid',
activation = 'relu',
subsample_length = stride_len1,
init = 'uniform'
) )
modelR.add( Convolution1D( nb_filter = num_filters1,
filter_length = filter_length1,
border_mode = 'valid',
activation = 'relu',
subsample_length = stride_len1,
init = 'uniform'
) )
# input_length=maxReviewLen, input_dim=VocabSize
modelL.add( Dropout( 0.25 ) )
modelR.add( Dropout( 0.25 ) )
modelL.add( MaxPooling1D( pool_length = 2 ) )
modelR.add( MaxPooling1D( pool_length = 2 ) )
modelL.add( Convolution1D( nb_filter = num_filters2,
filter_length = filter_length2,
border_mode = 'valid',
activation = 'relu',
subsample_length = stride_len2,
init = 'uniform'
) )
modelR.add( Convolution1D( nb_filter = num_filters2,
filter_length = filter_length2,
border_mode = 'valid',
activation = 'relu',
subsample_length = stride_len2,
init = 'uniform'
) )
modelL.add( Dropout( 0.40 ) )
modelR.add( Dropout( 0.40 ) )
# we use standard max pooling (halving the output of the previous layer):
modelL.add( MaxPooling1D( pool_length = 2 ) )
modelR.add( MaxPooling1D( pool_length = 2 ) )
modelL.add( Convolution1D( nb_filter = num_filters3,
filter_length = filter_length3,
border_mode = 'valid',
activation = 'relu',
subsample_length = stride_len3,
init = 'uniform'
) )
modelR.add( Convolution1D( nb_filter = num_filters3,
filter_length = filter_length3,
border_mode = 'valid',
activation = 'relu',
subsample_length = stride_len3,
init = 'uniform'
) )
modelL.add( Dropout( 0.30 ) )
modelR.add( Dropout( 0.30 ) )
modelL.add( MaxPooling1D( pool_length = 2 ) )
modelR.add( MaxPooling1D( pool_length = 2 ) )
modelL.add( Convolution1D( nb_filter = num_filters4,
filter_length = filter_length4,
border_mode = 'valid',
activation = 'relu',
subsample_length = stride_len4,
init = 'uniform'
) )
modelR.add( Convolution1D( nb_filter = num_filters4,
filter_length = filter_length4,
border_mode = 'valid',
activation = 'relu',
subsample_length = stride_len4,
init = 'uniform'
) )
modelL.add( Dropout( 0.25 ) )
modelR.add( Dropout( 0.25 ) )
modelL.add( MaxPooling1D( pool_length = pool_len4 ) )
modelR.add( MaxPooling1D( pool_length = pool_len4 ) )
# We flatten the output of the conv layer,
# so that we can add a vanilla dense layer:
modelL.add( Flatten( ) )
modelR.add( Flatten( ) )
# We add a vanilla hidden layer:
modelL.add( Dense( hidden_dims ) )
modelL.add( Activation( 'relu' ) )
modelR.add( Dense( hidden_dims ) )
modelR.add( Activation( 'relu' ) )
merged_vector = merge([modelL, modelR], mode='concat', concat_axis=-1)
Gw=Dense(1, activation=DistanceMetric)(merged_vector)
model.compile( loss = contrastiveLoss,
optimizer = 'rmsprop',
)