def _test_body():
   # pylint: disable=protected-access
   if context.executing_eagerly():
     beam_search_decoder._check_batch_beam(t, batch_size, beam_width)
   else:
     with self.cached_session():
       check_op = beam_search_decoder._check_batch_beam(
           t, batch_size, beam_width)
       self.evaluate(check_op)
Пример #2
0
 def _test_body():
     # pylint: disable=protected-access
     if context.executing_eagerly():
         beam_search_decoder._check_batch_beam(t, batch_size,
                                               beam_width)
     else:
         with self.cached_session():
             check_op = beam_search_decoder._check_batch_beam(
                 t, batch_size, beam_width)
             self.evaluate(check_op)
  def _test_array_shape_dynamic_checks(self, static_shape, dynamic_shape,
                                       batch_size, beam_width, is_valid=True):
    t = array_ops.placeholder_with_default(
        np.random.randn(*static_shape).astype(np.float32),
        shape=dynamic_shape)

    batch_size = array_ops.constant(batch_size)
    check_op = beam_search_decoder._check_batch_beam(t, batch_size, beam_width)  # pylint: disable=protected-access

    with self.cached_session() as sess:
      if is_valid:
        sess.run(check_op)
      else:
        with self.assertRaises(errors.InvalidArgumentError):
          sess.run(check_op)
Пример #4
0
  def _test_array_shape_dynamic_checks(self, static_shape, dynamic_shape,
                                       batch_size, beam_width, is_valid=True):
    t = array_ops.placeholder_with_default(
        np.random.randn(*static_shape).astype(np.float32),
        shape=dynamic_shape)

    batch_size = array_ops.constant(batch_size)
    check_op = beam_search_decoder._check_batch_beam(t, batch_size, beam_width)  # pylint: disable=protected-access

    with self.cached_session() as sess:
      if is_valid:
        sess.run(check_op)
      else:
        with self.assertRaises(errors.InvalidArgumentError):
          sess.run(check_op)
Пример #5
0
    def _maybe_sort_array_beams(self, t, parent_ids, sequence_length):
        """Maybe sorts beams within a `TensorArray`.

        Args:
          t: A `TensorArray` of size `max_time` that contains `Tensor`s of shape
            `[batch_size, beam_width, s]` or `[batch_size * beam_width, s]` where
            `s` is the depth shape.
          parent_ids: The parent ids of shape `[max_time, batch_size, beam_width]`.
          sequence_length: The sequence length of shape `[batch_size, beam_width]`.

        Returns:
          A `TensorArray` where beams are sorted in each `Tensor` or `t` itself if
          it is not a `TensorArray` or does not meet shape requirements.
        """
        if not isinstance(t, tf.TensorArray):
            return t
        # pylint: disable=protected-access
        if (not t._infer_shape or not t._element_shape
                or t._element_shape[0].ndims is None
                or t._element_shape[0].ndims < 1):
            shape = (t._element_shape[0] if t._infer_shape and t._element_shape
                     else tf.TensorShape(None))
            tf.logger.warn(
                "The TensorArray %s in the cell state is not amenable to "
                "sorting based on the beam search result. For a "
                "TensorArray to be sorted, its elements shape must be "
                "defined and have at least a rank of 1, but saw shape: %s" %
                (t.handle.name, shape))
            return t
        # shape = t._element_shape[0]
        # pylint: enable=protected-access
        # if not _check_static_batch_beam_maybe(
        #    shape, tensor_util.constant_value(self._batch_size), self._beam_width):
        #    return t
        t = t.stack()
        with tf.control_dependencies(
            [_check_batch_beam(t, self._batch_size, self._beam_width)]):
            return gather_tree_from_array(t, parent_ids, sequence_length)