Skip to content

Commit d4091ee

Browse files
Replaces custom _lengths_to_masks function with the official, more efficient sequence_mask function that supersedes it.
PiperOrigin-RevId: 179971521
1 parent a64485d commit d4091ee

3 files changed

Lines changed: 14 additions & 41 deletions

File tree

tensorflow/contrib/crf/__init__.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,31 @@
1616
1717
See the @{$python/contrib.crf} guide.
1818
19-
@@crf_sequence_score
20-
@@crf_log_norm
21-
@@crf_log_likelihood
22-
@@crf_unary_score
2319
@@crf_binary_score
2420
@@crf_decode
25-
@@CrfForwardRnnCell
26-
@@CrfDecodeForwardRnnCell
21+
@@crf_log_likelihood
22+
@@crf_log_norm
23+
@@crf_sequence_score
24+
@@crf_unary_score
2725
@@CrfDecodeBackwardRnnCell
26+
@@CrfDecodeForwardRnnCell
27+
@@CrfForwardRnnCell
2828
@@viterbi_decode
2929
"""
3030

3131
from __future__ import absolute_import
3232
from __future__ import division
3333
from __future__ import print_function
3434

35-
from tensorflow.contrib.crf.python.ops.crf import _lengths_to_masks
3635
from tensorflow.contrib.crf.python.ops.crf import crf_binary_score
3736
from tensorflow.contrib.crf.python.ops.crf import crf_decode
3837
from tensorflow.contrib.crf.python.ops.crf import crf_log_likelihood
3938
from tensorflow.contrib.crf.python.ops.crf import crf_log_norm
4039
from tensorflow.contrib.crf.python.ops.crf import crf_sequence_score
4140
from tensorflow.contrib.crf.python.ops.crf import crf_unary_score
42-
from tensorflow.contrib.crf.python.ops.crf import CrfForwardRnnCell
43-
from tensorflow.contrib.crf.python.ops.crf import CrfDecodeForwardRnnCell
4441
from tensorflow.contrib.crf.python.ops.crf import CrfDecodeBackwardRnnCell
42+
from tensorflow.contrib.crf.python.ops.crf import CrfDecodeForwardRnnCell
43+
from tensorflow.contrib.crf.python.ops.crf import CrfForwardRnnCell
4544
from tensorflow.contrib.crf.python.ops.crf import viterbi_decode
4645

4746
from tensorflow.python.util.all_util import remove_undocumented

tensorflow/contrib/crf/python/kernel_tests/crf_test.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,6 @@ def testCrfLogLikelihood(self):
179179
tf_total_log_likelihood = sess.run(total_log_likelihood)
180180
self.assertAllClose(tf_total_log_likelihood, 0.0)
181181

182-
def testLengthsToMasks(self):
183-
with self.test_session() as sess:
184-
sequence_lengths = [4, 1, 8, 2]
185-
max_sequence_length = max(sequence_lengths)
186-
mask = crf._lengths_to_masks(sequence_lengths, max_sequence_length)
187-
tf_mask = sess.run(mask)
188-
self.assertEqual(len(tf_mask), len(sequence_lengths))
189-
for m, l in zip(tf_mask, sequence_lengths):
190-
self.assertAllEqual(m[:l], [1] * l)
191-
self.assertAllEqual(m[l:], [0] * (len(m) - l))
192-
193182
def testViterbiDecode(self):
194183
inputs = np.array(
195184
[[4, 5, -3], [3, -1, 3], [-1, 2, 1], [0, 0, 0]], dtype=np.float32)

tensorflow/contrib/crf/python/ops/crf.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,6 @@
7070
]
7171

7272

73-
def _lengths_to_masks(lengths, max_length):
74-
"""Creates a binary matrix that can be used to mask away padding.
75-
76-
Args:
77-
lengths: A vector of integers representing lengths.
78-
max_length: An integer indicating the maximum length. All values in
79-
lengths should be less than max_length.
80-
Returns:
81-
masks: Masks that can be used to get rid of padding.
82-
"""
83-
tiled_ranges = array_ops.tile(
84-
array_ops.expand_dims(math_ops.range(max_length), 0),
85-
[array_ops.shape(lengths)[0], 1])
86-
lengths = array_ops.expand_dims(lengths, 1)
87-
masks = math_ops.to_float(
88-
math_ops.to_int64(tiled_ranges) < math_ops.to_int64(lengths))
89-
return masks
90-
91-
9273
def crf_sequence_score(inputs, tag_indices, sequence_lengths,
9374
transition_params):
9475
"""Computes the unnormalized score for a tag sequence.
@@ -234,7 +215,9 @@ def crf_unary_score(tag_indices, sequence_lengths, inputs):
234215
array_ops.gather(flattened_inputs, flattened_tag_indices),
235216
[batch_size, max_seq_len])
236217

237-
masks = _lengths_to_masks(sequence_lengths, array_ops.shape(tag_indices)[1])
218+
masks = array_ops.sequence_mask(sequence_lengths,
219+
maxlen=array_ops.shape(tag_indices)[1],
220+
dtype=dtypes.float32)
238221

239222
unary_scores = math_ops.reduce_sum(unary_scores * masks, 1)
240223
return unary_scores
@@ -268,7 +251,9 @@ def crf_binary_score(tag_indices, sequence_lengths, transition_params):
268251
binary_scores = array_ops.gather(flattened_transition_params,
269252
flattened_transition_indices)
270253

271-
masks = _lengths_to_masks(sequence_lengths, array_ops.shape(tag_indices)[1])
254+
masks = array_ops.sequence_mask(sequence_lengths,
255+
maxlen=array_ops.shape(tag_indices)[1],
256+
dtype=dtypes.float32)
272257
truncated_masks = array_ops.slice(masks, [0, 1], [-1, -1])
273258
binary_scores = math_ops.reduce_sum(binary_scores * truncated_masks, 1)
274259
return binary_scores

0 commit comments

Comments
 (0)