code: # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  
  """Tests for contrib.seq2seq.python.ops.seq2seq."""
  # pylint: disable=unused-import,g-bad-import-order
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  # pylint: enable=unused-import
  
  import tensorflow as tf
  from tensorflow.contrib import layers
  
  class Seq2SeqTest(tf.test.TestCase):
  
    # test a default call of rnn_decoder
    def test_rnn_decoder(self):
      pass
  
    # test default call with time_major=True
    def test_dynamic_rnn_decoder_time_major(self):
      with self.test_session() as sess:
        with tf.variable_scope("root", initializer=
                               tf.constant_initializer(0.5)) as varscope:
          # Define inputs/outputs to model
          batch_size = 2
          encoder_embedding_size = 3
          decoder_embedding_size = 4
          encoder_hidden_size = 5
          decoder_hidden_size = encoder_hidden_size
          input_sequence_length = 6
          decoder_sequence_length = 7
          num_decoder_symbols = 20
          start_of_sequence_id = end_of_sequence_id = 1
          decoder_embeddings = tf.get_variable('decoder_embeddings',
              [num_decoder_symbols, decoder_embedding_size],
              initializer=tf.random_normal_initializer(stddev=0.1))
          inputs = tf.constant(0.5, shape=[input_sequence_length, batch_size,
                                           encoder_embedding_size])
          decoder_inputs = tf.constant(0.4, shape=[decoder_sequence_length,
                                                   batch_size,
                                                   decoder_embedding_size])
          decoder_length = tf.constant(decoder_sequence_length, dtype=tf.int32,
                                       shape=[batch_size,])
          with tf.variable_scope("rnn") as scope:
            # setting up weights for computing the final output
            output_fn = lambda x: layers.linear(x, num_decoder_symbols,
                                                scope=scope)
  
            # Define model
            encoder_outputs, encoder_state = tf.nn.dynamic_rnn(
                cell=tf.nn.rnn_cell.GRUCell(encoder_hidden_size), inputs=inputs,
                dtype=tf.float32, time_major=True, scope=scope)
  
  
          with tf.variable_scope("decoder") as scope:
            # Train decoder
            decoder_cell = tf.nn.rnn_cell.GRUCell(decoder_hidden_size)
            decoder_fn_train = tf.contrib.seq2seq.simple_decoder_fn_train(
                encoder_state=encoder_state)
            decoder_outputs_train, decoder_state_train = (
              tf.contrib.seq2seq.dynamic_rnn_decoder(
                  cell=decoder_cell,
                  decoder_fn=decoder_fn_train,
                  inputs=decoder_inputs,
                  sequence_length=decoder_length,
                  time_major=True,
                  scope=scope))
            decoder_outputs_train = output_fn(decoder_outputs_train)
  
            # Setup variable reuse
            scope.reuse_variables()
  
            # Inference decoder
            decoder_fn_inference = (
                tf.contrib.seq2seq.simple_decoder_fn_inference(
                  output_fn=output_fn,
                  encoder_state=encoder_state,
                  embeddings=decoder_embeddings,
                  start_of_sequence_id=start_of_sequence_id,
                  end_of_sequence_id=end_of_sequence_id,
                  #TODO: find out why it goes to +1
                  maximum_length=decoder_sequence_length-1,
                  num_decoder_symbols=num_decoder_symbols,
                  dtype=tf.int32))
            decoder_outputs_inference, decoder_state_inference = (
                tf.contrib.seq2seq.dynamic_rnn_decoder(
                  cell=decoder_cell,
                  decoder_fn=decoder_fn_inference,
                  time_major=True,
                  scope=scope))
  
          # Run model
          tf.global_variables_initializer().run()
          decoder_outputs_train_res, decoder_state_train_res = sess.run(
              [decoder_outputs_train, decoder_state_train])
          decoder_outputs_inference_res, decoder_state_inference_res = sess.run(
              [decoder_outputs_inference, decoder_state_inference])
  
          # Assert outputs
          self.assertEqual((decoder_sequence_length, batch_size,
                            num_decoder_symbols),
                           decoder_outputs_train_res.shape)
          self.assertEqual((batch_size, num_decoder_symbols),
                           decoder_outputs_inference_res.shape[1:3])
          self.assertEqual((batch_size, decoder_hidden_size),
                           decoder_state_train_res.shape)
          self.assertEqual((batch_size, decoder_hidden_size),
                           decoder_state_inference_res.shape)
          # The dynamic decoder might end earlier than `maximal_length`
          # under inference
          true_value = (decoder_sequence_length>=
                        decoder_state_inference_res.shape[0])
          self.assertEqual((true_value), True)
  
  if __name__ == '__main__':
    tf.test.main()
   # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  """Tests for device function for replicated training."""
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import tensorflow as tf
  
  
  class DeviceSetterTest(tf.test.TestCase):
  
    _cluster_spec = tf.train.ClusterSpec({
        "ps": ["ps0:2222", "ps1:2222"],
        "worker": ["worker0:2222", "worker1:2222", "worker2:2222"]})
  
    def testCPUOverride(self):
      with tf.device(tf.train.replica_device_setter(cluster=self._cluster_spec)):
        with tf.device("/cpu:0"):
          v = tf.Variable([1, 2])
        w = tf.Variable([2, 1])
        with tf.device("/cpu:0"):
          a = v + w
        self.assertDeviceEqual("/job:ps/task:0/cpu:0", v.device)
        self.assertDeviceEqual("/job:ps/task:0/cpu:0", v.initializer.device)
        self.assertDeviceEqual("/job:ps/task:1", w.device)
        self.assertDeviceEqual("/job:ps/task:1", w.initializer.device)
        self.assertDeviceEqual("/job:worker/cpu:0", a.device)
  
    def testPS2TasksWithClusterSpecClass(self):
      with tf.device(tf.train.replica_device_setter(cluster=self._cluster_spec)):
        v = tf.Variable([1, 2])
        w = tf.Variable([2, 1])
        a = v + w
        self.assertDeviceEqual("/job:ps/task:0", v.device)
        self.assertDeviceEqual("/job:ps/task:0", v.initializer.device)
        self.assertDeviceEqual("/job:ps/task:1", w.device)
        self.assertDeviceEqual("/job:ps/task:1", w.initializer.device)
        self.assertDeviceEqual("/job:worker", a.device)
  
    def testPS2TasksWithClusterSpecDict(self):
      with tf.device(tf.train.replica_device_setter(
          cluster=self._cluster_spec.as_dict())):
        v = tf.Variable([1, 2])
        w = tf.Variable([2, 1])
        a = v + w
        self.assertDeviceEqual("/job:ps/task:0", v.device)
        self.assertDeviceEqual("/job:ps/task:0", v.initializer.device)
        self.assertDeviceEqual("/job:ps/task:1", w.device)
        self.assertDeviceEqual("/job:ps/task:1", w.initializer.device)
        self.assertDeviceEqual("/job:worker", a.device)
  
    def testPS2TasksWithClusterDef(self):
      with tf.device(tf.train.replica_device_setter(
          cluster=self._cluster_spec.as_cluster_def())):
        v = tf.Variable([1, 2])
        w = tf.Variable([2, 1])
        a = v + w
        self.assertDeviceEqual("/job:ps/task:0", v.device)
        self.assertDeviceEqual("/job:ps/task:0", v.initializer.device)
        self.assertDeviceEqual("/job:ps/task:1", w.device)
        self.assertDeviceEqual("/job:ps/task:1", w.initializer.device)
        self.assertDeviceEqual("/job:worker", a.device)
  
    def testPS2TasksWithDevice(self):
      cluster_spec = tf.train.ClusterSpec({
          "sun": ["sun0:2222", "sun1:2222", "sun2:2222"],
          "moon": ["moon0:2222", "moon1:2222"]})
  
      with tf.device(tf.train.replica_device_setter(
          ps_device="/job:moon", worker_device="/job:sun",
          cluster=cluster_spec.as_cluster_def())):
        v = tf.Variable([1, 2])
        w = tf.Variable([2, 1])
        a = v + w
        self.assertDeviceEqual("/job:moon/task:0", v.device)
        self.assertDeviceEqual("/job:moon/task:0", v.initializer.device)
        self.assertDeviceEqual("/job:moon/task:1", w.device)
        self.assertDeviceEqual("/job:moon/task:1", w.initializer.device)
        self.assertDeviceEqual("/job:sun", a.device)
  
    def testPS2TasksWithCPUConstraint(self):
      cluster_spec = tf.train.ClusterSpec({
          "sun": ["sun0:2222", "sun1:2222", "sun2:2222"],
          "moon": ["moon0:2222", "moon1:2222"]})
  
      with tf.device(tf.train.replica_device_setter(
          ps_device="/job:moon/cpu:0", worker_device="/job:sun",
          cluster=cluster_spec.as_cluster_def())):
        v = tf.Variable([1, 2])
        w = tf.Variable([2, 1])
        a = v + w
        self.assertDeviceEqual("/job:moon/task:0/cpu:0", v.device)
        self.assertDeviceEqual("/job:moon/task:0/cpu:0", v.initializer.device)
        self.assertDeviceEqual("/job:moon/task:1/cpu:0", w.device)
        self.assertDeviceEqual("/job:moon/task:1/cpu:0", w.initializer.device)
        self.assertDeviceEqual("/job:sun", a.device)
  
  
  if __name__ == "__main__":
    tf.test.main()
   # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  
  """Parsing Ops."""
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import collections
  import re
  
  from tensorflow.python.framework import constant_op
  from tensorflow.python.framework import dtypes
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import sparse_tensor
  from tensorflow.python.framework import tensor_shape
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import control_flow_ops
  from tensorflow.python.ops import gen_parsing_ops
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import sparse_ops
  # go/tf-wildcard-import
  # pylint: disable=wildcard-import,undefined-variable
  from tensorflow.python.ops.gen_parsing_ops import *
  # pylint: enable=wildcard-import,undefined-variable
  
  
  ops.NotDifferentiable("DecodeRaw")
  ops.NotDifferentiable("ParseTensor")
  ops.NotDifferentiable("StringToNumber")
  
  
  class VarLenFeature(collections.namedtuple("VarLenFeature", ["dtype"])):
    """Configuration for parsing a variable-length input feature.
  
    Fields:
      dtype: Data type of input.
    """
    pass
  
  
  class SparseFeature(
      collections.namedtuple(
          "SparseFeature",
          ["index_key", "value_key", "dtype", "size", "already_sorted"])):
    """Configuration for parsing a sparse input feature.
  
    Fields:
      index_key: Name of index feature.  The underlying feature's type must
        be `int64` and its length must always match that of the `value_key`
        feature.
      value_key: Name of value feature.  The underlying feature's type must
        be `dtype` and its length must always match that of the `index_key`
        feature.
      dtype: Data type of the `value_key` feature.
      size: Each value in the `index_key` feature must be in `[0, size)`.
      already_sorted: A boolean to specify whether the values in `index_key` are
        already sorted. If so skip sorting, False by default (optional).
    """
    pass
  SparseFeature.__new__.__defaults__ = (False,)
  
  
  class FixedLenFeature(collections.namedtuple(
      "FixedLenFeature", ["shape", "dtype", "default_value"])):
    """Configuration for parsing a fixed-length input feature.
  
    To treat sparse input as dense, provide a `default_value`; otherwise,
    the parse functions will fail on any examples missing this feature.
  
    Fields:
      shape: Shape of input data.
      dtype: Data type of input.
      default_value: Value to be used if an example is missing this feature. It
          must be compatible with `dtype`.
    """
    pass
  FixedLenFeature.__new__.__defaults__ = (None,)
  
  
  # NOTE: If we ever support a default_value for sequence dense features, we can
  # remove this class and use FixedLenFeature in its place.
  class FixedLenSequenceFeature(collections.namedtuple(
      "FixedLenSequenceFeature", ["shape", "dtype", "allow_missing"])):
    """Configuration for a dense input feature in a sequence item.
  
    To treat a sparse input as dense, provide `allow_missing=True`; otherwise,
    the parse functions will fail on any examples missing this feature.
  
    Fields:
      shape: Shape of input data.
      dtype: Data type of input.
      allow_missing: Whether to allow this feature to be missing from a feature
        list item.
    """
    pass
  FixedLenSequenceFeature.__new__.__defaults__ = (False,)
  
  
  def _features_to_raw_params(features, types):
    """Split feature tuples into raw params used by `gen_parsing_ops`.
  
    Args:
      features: A `dict` mapping feature keys to objects of a type in `types`.
      types: Type of features to allow, among `FixedLenFeature`, `VarLenFeature`,
        `SparseFeature`, and `FixedLenSequenceFeature`.
  
    Returns:
      Tuple of `sparse_keys`, `sparse_types`, `dense_keys`, `dense_types`,
        `dense_defaults`, `dense_shapes`.
  
    Raises:
      ValueError: if `features` contains an item not in `types`, or an invalid
          feature.
    """
    sparse_keys = []
    sparse_types = []
    dense_keys = []
    dense_types = []
    dense_defaults = {}
    dense_shapes = []
    if features:
      # NOTE: We iterate over sorted keys to keep things deterministic.
      for key in sorted(features.keys()):
        feature = features[key]
        if isinstance(feature, VarLenFeature):
          if VarLenFeature not in types:
            raise ValueError("Unsupported VarLenFeature %s.", feature)
          if not feature.dtype:
            raise ValueError("Missing type for feature %s." % key)
          sparse_keys.append(key)
          sparse_types.append(feature.dtype)
        elif isinstance(feature, SparseFeature):
          if SparseFeature not in types:
            raise ValueError("Unsupported SparseFeature %s.", feature)
          if not feature.index_key:
            raise ValueError(
                "Missing index_key for SparseFeature %s.", feature)
          if not feature.value_key:
            raise ValueError(
                "Missing value_key for SparseFeature %s.", feature)
          if not feature.dtype:
            raise ValueError("Missing type for feature %s." % key)
          if feature.index_key in sparse_keys:
            dtype = sparse_types[sparse_keys.index(feature.index_key)]
            if dtype != dtypes.int64:
              raise ValueError("Conflicting type %s vs int64 for feature %s." % (
                  dtype, feature.index_key))
          else:
            sparse_keys.append(feature.index_key)
            sparse_types.append(dtypes.int64)
  
          if feature.value_key in sparse_keys:
            dtype = sparse_types[sparse_keys.index(feature.value_key)]
            if dtype != feature.dtype:
              raise ValueError("Conflicting type %s vs %s for feature %s." % (
                  dtype, feature.dtype, feature.value_key))
          else:
            sparse_keys.append(feature.value_key)
            sparse_types.append(feature.dtype)
        elif isinstance(feature, FixedLenFeature):
          if FixedLenFeature not in types:
            raise ValueError("Unsupported FixedLenFeature %s.", feature)
          if not feature.dtype:
            raise ValueError("Missing type for feature %s." % key)
          if feature.shape is None:
            raise ValueError("Missing shape for feature %s." % key)
          dense_keys.append(key)
          dense_shapes.append(feature.shape)
          dense_types.append(feature.dtype)
          if feature.default_value is not None:
            dense_defaults[key] = feature.default_value
        elif isinstance(feature, FixedLenSequenceFeature):
          if FixedLenSequenceFeature not in types:
            raise ValueError("Unsupported FixedLenSequenceFeature %s.", feature)
          if not feature.dtype:
            raise ValueError("Missing type for feature %s." % key)
          if feature.shape is None:
            raise ValueError("Missing shape for feature %s." % key)
          dense_keys.append(key)
          dense_shapes.append(feature.shape)
          dense_types.append(feature.dtype)
          if feature.allow_missing:
            dense_defaults[key] = None
        else:
          raise ValueError("Invalid feature %s:%s." % (key, feature))
    return (
        sparse_keys, sparse_types, dense_keys, dense_types, dense_defaults,
        dense_shapes)
  
  
  def _construct_sparse_tensors_for_sparse_features(features, tensor_dict):
    """Merges SparseTensors of indices and values of SparseFeatures.
  
    Updates `tensor_dict`. For `SparseFeatures` in the values of `features`
    expects their `index_key`s and `index_value`s to be present in `tensor_dict`
    mapping to `SparseTensor`s. Removes those, constructs a single `SparseTensor`
    from them, and adds it to `tensor_dict` with the key from `features`.
  
    Args:
      features: A `dict` mapping feature keys to `SparseFeature` values.
        Values of other types will be ignored.
      tensor_dict: A `dict` mapping feature keys to `Tensor` and `SparseTensor`
        values. Expected to contain keys of the `SparseFeature`s' `index_key`s and
        `value_key`s and mapping them to `SparseTensor`s.
    """
    # Construct SparseTensors for SparseFeatures.
    for key in sorted(features.keys()):
      feature = features[key]
      if isinstance(feature, SparseFeature):
        sp_ids = tensor_dict[feature.index_key]
        sp_values = tensor_dict[feature.value_key]
        tensor_dict[key] = sparse_ops.sparse_merge(
            sp_ids,
            sp_values,
            feature.size,
            feature.already_sorted)
    # Remove tensors from dictionary that were only used to construct
    # SparseTensors for SparseFeature.
    for key in set(tensor_dict.keys()) - set(features.keys()):
      del tensor_dict[key]
  
  
  def parse_example(serialized, features, name=None, example_names=None):
    # pylint: disable=line-too-long
    """Parses `Example` protos into a `dict` of tensors.
  
    Parses a number of serialized [`Example`](https://www.tensorflow.org/code/tensorflow/core/example/example.proto)
    protos given in `serialized`.
  
    `example_names` may contain descriptive names for the corresponding serialized
    protos. These may be useful for debugging purposes, but they have no effect on
    the output. If not `None`, `example_names` must be the same length as
    `serialized`.
  
    This op parses serialized examples into a dictionary mapping keys to `Tensor`
    and `SparseTensor` objects. `features` is a dict from keys to `VarLenFeature`,
    `SparseFeature`, and `FixedLenFeature` objects. Each `VarLenFeature`
    and `SparseFeature` is mapped to a `SparseTensor`, and each
    `FixedLenFeature` is mapped to a `Tensor`.
  
    Each `VarLenFeature` maps to a `SparseTensor` of the specified type
    representing a ragged matrix. Its indices are `[batch, index]` where `batch`
    is the batch entry the value is from in `serialized`, and `index` is the
    value's index in the list of values associated with that feature and example.
  
    Each `SparseFeature` maps to a `SparseTensor` of the specified type
    representing a sparse matrix of shape
    `(serialized.size(), SparseFeature.size)`. Its indices are `[batch, index]`
    where `batch` is the batch entry the value is from in `serialized`, and
    `index` is the value's index is given by the values in the
    `SparseFeature.index_key` feature column.
  
    Each `FixedLenFeature` `df` maps to a `Tensor` of the specified type (or
    `tf.float32` if not specified) and shape `(serialized.size(),) + df.shape`.
  
    `FixedLenFeature` entries with a `default_value` are optional. With no default
    value, we will fail if that `Feature` is missing from any example in
    `serialized`.
  
    Examples:
  
    For example, if one expects a `tf.float32` sparse feature `ft` and three
    serialized `Example`s are provided:
  
    ```
    serialized = [
      features
        { feature { key: "ft" value { float_list { value: [1.0, 2.0] } } } },
      features
        { feature []},
      features
        { feature { key: "ft" value { float_list { value: [3.0] } } }
    ]
    ```
  
    then the output will look like:
  
    ```
    {"ft": SparseTensor(indices=[[0, 0], [0, 1], [2, 0]],
                        values=[1.0, 2.0, 3.0],
                        dense_shape=(3, 2)) }
    ```
  
    Given two `Example` input protos in `serialized`:
  
    ```
    [
      features {
        feature { key: "kw" value { bytes_list { value: [ "knit", "big" ] } } }
        feature { key: "gps" value { float_list { value: [] } } }
      },
      features {
        feature { key: "kw" value { bytes_list { value: [ "emmy" ] } } }
        feature { key: "dank" value { int64_list { value: [ 42 ] } } }
        feature { key: "gps" value { } }
      }
    ]
    ```
  
    And arguments
  
    ```
    example_names: ["input0", "input1"],
    features: {
        "kw": VarLenFeature(tf.string),
        "dank": VarLenFeature(tf.int64),
        "gps": VarLenFeature(tf.float32),
    }
    ```
  
    Then the output is a dictionary:
  
    ```python
    {
      "kw": SparseTensor(
          indices=[[0, 0], [0, 1], [1, 0]],
          values=["knit", "big", "emmy"]
          dense_shape=[2, 2]),
      "dank": SparseTensor(
          indices=[[1, 0]],
          values=[42],
          dense_shape=[2, 1]),
      "gps": SparseTensor(
          indices=[],
          values=[],
          dense_shape=[2, 0]),
    }
    ```
  
    For dense results in two serialized `Example`s:
  
    ```
    [
      features {
        feature { key: "age" value { int64_list { value: [ 0 ] } } }
        feature { key: "gender" value { bytes_list { value: [ "f" ] } } }
       },
       features {
        feature { key: "age" value { int64_list { value: [] } } }
        feature { key: "gender" value { bytes_list { value: [ "f" ] } } }
      }
    ]
    ```
  
    We can use arguments:
  
    ```
    example_names: ["input0", "input1"],
    features: {
        "age": FixedLenFeature([], dtype=tf.int64, default_value=-1),
        "gender": FixedLenFeature([], dtype=tf.string),
    }
    ```
  
    And the expected output is:
  
    ```python
    {
      "age": [[0], [-1]],
      "gender": [["f"], ["f"]],
    }
    ```
  
    Given two `Example` input protos in `serialized`:
  
    ```
    [
      features {
        feature { key: "val" value { float_list { value: [ 0.5, -1.0 ] } } }
        feature { key: "ix" value { int64_list { value: [ 3, 20 ] } } }
      },
      features {
        feature { key: "val" value { float_list { value: [ 0.0 ] } } }
        feature { key: "ix" value { int64_list { value: [ 42 ] } } }
      }
    ]
    ```
  
    And arguments
  
    ```
    example_names: ["input0", "input1"],
    features: {
        "sparse": SparseFeature("ix", "val", tf.float32, 100),
    }
    ```
  
    Then the output is a dictionary:
  
    ```python
    {
      "sparse": SparseTensor(
          indices=[[0, 3], [0, 20], [1, 42]],
          values=[0.5, -1.0, 0.0]
          dense_shape=[2, 100]),
    }
    ```
  
    Args:
      serialized: A vector (1-D Tensor) of strings, a batch of binary
        serialized `Example` protos.
      features: A `dict` mapping feature keys to `FixedLenFeature`,
        `VarLenFeature`, and `SparseFeature` values.
      name: A name for this operation (optional).
      example_names: A vector (1-D Tensor) of strings (optional), the names of
        the serialized protos in the batch.
  
    Returns:
      A `dict` mapping feature keys to `Tensor` and `SparseTensor` values.
  
    Raises:
      ValueError: if any feature is invalid.
    """
    if not features:
      raise ValueError("Missing: features was %s." % features)
    (sparse_keys, sparse_types, dense_keys, dense_types, dense_defaults,
     dense_shapes) = _features_to_raw_params(
         features, [VarLenFeature, SparseFeature, FixedLenFeature])
    outputs = _parse_example_raw(
        serialized, example_names, sparse_keys, sparse_types, dense_keys,
        dense_types, dense_defaults, dense_shapes, name)
    _construct_sparse_tensors_for_sparse_features(features, outputs)
    return outputs
  
  
  def _parse_example_raw(serialized,
                         names=None,
                         sparse_keys=None,
                         sparse_types=None,
                         dense_keys=None,
                         dense_types=None,
                         dense_defaults=None,
                         dense_shapes=None,
                         name=None):
    """Parses `Example` protos.
  
    Args:
      serialized: A vector (1-D Tensor) of strings, a batch of binary
        serialized `Example` protos.
      names: A vector (1-D Tensor) of strings (optional), the names of
        the serialized protos.
      sparse_keys: A list of string keys in the examples' features.
        The results for these keys will be returned as `SparseTensor` objects.
      sparse_types: A list of `DTypes` of the same length as `sparse_keys`.
        Only `tf.float32` (`FloatList`), `tf.int64` (`Int64List`),
        and `tf.string` (`BytesList`) are supported.
      dense_keys: A list of string keys in the examples' features.
        The results for these keys will be returned as `Tensor`s
      dense_types: A list of DTypes of the same length as `dense_keys`.
        Only `tf.float32` (`FloatList`), `tf.int64` (`Int64List`),
        and `tf.string` (`BytesList`) are supported.
      dense_defaults: A dict mapping string keys to `Tensor`s.
        The keys of the dict must match the dense_keys of the feature.
      dense_shapes: A list of tuples with the same length as `dense_keys`.
        The shape of the data for each dense feature referenced by `dense_keys`.
        Required for any input tensors identified by `dense_keys` whose shapes are
        anything other than `[]` or `[1]`.
      name: A name for this operation (optional).
  
    Returns:
      A `dict` mapping keys to `Tensor`s and `SparseTensor`s.
  
    Raises:
      ValueError: If sparse and dense key sets intersect, or input lengths do not
        match up.
    """
    with ops.name_scope(name, "ParseExample", [serialized, names]):
      names = [] if names is None else names
      dense_defaults = {} if dense_defaults is None else dense_defaults
      sparse_keys = [] if sparse_keys is None else sparse_keys
      sparse_types = [] if sparse_types is None else sparse_types
      dense_keys = [] if dense_keys is None else dense_keys
      dense_types = [] if dense_types is None else dense_types
      dense_shapes = (
          [[]] * len(dense_keys) if dense_shapes is None else dense_shapes)
  
      num_dense = len(dense_keys)
      num_sparse = len(sparse_keys)
  
      if len(dense_shapes) != num_dense:
        raise ValueError("len(dense_shapes) != len(dense_keys): %d vs. %d"
                         % (len(dense_shapes), num_dense))
      if len(dense_types) != num_dense:
        raise ValueError("len(dense_types) != len(num_dense): %d vs. %d"
                         % (len(dense_types), num_dense))
      if len(sparse_types) != num_sparse:
        raise ValueError("len(sparse_types) != len(sparse_keys): %d vs. %d"
                         % (len(sparse_types), num_sparse))
      if num_dense + num_sparse == 0:
        raise ValueError("Must provide at least one sparse key or dense key")
      if not set(dense_keys).isdisjoint(set(sparse_keys)):
        raise ValueError(
            "Dense and sparse keys must not intersect; intersection: %s" %
            set(dense_keys).intersection(set(sparse_keys)))
  
      dense_defaults_vec = []
      for i, key in enumerate(dense_keys):
        default_value = dense_defaults.get(key)
        if default_value is None:
          default_value = constant_op.constant([], dtype=dense_types[i])
        elif not isinstance(default_value, ops.Tensor):
          key_name = "key_" + re.sub("[^A-Za-z0-9_.\\-/]", "_", key)
          default_value = ops.convert_to_tensor(
              default_value, dtype=dense_types[i], name=key_name)
          default_value = array_ops.reshape(default_value, dense_shapes[i])
  
        dense_defaults_vec.append(default_value)
  
      dense_shapes = [tensor_shape.as_shape(shape).as_proto()
                      for shape in dense_shapes]
  
      # pylint: disable=protected-access
      outputs = gen_parsing_ops._parse_example(
          serialized=serialized,
          names=names,
          dense_defaults=dense_defaults_vec,
          sparse_keys=sparse_keys,
          sparse_types=sparse_types,
          dense_keys=dense_keys,
          dense_shapes=dense_shapes,
          name=name)
      # pylint: enable=protected-access
  
      (sparse_indices, sparse_values, sparse_shapes, dense_values) = outputs
  
      sparse_tensors = [
          sparse_tensor.SparseTensor(ix, val, shape) for (ix, val, shape)
          in zip(sparse_indices, sparse_values, sparse_shapes)]
  
      return dict(zip(sparse_keys + dense_keys, sparse_tensors + dense_values))
  
  
  def parse_single_example(serialized, features, name=None, example_names=None):
    """Parses a single `Example` proto.
  
    Similar to `parse_example`, except:
  
    For dense tensors, the returned `Tensor` is identical to the output of
    `parse_example`, except there is no batch dimension, the output shape is the
    same as the shape given in `dense_shape`.
  
    For `SparseTensor`s, the first (batch) column of the indices matrix is removed
    (the indices matrix is a column vector), the values vector is unchanged, and
    the first (`batch_size`) entry of the shape vector is removed (it is now a
    single element vector).
  
    Args:
      serialized: A scalar string Tensor, a single serialized Example.
        See `_parse_single_example_raw` documentation for more details.
      features: A `dict` mapping feature keys to `FixedLenFeature` or
        `VarLenFeature` values.
      name: A name for this operation (optional).
      example_names: (Optional) A scalar string Tensor, the associated name.
        See `_parse_single_example_raw` documentation for more details.
  
    Returns:
      A `dict` mapping feature keys to `Tensor` and `SparseTensor` values.
  
    Raises:
      ValueError: if any feature is invalid.
    """
    if not features:
      raise ValueError("Missing features.")
    (sparse_keys, sparse_types, dense_keys, dense_types, dense_defaults,
     dense_shapes) = _features_to_raw_params(
         features, [VarLenFeature, FixedLenFeature, SparseFeature])
    outputs = _parse_single_example_raw(
        serialized, example_names, sparse_keys, sparse_types, dense_keys,
        dense_types, dense_defaults, dense_shapes, name)
    _construct_sparse_tensors_for_sparse_features(features, outputs)
    return outputs
  
  
  def _parse_single_example_raw(serialized,
                                names=None,
                                sparse_keys=None,
                                sparse_types=None,
                                dense_keys=None,
                                dense_types=None,
                                dense_defaults=None,
                                dense_shapes=None,
                                name=None):
    """Parses a single `Example` proto.
  
    Args:
      serialized: A scalar string Tensor, a single serialized Example.
        See `_parse_example_raw` documentation for more details.
      names: (Optional) A scalar string Tensor, the associated name.
        See `_parse_example_raw` documentation for more details.
      sparse_keys: See `_parse_example_raw` documentation for more details.
      sparse_types: See `_parse_example_raw` documentation for more details.
      dense_keys: See `_parse_example_raw` documentation for more details.
      dense_types: See `_parse_example_raw` documentation for more details.
      dense_defaults: See `_parse_example_raw` documentation for more details.
      dense_shapes: See `_parse_example_raw` documentation for more details.
      name: A name for this operation (optional).
  
    Returns:
      A `dict` mapping feature keys to `Tensor` and `SparseTensor` values.
  
    Raises:
      ValueError: if any feature is invalid.
    """
    with ops.name_scope(name, "ParseSingleExample", [serialized, names]):
      serialized = ops.convert_to_tensor(serialized)
      serialized_shape = serialized.get_shape()
      if serialized_shape.ndims is not None:
        if serialized_shape.ndims != 0:
          raise ValueError("Input serialized must be a scalar")
      else:
        serialized = control_flow_ops.with_dependencies(
            [control_flow_ops.Assert(
                math_ops.equal(array_ops.rank(serialized), 0),
                ["Input serialized must be a scalar"],
                name="SerializedIsScalar")],
            serialized,
            name="SerializedDependencies")
      serialized = array_ops.expand_dims(serialized, 0)
      if names is not None:
        names = ops.convert_to_tensor(names)
        names_shape = names.get_shape()
        if names_shape.ndims is not None:
          if names_shape.ndims != 0:
            raise ValueError("Input names must be a scalar")
        else:
          names = control_flow_ops.with_dependencies(
              [control_flow_ops.Assert(
                  math_ops.equal(array_ops.rank(names), 0),
                  ["Input names must be a scalar"],
                  name="NamesIsScalar")],
              names,
              name="NamesDependencies")
        names = array_ops.expand_dims(names, 0)
  
      outputs = _parse_example_raw(
          serialized,
          names=names,
          sparse_keys=sparse_keys,
          sparse_types=sparse_types,
          dense_keys=dense_keys,
          dense_types=dense_types,
          dense_defaults=dense_defaults,
          dense_shapes=dense_shapes,
          name=name)
      if dense_keys is not None:
        for d in dense_keys:
          d_name = re.sub("[^A-Za-z0-9_.\\-/]", "_", d)
          outputs[d] = array_ops.squeeze(
              outputs[d], [0], name="Squeeze_%s" % d_name)
      if sparse_keys is not None:
        for s in sparse_keys:
          s_name = re.sub("[^A-Za-z0-9_.\\-/]", "_", s)
          outputs[s] = sparse_tensor.SparseTensor(
              array_ops.slice(outputs[s].indices,
                              [0, 1], [-1, -1], name="Slice_Indices_%s" % s_name),
              outputs[s].values,
              array_ops.slice(outputs[s].dense_shape,
                              [1], [-1], name="Squeeze_Shape_%s" % s_name))
      return outputs
  
  
  def parse_single_sequence_example(
      serialized, context_features=None, sequence_features=None,
      example_name=None, name=None):
    # pylint: disable=line-too-long
    """Parses a single `SequenceExample` proto.
  
    Parses a single serialized [`SequenceExample`](https://www.tensorflow.org/code/tensorflow/core/example/example.proto)
    proto given in `serialized`.
  
    This op parses a serialize sequence example into a tuple of dictionaries
    mapping keys to `Tensor` and `SparseTensor` objects respectively.
    The first dictionary contains mappings for keys appearing in
    `context_features`, and the second dictionary contains mappings for keys
    appearing in `sequence_features`.
  
    At least one of `context_features` and `sequence_features` must be provided
    and non-empty.
  
    The `context_features` keys are associated with a `SequenceExample` as a
    whole, independent of time / frame.  In contrast, the `sequence_features` keys
    provide a way to access variable-length data within the `FeatureList` section
    of the `SequenceExample` proto.  While the shapes of `context_features` values
    are fixed with respect to frame, the frame dimension (the first dimension)
    of `sequence_features` values may vary between `SequenceExample` protos,
    and even between `feature_list` keys within the same `SequenceExample`.
  
    `context_features` contains `VarLenFeature` and `FixedLenFeature` objects.
    Each `VarLenFeature` is mapped to a `SparseTensor`, and each `FixedLenFeature`
    is mapped to a `Tensor`, of the specified type, shape, and default value.
  
    `sequence_features` contains `VarLenFeature` and `FixedLenSequenceFeature`
    objects. Each `VarLenFeature` is mapped to a `SparseTensor`, and each
    `FixedLenSequenceFeature` is mapped to a `Tensor`, each of the specified type.
    The shape will be `(T,) + df.dense_shape` for `FixedLenSequenceFeature` `df`, where
    `T` is the length of the associated `FeatureList` in the `SequenceExample`.
    For instance, `FixedLenSequenceFeature([])` yields a scalar 1-D `Tensor` of
    static shape `[None]` and dynamic shape `[T]`, while
    `FixedLenSequenceFeature([k])` (for `int k >= 1`) yields a 2-D matrix `Tensor`
    of static shape `[None, k]` and dynamic shape `[T, k]`.
  
    Each `SparseTensor` corresponding to `sequence_features` represents a ragged
    vector.  Its indices are `[time, index]`, where `time` is the `FeatureList`
    entry and `index` is the value's index in the list of values associated with
    that time.
  
    `FixedLenFeature` entries with a `default_value` and `FixedLenSequenceFeature`
    entries with `allow_missing=True` are optional; otherwise, we will fail if
    that `Feature` or `FeatureList` is missing from any example in `serialized`.
  
    `example_name` may contain a descriptive name for the corresponding serialized
    proto. This may be useful for debugging purposes, but it has no effect on the
    output. If not `None`, `example_name` must be a scalar.
  
    Args:
      serialized: A scalar (0-D Tensor) of type string, a single binary
        serialized `SequenceExample` proto.
      context_features: A `dict` mapping feature keys to `FixedLenFeature` or
        `VarLenFeature` values. These features are associated with a
        `SequenceExample` as a whole.
      sequence_features: A `dict` mapping feature keys to
        `FixedLenSequenceFeature` or `VarLenFeature` values. These features are
        associated with data within the `FeatureList` section of the
        `SequenceExample` proto.
      example_name: A scalar (0-D Tensor) of strings (optional), the name of
        the serialized proto.
      name: A name for this operation (optional).
  
    Returns:
      A tuple of two `dict`s, each mapping keys to `Tensor`s and `SparseTensor`s.
      The first dict contains the context key/values.
      The second dict contains the feature_list key/values.
  
    Raises:
      ValueError: if any feature is invalid.
    """
    # pylint: enable=line-too-long
    if not (context_features or sequence_features):
      raise ValueError("Missing features.")
    (context_sparse_keys, context_sparse_types, context_dense_keys,
     context_dense_types, context_dense_defaults,
     context_dense_shapes) = _features_to_raw_params(
         context_features, [VarLenFeature, FixedLenFeature])
    (feature_list_sparse_keys, feature_list_sparse_types,
     feature_list_dense_keys, feature_list_dense_types,
     feature_list_dense_defaults,
     feature_list_dense_shapes) = _features_to_raw_params(
         sequence_features, [VarLenFeature, FixedLenSequenceFeature])
    return _parse_single_sequence_example_raw(
        serialized, context_sparse_keys, context_sparse_types,
        context_dense_keys, context_dense_types, context_dense_defaults,
        context_dense_shapes, feature_list_sparse_keys,
        feature_list_sparse_types, feature_list_dense_keys,
        feature_list_dense_types, feature_list_dense_shapes,
        feature_list_dense_defaults, example_name, name)
  
  
  def _parse_single_sequence_example_raw(serialized,
                                         context_sparse_keys=None,
                                         context_sparse_types=None,
                                         context_dense_keys=None,
                                         context_dense_types=None,
                                         context_dense_defaults=None,
                                         context_dense_shapes=None,
                                         feature_list_sparse_keys=None,
                                         feature_list_sparse_types=None,
                                         feature_list_dense_keys=None,
                                         feature_list_dense_types=None,
                                         feature_list_dense_shapes=None,
                                         feature_list_dense_defaults=None,
                                         debug_name=None,
                                         name=None):
    """Parses a single `SequenceExample` proto.
  
    Args:
      serialized: A scalar (0-D Tensor) of type string, a single binary
        serialized `SequenceExample` proto.
      context_sparse_keys: A list of string keys in the `SequenceExample`'s
        features.  The results for these keys will be returned as
        `SparseTensor` objects.
      context_sparse_types: A list of `DTypes`, the same length as `sparse_keys`.
        Only `tf.float32` (`FloatList`), `tf.int64` (`Int64List`),
        and `tf.string` (`BytesList`) are supported.
      context_dense_keys: A list of string keys in the examples' features.
        The results for these keys will be returned as `Tensor`s
      context_dense_types: A list of DTypes, same length as `context_dense_keys`.
        Only `tf.float32` (`FloatList`), `tf.int64` (`Int64List`),
        and `tf.string` (`BytesList`) are supported.
      context_dense_defaults: A dict mapping string keys to `Tensor`s.
        The keys of the dict must match the context_dense_keys of the feature.
      context_dense_shapes: A list of tuples, same length as `context_dense_keys`.
        The shape of the data for each context_dense feature referenced by
        `context_dense_keys`.  Required for any input tensors identified by
        `context_dense_keys` whose shapes are anything other than `[]` or `[1]`.
      feature_list_sparse_keys: A list of string keys in the `SequenceExample`'s
        feature_lists.  The results for these keys will be returned as
        `SparseTensor` objects.
      feature_list_sparse_types: A list of `DTypes`, same length as `sparse_keys`.
        Only `tf.float32` (`FloatList`), `tf.int64` (`Int64List`),
        and `tf.string` (`BytesList`) are supported.
      feature_list_dense_keys: A list of string keys in the `SequenceExample`'s
        features_lists. The results for these keys will be returned as `Tensor`s.
      feature_list_dense_types: A list of `DTypes`, same length as
        `feature_list_dense_keys`.  Only `tf.float32` (`FloatList`),
        `tf.int64` (`Int64List`), and `tf.string` (`BytesList`) are supported.
      feature_list_dense_shapes: A list of tuples, same length as
        `feature_list_dense_keys`.  The shape of the data for each
        `FeatureList` feature referenced by `feature_list_dense_keys`.
      feature_list_dense_defaults: A dict mapping key strings to values.
        The only currently allowed value is `None`.  Any key appearing
        in this dict with value `None` is allowed to be missing from the
        `SequenceExample`.  If missing, the key is treated as zero-length.
      debug_name: A scalar (0-D Tensor) of strings (optional), the name of
        the serialized proto.
      name: A name for this operation (optional).
  
    Returns:
      A tuple of two `dict`s, each mapping keys to `Tensor`s and `SparseTensor`s.
      The first dict contains the context key/values.
      The second dict contains the feature_list key/values.
  
    Raises:
      ValueError: If context_sparse and context_dense key sets intersect,
        if input lengths do not match up, or if a value in
        feature_list_dense_defaults is not None.
      TypeError: if feature_list_dense_defaults is not either None or a dict.
    """
    with ops.name_scope(name, "ParseSingleSequenceExample", [serialized]):
      context_dense_defaults = (
          {} if context_dense_defaults is None else context_dense_defaults)
      context_sparse_keys = (
          [] if context_sparse_keys is None else context_sparse_keys)
      context_sparse_types = (
          [] if context_sparse_types is None else context_sparse_types)
      context_dense_keys = (
          [] if context_dense_keys is None else context_dense_keys)
      context_dense_types = (
          [] if context_dense_types is None else context_dense_types)
      context_dense_shapes = (
          [[]] * len(context_dense_keys)
          if context_dense_shapes is None else context_dense_shapes)
      feature_list_sparse_keys = (
          [] if feature_list_sparse_keys is None else feature_list_sparse_keys)
      feature_list_sparse_types = (
          [] if feature_list_sparse_types is None else feature_list_sparse_types)
      feature_list_dense_keys = (
          [] if feature_list_dense_keys is None else feature_list_dense_keys)
      feature_list_dense_types = (
          [] if feature_list_dense_types is None else feature_list_dense_types)
      feature_list_dense_shapes = (
          [[]] * len(feature_list_dense_keys)
          if feature_list_dense_shapes is None else feature_list_dense_shapes)
      feature_list_dense_defaults = (
          dict() if feature_list_dense_defaults is None
          else feature_list_dense_defaults)
      debug_name = "" if debug_name is None else debug_name
  
      # Internal
      feature_list_dense_missing_assumed_empty = []
  
      num_context_dense = len(context_dense_keys)
      num_feature_list_dense = len(feature_list_dense_keys)
      num_context_sparse = len(context_sparse_keys)
      num_feature_list_sparse = len(feature_list_sparse_keys)
  
      if len(context_dense_shapes) != num_context_dense:
        raise ValueError(
            "len(context_dense_shapes) != len(context_dense_keys): %d vs. %d"
            % (len(context_dense_shapes), num_context_dense))
      if len(context_dense_types) != num_context_dense:
        raise ValueError(
            "len(context_dense_types) != len(num_context_dense): %d vs. %d"
            % (len(context_dense_types), num_context_dense))
      if len(feature_list_dense_shapes) != num_feature_list_dense:
        raise ValueError(
            "len(feature_list_dense_shapes) != len(feature_list_dense_keys): "
            "%d vs. %d" % (len(feature_list_dense_shapes),
                           num_feature_list_dense))
      if len(feature_list_dense_types) != num_feature_list_dense:
        raise ValueError(
            "len(feature_list_dense_types) != len(num_feature_list_dense):"
            "%d vs. %d" % (len(feature_list_dense_types), num_feature_list_dense))
      if len(context_sparse_types) != num_context_sparse:
        raise ValueError(
            "len(context_sparse_types) != len(context_sparse_keys): %d vs. %d"
            % (len(context_sparse_types), num_context_sparse))
      if len(feature_list_sparse_types) != num_feature_list_sparse:
        raise ValueError(
            "len(feature_list_sparse_types) != len(feature_list_sparse_keys): "
            "%d vs. %d"
            % (len(feature_list_sparse_types), num_feature_list_sparse))
      if (num_context_dense + num_context_sparse
          + num_feature_list_dense + num_feature_list_sparse) == 0:
        raise ValueError(
            "Must provide at least one context_sparse key, context_dense key, "
            ", feature_list_sparse key, or feature_list_dense key")
      if not set(context_dense_keys).isdisjoint(set(context_sparse_keys)):
        raise ValueError(
            "context_dense and context_sparse keys must not intersect; "
            "intersection: %s" %
            set(context_dense_keys).intersection(set(context_sparse_keys)))
      if not set(feature_list_dense_keys).isdisjoint(
          set(feature_list_sparse_keys)):
        raise ValueError(
            "feature_list_dense and feature_list_sparse keys must not intersect; "
            "intersection: %s" %
            set(feature_list_dense_keys).intersection(
                set(feature_list_sparse_keys)))
      if not isinstance(feature_list_dense_defaults, dict):
        raise TypeError("feature_list_dense_defaults must be a dict")
      for k, v in feature_list_dense_defaults.items():
        if v is not None:
          raise ValueError("Value feature_list_dense_defaults[%s] must be None"
                           % k)
        feature_list_dense_missing_assumed_empty.append(k)
  
      context_dense_defaults_vec = []
      for i, key in enumerate(context_dense_keys):
        default_value = context_dense_defaults.get(key)
        if default_value is None:
          default_value = constant_op.constant([], dtype=context_dense_types[i])
        elif not isinstance(default_value, ops.Tensor):
          key_name = "key_" + re.sub("[^A-Za-z0-9_.\\-/]", "_", key)
          default_value = ops.convert_to_tensor(
              default_value, dtype=context_dense_types[i], name=key_name)
          default_value = array_ops.reshape(
              default_value, context_dense_shapes[i])
  
        context_dense_defaults_vec.append(default_value)
  
      context_dense_shapes = [tensor_shape.as_shape(shape).as_proto()
                              for shape in context_dense_shapes]
      feature_list_dense_shapes = [tensor_shape.as_shape(shape).as_proto()
                                   for shape in feature_list_dense_shapes]
  
      # pylint: disable=protected-access
      outputs = gen_parsing_ops._parse_single_sequence_example(
          serialized=serialized,
          debug_name=debug_name,
          context_dense_defaults=context_dense_defaults_vec,
          context_sparse_keys=context_sparse_keys,
          context_sparse_types=context_sparse_types,
          context_dense_keys=context_dense_keys,
          context_dense_shapes=context_dense_shapes,
          feature_list_sparse_keys=feature_list_sparse_keys,
          feature_list_sparse_types=feature_list_sparse_types,
          feature_list_dense_keys=feature_list_dense_keys,
          feature_list_dense_types=feature_list_dense_types,
          feature_list_dense_shapes=feature_list_dense_shapes,
          feature_list_dense_missing_assumed_empty=(
              feature_list_dense_missing_assumed_empty),
          name=name)
      # pylint: enable=protected-access
  
      (context_sparse_indices, context_sparse_values,
       context_sparse_shapes, context_dense_values,
       feature_list_sparse_indices, feature_list_sparse_values,
       feature_list_sparse_shapes, feature_list_dense_values) = outputs
  
      context_sparse_tensors = [
          sparse_tensor.SparseTensor(ix, val, shape) for (ix, val, shape)
          in zip(context_sparse_indices,
                 context_sparse_values,
                 context_sparse_shapes)]
  
      feature_list_sparse_tensors = [
          sparse_tensor.SparseTensor(ix, val, shape) for (ix, val, shape)
          in zip(feature_list_sparse_indices,
                 feature_list_sparse_values,
                 feature_list_sparse_shapes)]
  
      context_output = dict(
          zip(context_sparse_keys + context_dense_keys,
              context_sparse_tensors + context_dense_values))
      feature_list_output = dict(
          zip(feature_list_sparse_keys + feature_list_dense_keys,
              feature_list_sparse_tensors + feature_list_dense_values))
  
      return (context_output, feature_list_output)
   # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  
  """Library for getting system information during TensorFlow tests."""
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import os
  import shlex
  import subprocess
  import tempfile
  import time
  
  import tensorflow as tf
  
  from tensorflow.core.util import test_log_pb2
  from tensorflow.tools.test import system_info_lib
  
  
  def get_git_commit_sha():
    """Get git commit SHA for this build.
  
    Attempt to get the SHA from environment variable GIT_COMMIT, which should
    be available on Jenkins build agents.
  
    Returns:
      SHA hash of the git commit used for the build, if available
    """
  
    return os.getenv("GIT_COMMIT")
  
  
  def process_test_logs(
      name, test_name, test_args, start_time, run_time, log_files):
    """Gather test information and put it in a TestResults proto.
  
    Args:
      name: Benchmark target identifier.
      test_name:  A unique bazel target, e.g. "//path/to:test"
      test_args:  A string containing all arguments to run the target with.
  
      start_time: Test starting time (epoch)
      run_time:   Wall time that the test ran for
      log_files:  Paths to the log files
  
    Returns:
      A TestResults proto
    """
  
    results = test_log_pb2.TestResults()
    results.name = name
    results.target = test_name
    results.start_time = start_time
    results.run_time = run_time
  
    # Gather source code information
    git_sha = get_git_commit_sha()
    if git_sha:
      results.commit_id.hash = git_sha
  
    results.entries.CopyFrom(process_benchmarks(log_files))
    results.run_configuration.argument.extend(test_args)
    results.machine_configuration.CopyFrom(
        system_info_lib.gather_machine_configuration())
    return results
  
  
  def process_benchmarks(log_files):
    benchmarks = test_log_pb2.BenchmarkEntries()
    for f in log_files:
      content = tf.gfile.GFile(f, "rb").read()
      if benchmarks.MergeFromString(content) != len(content):
        raise Exception("Failed parsing benchmark entry from %s" % f)
    return benchmarks
  
  
  def run_and_gather_logs(name, test_name, test_args):
    """Run the bazel test given by test_name.  Gather and return the logs.
  
    Args:
      name: Benchmark target identifier.
      test_name: A unique bazel target, e.g. "//path/to:test"
      test_args: A string containing all arguments to run the target with.
  
    Returns:
      A tuple (test_results, mangled_test_name), where
      test_results: A test_log_pb2.TestResults proto
      mangled_test_name: A string, the mangled test name.
  
    Raises:
      ValueError: If the test_name is not a valid target.
      subprocess.CalledProcessError: If the target itself fails.
      IOError: If there are problems gathering test log output from the test.
    """
    if not (test_name
            and test_name.startswith("//")
            and ".." not in test_name
            and not test_name.endswith(":")
            and not test_name.endswith(":all")
            and not test_name.endswith("...")
            and len(test_name.split(":")) == 2):
      raise ValueError("Expected test_name parameter with a unique test, e.g.: "
                       "--test_name=//path/to:test")
    test_executable = test_name.rstrip().strip("/").replace(":", "/")
  
    if tf.gfile.Exists(os.path.join("bazel-bin", test_executable)):
      # Running in standalone mode from core of the repository
      test_executable = os.path.join("bazel-bin", test_executable)
    else:
      # Hopefully running in sandboxed mode
      test_executable = os.path.join(".", test_executable)
  
    temp_directory = tempfile.mkdtemp(prefix="run_and_gather_logs")
    mangled_test_name = test_name.strip("/").replace("/", "_").replace(":", "_")
    test_file_prefix = os.path.join(temp_directory, mangled_test_name)
    test_file_prefix = "%s." % test_file_prefix
  
    try:
      if not tf.gfile.Exists(test_executable):
        raise ValueError("Executable does not exist: %s" % test_executable)
      test_args = shlex.split(test_args)
  
      # This key is defined in tf/core/util/reporter.h as
      # TestReporter::kTestReporterEnv.
      os.environ["TEST_REPORT_FILE_PREFIX"] = test_file_prefix
      start_time = time.time()
      subprocess.check_call([test_executable] + test_args)
      run_time = time.time() - start_time
      log_files = tf.gfile.Glob("{}*".format(test_file_prefix))
  
      return (process_test_logs(name, test_name, test_args,
                                start_time=int(start_time),
                                run_time=run_time, log_files=log_files),
              mangled_test_name)
  
    finally:
      try:
        tf.gfile.DeleteRecursively(temp_directory)
      except OSError:
        pass
  
apis: tensorflow.contrib.seq2seq.simple_decoder_fn_inference tensorflow.constant_initializer tensorflow.nn.rnn_cell.GRUCell tensorflow.global_variables_initializer tensorflow.contrib.seq2seq.dynamic_rnn_decoder tensorflow.variable_scope tensorflow.random_normal_initializer tensorflow.contrib.seq2seq.simple_decoder_fn_train tensorflow.constant tensorflow.contrib.layers.linear tensorflow.test.main
apis: tensorflow.device tensorflow.train.ClusterSpec tensorflow.train.replica_device_setter tensorflow.Variable tensorflow.test.main
apis: tensorflow.python.ops.gen_parsing_ops._parse_example tensorflow.python.ops.array_ops.slice tensorflow.python.ops.array_ops.squeeze tensorflow.python.ops.array_ops.rank tensorflow.python.framework.ops.NotDifferentiable tensorflow.python.ops.array_ops.expand_dims tensorflow.python.framework.ops.name_scope tensorflow.python.ops.gen_parsing_ops._parse_single_sequence_example tensorflow.python.framework.tensor_shape.as_shape tensorflow.python.ops.array_ops.reshape tensorflow.python.framework.sparse_tensor.SparseTensor tensorflow.python.framework.ops.convert_to_tensor tensorflow.python.framework.constant_op.constant tensorflow.python.ops.sparse_ops.sparse_merge
apis: tensorflow.tools.test.system_info_lib.gather_machine_configuration tensorflow.gfile.GFile tensorflow.core.util.test_log_pb2.BenchmarkEntries tensorflow.core.util.test_log_pb2.TestResults tensorflow.gfile.Exists tensorflow.gfile.DeleteRecursively