@@ -31,10 +31,14 @@ def __init__(self, ngram_size, input_vocab_size, output_vocab_size, input_embedd
31
31
self .input_embeddings = numpy .zeros ((input_vocab_size , input_embedding_dimension ))
32
32
if not num_hidden :
33
33
self .hidden1_weights = numpy .zeros ((output_embedding_dimension , (ngram_size - 1 )* input_embedding_dimension ))
34
+ self .hidden1_biases = numpy .zeros ((output_embedding_dimension , 1 ))
34
35
self .hidden2_weights = numpy .zeros ((1 , 1 ))
36
+ self .hidden2_biases = numpy .zeros ((1 , 1 ))
35
37
else :
36
38
self .hidden1_weights = numpy .zeros ((num_hidden , (ngram_size - 1 )* input_embedding_dimension ))
39
+ self .hidden1_biases = numpy .zeros ((num_hidden , 1 ))
37
40
self .hidden2_weights = numpy .zeros ((output_embedding_dimension , num_hidden ))
41
+ self .hidden2_biases = numpy .zeros ((output_embedding_dimension , 1 ))
38
42
self .output_weights = numpy .zeros ((output_vocab_size , output_embedding_dimension ))
39
43
self .output_biases = numpy .zeros ((output_vocab_size , 1 ))
40
44
@@ -43,17 +47,19 @@ def uniform(m):
43
47
m [:,:] = numpy .random .uniform (- r , r , m .shape )
44
48
uniform (self .input_embeddings )
45
49
uniform (self .hidden1_weights )
50
+ uniform (self .hidden1_biases )
46
51
uniform (self .hidden2_weights )
52
+ uniform (self .hidden2_biases )
47
53
uniform (self .output_weights )
48
54
uniform (self .output_biases )
49
55
50
56
def forward_prop (self , inputs , output = None , normalize = True ):
51
57
u = numpy .bmat ([[self .input_embeddings .T * ui ] for ui in inputs ])
52
- h1 = numpy .maximum (0. , self .hidden1_weights * u )
58
+ h1 = numpy .maximum (0. , self .hidden1_weights * u + self . hidden1_biases )
53
59
if not self .num_hidden :
54
60
h2 = h1
55
61
else :
56
- h2 = numpy .maximum (0. , self .hidden2_weights * h1 )
62
+ h2 = numpy .maximum (0. , self .hidden2_weights * h1 + self . hidden2_biases )
57
63
58
64
if output is None :
59
65
o = self .output_weights * h2 + self .output_biases
@@ -119,9 +125,15 @@ def write_vector(m):
119
125
outfile .write ("\\ hidden_weights 1\n " )
120
126
write_matrix (self .hidden1_weights )
121
127
128
+ outfile .write ("\\ hidden_biases 1\n " )
129
+ write_matrix (self .hidden1_biases )
130
+
122
131
outfile .write ("\\ hidden_weights 2\n " )
123
132
write_matrix (self .hidden2_weights )
124
133
134
+ outfile .write ("\\ hidden_biases 2\n " )
135
+ write_matrix (self .hidden2_biases )
136
+
125
137
outfile .write ("\\ output_weights\n " )
126
138
write_matrix (self .output_weights )
127
139
@@ -207,11 +219,21 @@ def read_matrix(lines, m, n, out=None):
207
219
read_matrix (lines , m .output_embedding_dimension , (m .ngram_size - 1 )* m .input_embedding_dimension , out = m .hidden1_weights )
208
220
else :
209
221
read_matrix (lines , m .num_hidden , (m .ngram_size - 1 )* m .input_embedding_dimension , out = m .hidden1_weights )
222
+ elif section == "\\ hidden_biases 1" :
223
+ if not m .num_hidden :
224
+ read_matrix (lines , m .output_embedding_dimension , 1 , out = m .hidden1_biases )
225
+ else :
226
+ read_matrix (lines , m .num_hidden , 1 , out = m .hidden1_biases )
210
227
elif section == "\\ hidden_weights 2" :
211
228
if not m .num_hidden :
212
229
read_matrix (lines , 1 , 1 , out = m .hidden2_weights )
213
230
else :
214
231
read_matrix (lines , m .output_embedding_dimension , m .num_hidden , out = m .hidden2_weights )
232
+ elif section == "\\ hidden_biases 2" :
233
+ if not m .num_hidden :
234
+ read_matrix (lines , 1 , 1 , out = m .hidden2_biases )
235
+ else :
236
+ read_matrix (lines , m .output_embedding_dimension , 1 , out = m .hidden2_biases )
215
237
elif section == "\\ output_weights" :
216
238
read_matrix (lines , m .output_vocab_size , m .output_embedding_dimension , out = m .output_weights )
217
239
elif section == "\\ output_biases" :
0 commit comments