FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fixes an issue with calling tf.contrib.seq2seq.dynamic_decode with an… · codeplaysoftware/tensorflow@005b8aa · GitHub

Repository navigation

Commit 005b8aa

Browse files
Fixes an issue with calling tf.contrib.seq2seq.dynamic_decode with an extended BasicDecoder which for example returns a tf.contrib.seq2seq.AttentionWrapperState.
In this case the internal while-loop fails when trying to store an instance tf.contrib.seq2seq.AttentionWrapperState in the internal TensorArray. PiperOrigin-RevId: 190491787
1 parent 9d8779e commit 005b8aa

2 files changed

Lines changed: 11 additions & 19 deletions

File tree

‎tensorflow/contrib/seq2seq/python/ops/decoder.py‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from tensorflow.python.ops import control_flow_ops
3131
from tensorflow.python.ops import math_ops
3232
from tensorflow.python.ops import rnn
33+
from tensorflow.python.ops import rnn_cell_impl
3334
from tensorflow.python.ops import tensor_array_ops
3435
from tensorflow.python.ops import variable_scope
3536
from tensorflow.python.util import nest
@@ -39,6 +40,7 @@
3940

4041

4142
_transpose_batch_time = rnn._transpose_batch_time # pylint: disable=protected-access
43+
_zero_state_tensors = rnn_cell_impl._zero_state_tensors # pylint: disable=protected-access
4244

4345

4446
@six.add_metaclass(abc.ABCMeta)
@@ -133,16 +135,8 @@ def tracks_own_finished(self):
133135

134136
def _create_zero_outputs(size, dtype, batch_size):
135137
"""Create a zero outputs Tensor structure."""
136-
def _t(s):
137-
return (s if isinstance(s, ops.Tensor) else constant_op.constant(
138-
tensor_shape.TensorShape(s).as_list(),
139-
dtype=dtypes.int32,
140-
name="zero_suffix_shape"))
141-
142138
def _create(s, d):
143-
return array_ops.zeros(
144-
array_ops.concat(
145-
([batch_size], _t(s)), axis=0), dtype=d)
139+
return _zero_state_tensors(s, batch_size, d)
146140

147141
return nest.map_structure(_create, size, dtype)
148142

@@ -212,7 +206,8 @@ def dynamic_decode(decoder,
212206
initial_time = constant_op.constant(0, dtype=dtypes.int32)
213207

214208
def _shape(batch_size, from_shape):
215-
if not isinstance(from_shape, tensor_shape.TensorShape):
209+
if (not isinstance(from_shape, tensor_shape.TensorShape) or
210+
from_shape.ndims == 0):
216211
return tensor_shape.TensorShape(None)
217212
else:
218213
batch_size = tensor_util.constant_value(

‎tensorflow/python/ops/rnn.py‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,21 @@
4949

5050

5151
def _transpose_batch_time(x):
52-
"""Transpose the batch and time dimensions of a Tensor.
52+
"""Transposes the batch and time dimensions of a Tensor.
5353
54-
Retains as much of the static shape information as possible.
54+
If the input tensor has rank < 2 it returns the original tensor. Retains as
55+
much of the static shape information as possible.
5556
5657
Args:
57-
x: A tensor of rank 2 or higher.
58+
x: A Tensor.
5859
5960
Returns:
6061
x transposed along the first two dimensions.
61-
62-
Raises:
63-
ValueError: if `x` is rank 1 or lower.
6462
"""
6563
x_static_shape = x.get_shape()
6664
if x_static_shape.ndims is not None and x_static_shape.ndims < 2:
67-
raise ValueError(
68-
"Expected input tensor %s to have rank at least 2, but saw shape: %s" %
69-
(x, x_static_shape))
65+
return x
66+
7067
x_rank = array_ops.rank(x)
7168
x_t = array_ops.transpose(
7269
x, array_ops.concat(

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL