Skip to content

Commit

Permalink
fixed an error in test; new way for regularization
Browse files Browse the repository at this point in the history
  • Loading branch information
linxihui authored and linxihui committed Jan 3, 2017
1 parent c6d0281 commit e640e12
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
42 changes: 22 additions & 20 deletions keras/layers/crf.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ def __init__(self, output_dim,
weights=None, input_dim=None, input_length=None, unroll=False, **kwargs):
self.supports_masking = True
self.output_dim = output_dim
self.learn_mode = learn_mode.lower()
self.test_mode = test_mode.lower()
self.learn_mode = learn_mode
assert self.learn_mode in ['join', 'marginal']
self.test_mode = test_mode
if self.test_mode is None:
self.test_mode = 'viterbi' if self.learn_mode == 'join' else 'marginal'
else:
Expand Down Expand Up @@ -150,25 +150,27 @@ def build(self, input_shape):
if self.input_length is None:
self.input_length = self.input_spec[0].shape[1]
input_dim = input_shape[-1]
self.W = self.in_init((input_dim, self.output_dim), name='{}_W'.format(self.name))
self.U = self.chain_init((self.output_dim, self.output_dim), name='{}_U'.format(self.name))
self.b = K.zeros((self.output_dim,), name='{}_b'.format(self.name))
self.trainable_weights = [self.W, self.U, self.b]

self.W = self.add_weight((input_dim, self.output_dim),
initializer=self.in_init,
name='{}_W'.format(self.name),
regularizer=self.W_regularizer)
self.U = self.add_weight((self.output_dim, self.output_dim),
initializer=self.chain_init,
name='{}_U'.format(self.name),
regularizer=self.U_regularizer)
self.b = self.add_weight((self.output_dim,),
initializer='zero',
name='{}_b'.format(self.name),
regularizer=self.b_regularizer)

if self.boundary_energy:
self.a1 = K.zeros((self.output_dim,), name='{}_a1'.format(self.name))
self.an = K.zeros((self.output_dim,), name='{}_an'.format(self.name))
self.trainable_weights += [self.a1, self.an]

self.regularizers = []
if self.W_regularizer:
self.W_regularizer.set_param(self.W)
self.regularizers.append(self.W_regularizer)
if self.U_regularizer:
self.U_regularizer.set_param(self.U)
self.regularizers.append(self.U_regularizer)
if self.b_regularizer:
self.b_regularizer.set_param(self.b)
self.regularizers.append(self.b_regularizer)
self.a1 = self.add_weight((self.output_dim,),
initializer='zero',
name='{}_a1'.format(self.name))
self.an = self.add_weight((self.output_dim,),
initializer='zero',
name='{}_an'.format(self.name))

if self.initial_weights is not None:
self.set_weights(self.initial_weights)
Expand Down
5 changes: 2 additions & 3 deletions tests/keras/layers/test_crf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

@keras_test
def test_CRF():

# data
x = np.random.randint(1, embedding_num, nb_samples * timesteps).reshape((nb_samples, timesteps))
x[0, -4:] = 0 # right padding
Expand Down Expand Up @@ -43,8 +42,8 @@ def test_CRF():

# test `viterbi_acc
_, v_acc, _ = model.evaluate(x, y)
np_acc = (y_pred[x > 0] == y[x > 0]).mean()
assert_allclose([v_acc], [np_acc], atol=1e-6)
np_acc = (y_pred[x > 0] == y[:, :, 0][x > 0]).astype('float32').mean()
assert np.abs(v_acc - np_acc) < 1e-4

# test config
model.get_config()
Expand Down

0 comments on commit e640e12

Please sign in to comment.