aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/seq2seq
diff options
context:
space:
mode:
authorGravatar Jianwei Xie <xiejw@google.com>2018-01-24 11:31:06 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-01-24 11:35:51 -0800
commitad07a86d75ab06bbcfd6f8f6a24debd9036a52d0 (patch)
tree87041ba0e9c329c3838664345ecc7b5ea1b28787 /tensorflow/contrib/seq2seq
parent7f8e600bfb4ff5973bd1ec178b65538e2446fb69 (diff)
Fixed linter errors.
PiperOrigin-RevId: 183115307
Diffstat (limited to 'tensorflow/contrib/seq2seq')
-rw-r--r--tensorflow/contrib/seq2seq/python/kernel_tests/beam_search_decoder_test.py79
-rw-r--r--tensorflow/contrib/seq2seq/python/ops/beam_search_decoder.py99
2 files changed, 94 insertions, 84 deletions
diff --git a/tensorflow/contrib/seq2seq/python/kernel_tests/beam_search_decoder_test.py b/tensorflow/contrib/seq2seq/python/kernel_tests/beam_search_decoder_test.py
index f498b2bb57..9265540317 100644
--- a/tensorflow/contrib/seq2seq/python/kernel_tests/beam_search_decoder_test.py
+++ b/tensorflow/contrib/seq2seq/python/kernel_tests/beam_search_decoder_test.py
@@ -46,20 +46,18 @@ class TestGatherTree(test.TestCase):
# create (batch_size, max_time, beam_width) matrix and transpose it
predicted_ids = np.array(
- [[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
- [[2, 3, 4], [5, 6, 7], [8, 9, 10]]],
+ [[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[2, 3, 4], [5, 6, 7], [8, 9, 10]]],
dtype=np.int32).transpose([1, 0, 2])
parent_ids = np.array(
- [[[0, 0, 0], [0, 1, 1], [2, 1, 2]],
- [[0, 0, 0], [1, 2, 0], [2, 1, 1]]],
+ [[[0, 0, 0], [0, 1, 1], [2, 1, 2]], [[0, 0, 0], [1, 2, 0], [2, 1, 1]]],
dtype=np.int32).transpose([1, 0, 2])
# sequence_lengths is shaped (batch_size = 3)
max_sequence_lengths = [3, 3]
- expected_result = np.array(
- [[[2, 2, 2], [6, 5, 6], [7, 8, 9]],
- [[2, 4, 4], [7, 6, 6], [8, 9, 10]]]).transpose([1, 0, 2])
+ expected_result = np.array([[[2, 2, 2], [6, 5, 6], [7, 8, 9]],
+ [[2, 4, 4], [7, 6, 6],
+ [8, 9, 10]]]).transpose([1, 0, 2])
res = beam_search_ops.gather_tree(
predicted_ids,
@@ -157,8 +155,8 @@ class TestBeamStep(test.TestCase):
self.assertAllEqual(outputs_.predicted_ids, [[3, 3, 2], [2, 2, 1]])
self.assertAllEqual(outputs_.parent_ids, [[1, 0, 0], [2, 1, 0]])
self.assertAllEqual(next_state_.lengths, [[3, 3, 3], [3, 3, 3]])
- self.assertAllEqual(next_state_.finished, [[False, False, False],
- [False, False, False]])
+ self.assertAllEqual(next_state_.finished,
+ [[False, False, False], [False, False, False]])
expected_log_probs = []
expected_log_probs.append(state_.log_probs[0][[1, 0, 0]])
@@ -212,8 +210,8 @@ class TestBeamStep(test.TestCase):
self.assertAllEqual(outputs_.parent_ids, [[1, 0, 0], [1, 2, 0]])
self.assertAllEqual(outputs_.predicted_ids, [[0, 3, 2], [2, 0, 1]])
self.assertAllEqual(next_state_.lengths, [[1, 3, 3], [3, 1, 3]])
- self.assertAllEqual(next_state_.finished, [[True, False, False],
- [False, True, False]])
+ self.assertAllEqual(next_state_.finished,
+ [[True, False, False], [False, True, False]])
expected_log_probs = []
expected_log_probs.append(state_.log_probs[0][[1, 0, 0]])
@@ -226,9 +224,10 @@ class TestBeamStep(test.TestCase):
class TestLargeBeamStep(test.TestCase):
- """
- Tests a single step of beam search in such
- case that beam size is larger than vocabulary size.
+ """Tests large beam step.
+
+ Tests a single step of beam search in such case that beam size is larger than
+ vocabulary size.
"""
def setUp(self):
@@ -239,19 +238,21 @@ class TestLargeBeamStep(test.TestCase):
self.end_token = 0
self.length_penalty_weight = 0.6
-
def test_step(self):
- def get_probs():
- """this simulates the initialize method in BeamSearchDecoder"""
- log_prob_mask = array_ops.one_hot(array_ops.zeros([self.batch_size],
- dtype=dtypes.int32),
- depth=self.beam_width, on_value=True,
- off_value=False, dtype=dtypes.bool)
- log_prob_zeros = array_ops.zeros([self.batch_size, self.beam_width],
- dtype=dtypes.float32)
- log_prob_neg_inf = array_ops.ones([self.batch_size, self.beam_width],
- dtype=dtypes.float32) * -np.Inf
+ def get_probs():
+ """this simulates the initialize method in BeamSearchDecoder."""
+ log_prob_mask = array_ops.one_hot(
+ array_ops.zeros([self.batch_size], dtype=dtypes.int32),
+ depth=self.beam_width,
+ on_value=True,
+ off_value=False,
+ dtype=dtypes.bool)
+
+ log_prob_zeros = array_ops.zeros(
+ [self.batch_size, self.beam_width], dtype=dtypes.float32)
+ log_prob_neg_inf = array_ops.ones(
+ [self.batch_size, self.beam_width], dtype=dtypes.float32) * -np.Inf
log_probs = array_ops.where(log_prob_mask, log_prob_zeros,
log_prob_neg_inf)
@@ -260,12 +261,15 @@ class TestLargeBeamStep(test.TestCase):
log_probs = get_probs()
dummy_cell_state = array_ops.zeros([self.batch_size, self.beam_width])
+ # pylint: disable=invalid-name
_finished = array_ops.one_hot(
array_ops.zeros([self.batch_size], dtype=dtypes.int32),
- depth=self.beam_width, on_value=False,
- off_value=True, dtype=dtypes.bool)
+ depth=self.beam_width,
+ on_value=False,
+ off_value=True,
+ dtype=dtypes.bool)
_lengths = np.zeros([self.batch_size, self.beam_width], dtype=np.int64)
- _lengths[:, 0]=2
+ _lengths[:, 0] = 2
_lengths = constant_op.constant(_lengths, dtype=dtypes.int64)
beam_state = beam_search_decoder.BeamSearchDecoderState(
@@ -298,20 +302,20 @@ class TestLargeBeamStep(test.TestCase):
length_penalty_weight=self.length_penalty_weight)
with self.test_session() as sess:
- outputs_, next_state_, state_, log_probs_ = sess.run(
+ outputs_, next_state_, _, _ = sess.run(
[outputs, next_beam_state, beam_state, log_probs])
self.assertEqual(outputs_.predicted_ids[0, 0], 3)
self.assertEqual(outputs_.predicted_ids[0, 1], 2)
self.assertEqual(outputs_.predicted_ids[1, 0], 1)
neg_inf = -np.Inf
- self.assertAllEqual(next_state_.log_probs[:, -3:],
- [[neg_inf, neg_inf, neg_inf],
- [neg_inf, neg_inf, neg_inf]])
+ self.assertAllEqual(
+ next_state_.log_probs[:, -3:],
+ [[neg_inf, neg_inf, neg_inf], [neg_inf, neg_inf, neg_inf]])
self.assertEqual((next_state_.log_probs[:, :-3] > neg_inf).all(), True)
self.assertEqual((next_state_.lengths[:, :-3] > 0).all(), True)
- self.assertAllEqual(next_state_.lengths[:, -3:], [[0, 0, 0],
- [0, 0, 0]])
+ self.assertAllEqual(next_state_.lengths[:, -3:], [[0, 0, 0], [0, 0, 0]])
+
class BeamSearchDecoderTest(test.TestCase):
@@ -338,8 +342,8 @@ class BeamSearchDecoderTest(test.TestCase):
initial_state = cell.zero_state(batch_size, dtypes.float32)
if has_attention:
inputs = array_ops.placeholder_with_default(
- np.random.randn(batch_size, decoder_max_time,
- input_depth).astype(np.float32),
+ np.random.randn(batch_size, decoder_max_time, input_depth).astype(
+ np.float32),
shape=(None, None, input_depth))
tiled_inputs = beam_search_decoder.tile_batch(
inputs, multiplier=beam_width)
@@ -359,8 +363,7 @@ class BeamSearchDecoderTest(test.TestCase):
cell_state = cell.zero_state(
dtype=dtypes.float32, batch_size=batch_size_tensor * beam_width)
if has_attention:
- cell_state = cell_state.clone(
- cell_state=initial_state)
+ cell_state = cell_state.clone(cell_state=initial_state)
bsd = beam_search_decoder.BeamSearchDecoder(
cell=cell,
embedding=embedding,
diff --git a/tensorflow/contrib/seq2seq/python/ops/beam_search_decoder.py b/tensorflow/contrib/seq2seq/python/ops/beam_search_decoder.py
index a5f7169c31..d6184d6109 100644
--- a/tensorflow/contrib/seq2seq/python/ops/beam_search_decoder.py
+++ b/tensorflow/contrib/seq2seq/python/ops/beam_search_decoder.py
@@ -37,7 +37,6 @@ from tensorflow.python.ops import rnn_cell_impl
from tensorflow.python.ops import tensor_array_ops
from tensorflow.python.util import nest
-
__all__ = [
"BeamSearchDecoderOutput",
"BeamSearchDecoderState",
@@ -48,8 +47,8 @@ __all__ = [
class BeamSearchDecoderState(
- collections.namedtuple("BeamSearchDecoderState", ("cell_state", "log_probs",
- "finished", "lengths"))):
+ collections.namedtuple("BeamSearchDecoderState",
+ ("cell_state", "log_probs", "finished", "lengths"))):
pass
@@ -85,11 +84,12 @@ def _tile_batch(t, multiplier):
tiled_static_batch_size = (
t.shape[0].value * multiplier if t.shape[0].value is not None else None)
tiled = array_ops.tile(array_ops.expand_dims(t, 1), tiling)
- tiled = array_ops.reshape(
- tiled, array_ops.concat(([shape_t[0] * multiplier], shape_t[1:]), 0))
+ tiled = array_ops.reshape(tiled,
+ array_ops.concat(
+ ([shape_t[0] * multiplier], shape_t[1:]), 0))
tiled.set_shape(
- tensor_shape.TensorShape(
- [tiled_static_batch_size]).concatenate(t.shape[1:]))
+ tensor_shape.TensorShape([tiled_static_batch_size]).concatenate(
+ t.shape[1:]))
return tiled
@@ -197,8 +197,8 @@ class BeamSearchDecoder(decoder.Decoder):
"""
if not rnn_cell_impl._like_rnncell(cell): # pylint: disable=protected-access
raise TypeError("cell must be an RNNCell, received: %s" % type(cell))
- if (output_layer is not None
- and not isinstance(output_layer, layers_base.Layer)):
+ if (output_layer is not None and
+ not isinstance(output_layer, layers_base.Layer)):
raise TypeError(
"output_layer must be a Layer, received: %s" % type(output_layer))
self._cell = cell
@@ -223,16 +223,17 @@ class BeamSearchDecoder(decoder.Decoder):
self._beam_width = beam_width
self._length_penalty_weight = length_penalty_weight
self._initial_cell_state = nest.map_structure(
- self._maybe_split_batch_beams,
- initial_state, self._cell.state_size)
+ self._maybe_split_batch_beams, initial_state, self._cell.state_size)
self._start_tokens = array_ops.tile(
array_ops.expand_dims(self._start_tokens, 1), [1, self._beam_width])
self._start_inputs = self._embedding_fn(self._start_tokens)
-
+
self._finished = array_ops.one_hot(
array_ops.zeros([self._batch_size], dtype=dtypes.int32),
- depth=self._beam_width, on_value=False,
- off_value=True, dtype=dtypes.bool)
+ depth=self._beam_width,
+ on_value=False,
+ off_value=True,
+ dtype=dtypes.bool)
@property
def batch_size(self):
@@ -250,8 +251,7 @@ class BeamSearchDecoder(decoder.Decoder):
# dimensions to get the output size of the rnn with the layer
# applied to the top.
output_shape_with_unknown_batch = nest.map_structure(
- lambda s: tensor_shape.TensorShape([None]).concatenate(s),
- size)
+ lambda s: tensor_shape.TensorShape([None]).concatenate(s), size)
layer_output_shape = self._output_layer.compute_output_shape(
output_shape_with_unknown_batch)
return nest.map_structure(lambda s: s[1:], layer_output_shape)
@@ -302,10 +302,11 @@ class BeamSearchDecoder(decoder.Decoder):
log_probs = array_ops.one_hot( # shape(batch_sz, beam_sz)
array_ops.zeros([self._batch_size], dtype=dtypes.int32),
- depth=self._beam_width, on_value=0.0, off_value=-np.Inf,
+ depth=self._beam_width,
+ on_value=0.0,
+ off_value=-np.Inf,
dtype=nest.flatten(self._initial_cell_state)[0].dtype)
-
initial_state = BeamSearchDecoderState(
cell_state=self._initial_cell_state,
log_probs=log_probs,
@@ -365,11 +366,12 @@ class BeamSearchDecoder(decoder.Decoder):
t_shape = array_ops.shape(t)
static_batch_size = tensor_util.constant_value(self._batch_size)
batch_size_beam_width = (
- None if static_batch_size is None
- else static_batch_size * self._beam_width)
+ None
+ if static_batch_size is None else static_batch_size * self._beam_width)
reshaped_t = array_ops.reshape(
- t, array_ops.concat(
- ([self._batch_size * self._beam_width], t_shape[2:]), 0))
+ t,
+ array_ops.concat(([self._batch_size * self._beam_width], t_shape[2:]),
+ 0))
reshaped_t.set_shape(
(tensor_shape.TensorShape([batch_size_beam_width]).concatenate(s)))
return reshaped_t
@@ -398,8 +400,9 @@ class BeamSearchDecoder(decoder.Decoder):
s = tensor_shape.TensorShape(s)
t_shape = array_ops.shape(t)
reshaped_t = array_ops.reshape(
- t, array_ops.concat(
- ([self._batch_size, self._beam_width], t_shape[1:]), 0))
+ t,
+ array_ops.concat(([self._batch_size, self._beam_width], t_shape[1:]),
+ 0))
static_batch_size = tensor_util.constant_value(self._batch_size)
expected_reshaped_shape = tensor_shape.TensorShape(
[static_batch_size, self._beam_width]).concatenate(s)
@@ -409,8 +412,8 @@ class BeamSearchDecoder(decoder.Decoder):
"We expected it to have shape "
"(batch_size, beam_width, depth) == %s. Perhaps you "
"forgot to create a zero_state with "
- "batch_size=encoder_batch_size * beam_width?"
- % (reshaped_t.shape, expected_reshaped_shape))
+ "batch_size=encoder_batch_size * beam_width?" %
+ (reshaped_t.shape, expected_reshaped_shape))
reshaped_t.set_shape(expected_reshaped_shape)
return reshaped_t
@@ -482,15 +485,13 @@ class BeamSearchDecoder(decoder.Decoder):
cell_state = state.cell_state
inputs = nest.map_structure(
lambda inp: self._merge_batch_beams(inp, s=inp.shape[2:]), inputs)
- cell_state = nest.map_structure(
- self._maybe_merge_batch_beams,
- cell_state, self._cell.state_size)
+ cell_state = nest.map_structure(self._maybe_merge_batch_beams, cell_state,
+ self._cell.state_size)
cell_outputs, next_cell_state = self._cell(inputs, cell_state)
cell_outputs = nest.map_structure(
lambda out: self._split_batch_beams(out, out.shape[1:]), cell_outputs)
next_cell_state = nest.map_structure(
- self._maybe_split_batch_beams,
- next_cell_state, self._cell.state_size)
+ self._maybe_split_batch_beams, next_cell_state, self._cell.state_size)
if self._output_layer is not None:
cell_outputs = self._output_layer(cell_outputs)
@@ -553,7 +554,8 @@ def _beam_search_step(time, logits, next_cell_state, beam_state, batch_size,
lengths_to_add = array_ops.one_hot(
indices=array_ops.fill([batch_size, beam_width], end_token),
depth=vocab_size,
- on_value=np.int64(0), off_value=np.int64(1),
+ on_value=np.int64(0),
+ off_value=np.int64(1),
dtype=dtypes.int64)
add_mask = math_ops.to_int64(math_ops.logical_not(previously_finished))
lengths_to_add *= array_ops.expand_dims(add_mask, 2)
@@ -572,8 +574,8 @@ def _beam_search_step(time, logits, next_cell_state, beam_state, batch_size,
scores_flat = array_ops.reshape(scores, [batch_size, -1])
# Pick the next beams according to the specified successors function
- next_beam_size = ops.convert_to_tensor(beam_width, dtype=dtypes.int32,
- name="beam_width")
+ next_beam_size = ops.convert_to_tensor(
+ beam_width, dtype=dtypes.int32, name="beam_width")
next_beam_scores, word_indices = nn_ops.top_k(scores_flat, k=next_beam_size)
next_beam_scores.set_shape([static_batch_size, beam_width])
@@ -592,11 +594,11 @@ def _beam_search_step(time, logits, next_cell_state, beam_state, batch_size,
# name="next_beam_word_ids")
# would be a lot cleaner but for reasons unclear, that hides the results of
# the op which prevents capturing it with tfdbg debug ops.
- raw_next_word_ids = math_ops.mod(word_indices, vocab_size,
- name="next_beam_word_ids")
+ raw_next_word_ids = math_ops.mod(
+ word_indices, vocab_size, name="next_beam_word_ids")
next_word_ids = math_ops.to_int32(raw_next_word_ids)
- next_beam_ids = math_ops.to_int32(word_indices / vocab_size,
- name="next_beam_parent_ids")
+ next_beam_ids = math_ops.to_int32(
+ word_indices / vocab_size, name="next_beam_parent_ids")
# Append new ids to current predictions
previously_finished = _tensor_gather_helper(
@@ -605,9 +607,10 @@ def _beam_search_step(time, logits, next_cell_state, beam_state, batch_size,
batch_size=batch_size,
range_size=beam_width,
gather_shape=[-1])
- next_finished = math_ops.logical_or(previously_finished,
- math_ops.equal(next_word_ids, end_token),
- name="next_beam_finished")
+ next_finished = math_ops.logical_or(
+ previously_finished,
+ math_ops.equal(next_word_ids, end_token),
+ name="next_beam_finished")
# Calculate the length of the next predictions.
# 1. Finished beams remain unchanged.
@@ -768,8 +771,12 @@ def _maybe_tensor_gather_helper(gather_indices, gather_from, batch_size,
return gather_from
-def _tensor_gather_helper(gather_indices, gather_from, batch_size,
- range_size, gather_shape, name=None):
+def _tensor_gather_helper(gather_indices,
+ gather_from,
+ batch_size,
+ range_size,
+ gather_shape,
+ name=None):
"""Helper for gathering the right indices from the tensor.
This works by reshaping gather_from to gather_shape (e.g. [-1]) and then
@@ -800,9 +807,9 @@ def _tensor_gather_helper(gather_indices, gather_from, batch_size,
array_ops.reshape(gather_from, gather_shape), gather_indices)
final_shape = array_ops.shape(gather_from)[:1 + len(gather_shape)]
static_batch_size = tensor_util.constant_value(batch_size)
- final_static_shape = (tensor_shape.TensorShape([static_batch_size])
- .concatenate(
- gather_from.shape[1:1 + len(gather_shape)]))
+ final_static_shape = (
+ tensor_shape.TensorShape([static_batch_size]).concatenate(
+ gather_from.shape[1:1 + len(gather_shape)]))
output = array_ops.reshape(output, final_shape, name="output")
output.set_shape(final_static_shape)
return output