-
Notifications
You must be signed in to change notification settings - Fork 4
/
dcnn.py
executable file
·170 lines (135 loc) · 5.03 KB
/
dcnn.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
"""
Numpy version of DCNN, used for prediction, instead of training
"""
import numpy as np
from numpy_impl import (conv2d, LogisticRegression)
_MODEL_PATH = "models/filter_widths=10,7,,batch_size=10,,ks=20,5,,fold=1,1,,conv_layer_n=2,,ebd_dm=48,,nkerns=6,12,,dr=0.5,0.5,,l2_regs=1e-06,0.0001,1e-05,1e-06.pkl"
class WordEmbeddingLayer(object):
"""
Layer that takes input vectors, output the sentence matrix
"""
def __init__(self,
embeddings):
"""
embeddings: numpy.ndarray
Embedding, (vocab size, embedding dimension)
"""
assert embeddings.ndim == 2, "Should be have 2 dimensions"
self.embeddings = embeddings
def output(self, x):
"""
x: numpy.ndarray
the input sentences consiting of word indices (number of instances, sentence word number)
"""
sent_matrices = np.array(
map(lambda sent: self.embeddings[sent],
x)
)
# equivalent to dimshuffle(0, 'x', 2, 1) in Theano
return sent_matrices.swapaxes(1,2)[:,None,:,:]
class ConvFoldingPoolLayer(object):
"""
Convolution, folding and k-max pooling layer
"""
def __init__(self,
k,
fold,
W,
b):
"""
k: int
the k value in the max-pooling layer
fold: int, 0 or 1
fold or not
W: numpy.ndarray,
the filter weight matrices,
dimension: (number of filters, num input feature maps, filter height, filter width)
b: numpy.ndarray,
the filter bias,
dimension: (number of filters, )
"""
self.fold_flag = fold
self.W = W
self.b = b
self.k = k
def fold(self, x):
"""
x: np.ndarray
the input, 4d array
"""
return (x[:, :, np.arange(0, x.shape[2], 2)] +
x[:, :, np.arange(1, x.shape[2], 2)]) / 2
def k_max_pool(self, x, k):
"""
perform k-max pool on the input along the rows
x: numpy.ndarray
the input, 4d array
k: theano.tensor.iscalar
the k parameter
Returns:
4D numpy.ndarray
"""
ind = np.argsort(x, axis = 3)
sorted_ind = np.sort(ind[:,:,:, -k:], axis = 3)
dim0, dim1, dim2, dim3 = sorted_ind.shape
indices_dim0 = np.arange(dim0).repeat(dim1 * dim2 * dim3)
indices_dim1 = np.transpose(np.arange(dim1).repeat(dim2 * dim3).reshape((dim1*dim2*dim3, 1)).repeat(dim0, axis=1)).flatten()
indices_dim2 = np.transpose(np.arange(dim2).repeat(dim3).reshape((dim2*dim3, 1)).repeat(dim0 * dim1, axis = 1)).flatten()
return x[indices_dim0, indices_dim1, indices_dim2, sorted_ind.flatten()].reshape(sorted_ind.shape)
def output(self, x):
# non-linear transform of the convolution output
conv_out = conv2d(x,
self.W,
mode = "full")
if self.fold_flag:
# fold
fold_out = self.fold(conv_out)
else:
fold_out = conv_out
# k-max pool
pool_out = (self.k_max_pool(fold_out, self.k) +
self.b[np.newaxis, :, np.newaxis, np.newaxis])
return np.tanh(pool_out)
class DCNN(object):
def __init__(self, params):
self.e_layer = WordEmbeddingLayer(embeddings = params.embeddings)
self.c_layers = []
for i in xrange(params.conv_layer_n):
self.c_layers.append(ConvFoldingPoolLayer(params.ks[i],
params.fold[i],
W = params.W[i],
b = params.b[i])
)
self.l_layer = LogisticRegression(
params.logreg_W,
params.logreg_b
)
def _p_y_given_x(self, x):
output = self.e_layer.output(x)
for l in self.c_layers:
output = l.output(output)
assert output.ndim == 4
output = output.reshape(
(output.shape[0],
np.prod(output.shape[1:]))
)
return self.l_layer._p_y_given_x(output)
def predict(self, x):
return np.argmax(self._p_y_given_x(x), axis = 1)
# The following functions are
# FOR TESTING PURPOSE
#
def _nnl(self, x, y):
p_y_given_x = self._p_y_given_x(x)
return np.mean(
-np.log(p_y_given_x[np.arange(y.shape[0]), y])
)
def _errors(self, x, y):
assert y.dtype == np.int32, "%r != %r" %(y.dtype, np.int32)
pred_y = self.predict(x)
return np.sum(pred_y != y) / float(pred_y.shape[0])
def _c_layer_output(self, x):
output = self.e_layer.output(x)
for l in self.c_layers:
output = l.output(output)
return output