code: # 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.
  # ==============================================================================
  """Functions that wrap both gfile and gcs.
  
  This module is *not* intended to be a general-purpose IO wrapper library; it
  only implements the operations that are necessary for loading event files. The
  functions either dispatch to the gcs library or to gfile, depending on whether
  the path is a GCS 'pseudo-path' (i.e., it satisfies gcs.IsGCSPath) or not.
  """
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import os
  
  from tensorflow.python.platform import gfile
  from tensorflow.python.summary.impl import event_file_loader
  from tensorflow.python.summary.impl import gcs
  from tensorflow.python.summary.impl import gcs_file_loader
  
  
  def CreateFileLoader(path):
    """Creates a file loader for the given path.
  
    Args:
      path: A string representing either a normal path or a GCS
    Returns:
      An object with a Load() method that yields event_pb2.Event protos.
    """
    if gcs.IsGCSPath(path):
      return gcs_file_loader.GCSFileLoader(path)
    else:
      return event_file_loader.EventFileLoader(path)
  
  
  def ListDirectoryAbsolute(directory):
    """Yields all files in the given directory. The paths are absolute."""
    if gcs.IsGCSPath(directory):
      return gcs.ListDirectory(directory)
    else:
      return (os.path.join(directory, path)
              for path in gfile.ListDirectory(directory))
  
  
  def ListRecursively(top):
    """Walks a directory tree, yielding (dir_path, file_paths) tuples.
  
    For each of `top` and its subdirectories, yields a tuple containing the path
    to the directory and the path to each of the contained files.  Note that
    unlike os.Walk()/gfile.Walk(), this does not list subdirectories and the file
    paths are all absolute.
  
    If the directory does not exist, this yields nothing.
  
    Args:
      top: A path to a directory..
    Yields:
      A list of (dir_path, file_paths) tuples.
    """
    if gcs.IsGCSPath(top):
      for x in gcs.ListRecursively(top):
        yield x
    else:
      for dir_path, _, filenames in gfile.Walk(top):
        yield (dir_path, (os.path.join(dir_path, filename)
                          for filename in filenames))
  
  
  def IsDirectory(path):
    """Returns true if path exists and is a directory."""
    if gcs.IsGCSPath(path):
      return gcs.IsDirectory(path)
    else:
      return gfile.IsDirectory(path)
  
  
  def Exists(path):
    if gcs.IsGCSPath(path):
      return gcs.Exists(path)
    else:
      return gfile.Exists(path)
  
  
  def Size(path):
    """Returns the number of bytes in the given file. Doesn't work on GCS."""
    if gcs.IsGCSPath(path):
      raise NotImplementedError("io_wrapper.Size doesn't support GCS paths")
    else:
      return gfile.Open(path).size()
   # 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.
  # ==============================================================================
  """The Dirichlet Multinomial distribution class."""
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  from tensorflow.contrib.distributions.python.ops import distribution
  from tensorflow.contrib.distributions.python.ops import distribution_util
  from tensorflow.python.framework import ops
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import check_ops
  from tensorflow.python.ops import control_flow_ops
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import special_math_ops
  
  
  class DirichletMultinomial(distribution.Distribution):
    """DirichletMultinomial mixture distribution.
  
    This distribution is parameterized by a vector `alpha` of concentration
    parameters for `k` classes and `n`, the counts per each class..
  
    #### Mathematical details
  
    The Dirichlet Multinomial is a distribution over k-class count data, meaning
    for each k-tuple of non-negative integer `counts = [c_1,...,c_k]`, we have a
    probability of these draws being made from the distribution.  The distribution
    has hyperparameters `alpha = (alpha_1,...,alpha_k)`, and probability mass
    function (pmf):
  
    ```pmf(counts) = N! / (n_1!...n_k!) * Beta(alpha + c) / Beta(alpha)```
  
    where above `N = sum_j n_j`, `N!` is `N` factorial, and
    `Beta(x) = prod_j Gamma(x_j) / Gamma(sum_j x_j)` is the multivariate beta
    function.
  
    This is a mixture distribution in that `M` samples can be produced by:
      1. Choose class probabilities `p = (p_1,...,p_k) ~ Dir(alpha)`
      2. Draw integers `m = (n_1,...,n_k) ~ Multinomial(N, p)`
  
    This class provides methods to create indexed batches of Dirichlet
    Multinomial distributions.  If the provided `alpha` is rank 2 or higher, for
    every fixed set of leading dimensions, the last dimension represents one
    single Dirichlet Multinomial distribution.  When calling distribution
    functions (e.g. `dist.pmf(counts)`), `alpha` and `counts` are broadcast to the
    same shape (if possible).  In all cases, the last dimension of alpha/counts
    represents single Dirichlet Multinomial distributions.
  
    #### Examples
  
    ```python
    alpha = [1, 2, 3]
    n = 2
    dist = DirichletMultinomial(n, alpha)
    ```
  
    Creates a 3-class distribution, with the 3rd class is most likely to be drawn.
    The distribution functions can be evaluated on counts.
  
    ```python
    # counts same shape as alpha.
    counts = [0, 0, 2]
    dist.pmf(counts)  # Shape []
  
    # alpha will be broadcast to [[1, 2, 3], [1, 2, 3]] to match counts.
    counts = [[1, 1, 0], [1, 0, 1]]
    dist.pmf(counts)  # Shape [2]
  
    # alpha will be broadcast to shape [5, 7, 3] to match counts.
    counts = [[...]]  # Shape [5, 7, 3]
    dist.pmf(counts)  # Shape [5, 7]
    ```
  
    Creates a 2-batch of 3-class distributions.
  
    ```python
    alpha = [[1, 2, 3], [4, 5, 6]]  # Shape [2, 3]
    n = [3, 3]
    dist = DirichletMultinomial(n, alpha)
  
    # counts will be broadcast to [[2, 1, 0], [2, 1, 0]] to match alpha.
    counts = [2, 1, 0]
    dist.pmf(counts)  # Shape [2]
    ```
  
    """
  
    # TODO(b/27419586) Change docstring for dtype of alpha once int allowed.
    def __init__(self,
                 n,
                 alpha,
                 validate_args=False,
                 allow_nan_stats=True,
                 name="DirichletMultinomial"):
      """Initialize a batch of DirichletMultinomial distributions.
  
      Args:
        n:  Non-negative floating point tensor, whose dtype is the same as
          `alpha`. The shape is broadcastable to `[N1,..., Nm]` with `m >= 0`.
          Defines this as a batch of `N1 x ... x Nm` different Dirichlet
          multinomial distributions. Its components should be equal to integer
          values.
        alpha: Positive floating point tensor, whose dtype is the same as
          `n` with shape broadcastable to `[N1,..., Nm, k]` `m >= 0`.  Defines
          this as a batch of `N1 x ... x Nm` different `k` class Dirichlet
          multinomial distributions.
        validate_args: `Boolean`, default `False`.  Whether to assert valid
          values for parameters `alpha` and `n`, and `x` in `prob` and
          `log_prob`.  If `False`, correct behavior is not guaranteed.
        allow_nan_stats: `Boolean`, default `True`.  If `False`, raise an
          exception if a statistic (e.g. mean/mode/etc...) is undefined for any
          batch member.  If `True`, batch members with valid parameters leading to
          undefined statistics will return NaN for this statistic.
        name: The name to prefix Ops created by this distribution class.
  
      Examples:
  
      ```python
      # Define 1-batch of 2-class Dirichlet multinomial distribution,
      # also known as a beta-binomial.
      dist = DirichletMultinomial(2.0, [1.1, 2.0])
  
      # Define a 2-batch of 3-class distributions.
      dist = DirichletMultinomial([3., 4], [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
      ```
  
      """
      with ops.name_scope(name, values=[n, alpha]) as ns:
        # Broadcasting works because:
        # * The broadcasting convention is to prepend dimensions of size [1], and
        #   we use the last dimension for the distribution, wherease
        #   the batch dimensions are the leading dimensions, which forces the
        #   distribution dimension to be defined explicitly (i.e. it cannot be
        #   created automatically by prepending).  This forces enough
        #   explicitivity.
        #   * All calls involving `counts` eventually require a broadcast between
        #   `counts` and alpha.
        self._alpha = self._assert_valid_alpha(alpha, validate_args)
        self._n = self._assert_valid_n(n, validate_args)
        self._alpha_sum = math_ops.reduce_sum(
            self._alpha, reduction_indices=[-1], keep_dims=False)
        super(DirichletMultinomial, self).__init__(
            dtype=self._alpha.dtype,
            parameters={"alpha": self._alpha,
                        "alpha_sum": self._alpha_sum,
                        "n": self._n},
            is_continuous=False,
            is_reparameterized=False,
            validate_args=validate_args,
            allow_nan_stats=allow_nan_stats,
            name=ns)
  
    @property
    def n(self):
      """Parameter defining this distribution."""
      return self._n
  
    @property
    def alpha(self):
      """Parameter defining this distribution."""
      return self._alpha
  
    @property
    def alpha_sum(self):
      """Summation of alpha parameter."""
      return self._alpha_sum
  
    def _batch_shape(self):
      return array_ops.shape(self.alpha_sum)
  
    def _get_batch_shape(self):
      return self.alpha_sum.get_shape()
  
    def _event_shape(self):
      return array_ops.reverse(array_ops.shape(self.alpha), [True])[0]
  
    def _get_event_shape(self):
      # Event shape depends only on alpha, not "n".
      return self.alpha.get_shape().with_rank_at_least(1)[-1:]
  
    def _log_prob(self, counts):
      counts = self._assert_valid_counts(counts)
      ordered_prob = (special_math_ops.lbeta(self.alpha + counts) -
                      special_math_ops.lbeta(self.alpha))
      log_prob = ordered_prob + distribution_util.log_combinations(
          self.n, counts)
      return log_prob
  
    def _prob(self, counts):
      return math_ops.exp(self._log_prob(counts))
  
    def _mean(self):
      normalized_alpha = self.alpha / array_ops.expand_dims(self.alpha_sum, -1)
      return array_ops.expand_dims(self.n, -1) * normalized_alpha
  
    def _variance(self):
      alpha_sum = array_ops.expand_dims(self.alpha_sum, -1)
      normalized_alpha = self.alpha / alpha_sum
      variance = -math_ops.batch_matmul(
          array_ops.expand_dims(normalized_alpha, -1),
          array_ops.expand_dims(normalized_alpha, -2))
      variance = array_ops.matrix_set_diag(variance, normalized_alpha *
                                           (1. - normalized_alpha))
      shared_factor = (self.n * (alpha_sum + self.n) /
                       (alpha_sum + 1) * array_ops.ones_like(self.alpha))
      variance *= array_ops.expand_dims(shared_factor, -1)
      return variance
  
    def _assert_valid_counts(self, counts):
      """Check counts for proper shape, values, then return tensor version."""
      counts = ops.convert_to_tensor(counts, name="counts")
      if not self.validate_args:
        return counts
      candidate_n = math_ops.reduce_sum(counts, reduction_indices=[-1])
      return control_flow_ops.with_dependencies([
          check_ops.assert_non_negative(counts),
          check_ops.assert_equal(
              self._n, candidate_n,
              message="counts do not sum to n"),
          distribution_util.assert_integer_form(counts)], counts)
  
    def _assert_valid_alpha(self, alpha, validate_args):
      alpha = ops.convert_to_tensor(alpha, name="alpha")
      if not validate_args:
        return alpha
      return control_flow_ops.with_dependencies(
          [check_ops.assert_rank_at_least(alpha, 1),
           check_ops.assert_positive(alpha)], alpha)
  
    def _assert_valid_n(self, n, validate_args):
      n = ops.convert_to_tensor(n, name="n")
      if not validate_args:
        return n
      return control_flow_ops.with_dependencies(
          [check_ops.assert_non_negative(n),
           distribution_util.assert_integer_form(n)], n)
  
  
  _prob_note = """
  
    For each batch of counts `[n_1,...,n_k]`, `P[counts]` is the probability
    that after sampling `n` draws from this Dirichlet Multinomial
    distribution, the number of draws falling in class `j` is `n_j`.  Note that
    different sequences of draws can result in the same counts, thus the
    probability includes a combinatorial coefficient.
  
    Note that input, "counts", must be a non-negative tensor with dtype `dtype`
    and whose shape can be broadcast with `self.alpha`.  For fixed leading
    dimensions, the last dimension represents counts for the corresponding
    Dirichlet Multinomial distribution in `self.alpha`. `counts` is only legal if
    it sums up to `n` and its components are equal to integer values.
  """
  distribution_util.append_class_fun_doc(DirichletMultinomial.log_prob,
                                         doc_str=_prob_note)
  distribution_util.append_class_fun_doc(DirichletMultinomial.prob,
                                         doc_str=_prob_note)
  
  distribution_util.append_class_fun_doc(DirichletMultinomial.variance,
                                         doc_str="""
  
    The variance for each batch member is defined as the following:
  
    ```
    Var(X_j) = n * alpha_j / alpha_0 * (1 - alpha_j / alpha_0) *
      (n + alpha_0) / (1 + alpha_0)
    ```
  
    where `alpha_0 = sum_j alpha_j`.
  
    The covariance between elements in a batch is defined as:
  
    ```
    Cov(X_i, X_j) = -n * alpha_i * alpha_j / alpha_0 ** 2 *
      (n + alpha_0) / (1 + alpha_0)
    ```
  
  """)
   # 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 slim.evaluation."""
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  
  import glob
  import os
  import time
  
  import numpy as np
  import tensorflow as tf
  
  from tensorflow.python.platform import flags
  from tensorflow.python.platform import gfile
  
  slim = tf.contrib.slim
  
  FLAGS = flags.FLAGS
  
  
  def GenerateTestData(num_classes, batch_size):
    inputs = np.random.rand(batch_size, num_classes)
  
    np.random.seed(0)
    labels = np.random.randint(low=0, high=num_classes, size=batch_size)
    labels = labels.reshape((batch_size,))
    return inputs, labels
  
  
  def TestModel(inputs):
    scale = tf.Variable(1.0, trainable=False)
  
    # Scaling the outputs wont change the result...
    outputs = tf.mul(inputs, scale)
    return tf.argmax(outputs, 1), scale
  
  
  def GroundTruthAccuracy(inputs, labels, batch_size):
    predictions = np.argmax(inputs, 1)
    num_correct = np.sum(predictions == labels)
    return float(num_correct) / batch_size
  
  
  class EvaluationTest(tf.test.TestCase):
  
    def setUp(self):
      super(EvaluationTest, self).setUp()
  
      num_classes = 8
      batch_size = 16
      inputs, labels = GenerateTestData(num_classes, batch_size)
      self._expected_accuracy = GroundTruthAccuracy(inputs, labels, batch_size)
  
      self._global_step = slim.get_or_create_global_step()
      self._inputs = tf.constant(inputs, dtype=tf.float32)
      self._labels = tf.constant(labels, dtype=tf.int64)
      self._predictions, self._scale = TestModel(self._inputs)
  
    def testUpdateOpsAreEvaluated(self):
      accuracy, update_op = slim.metrics.streaming_accuracy(
          self._predictions, self._labels)
      initial_op = tf.group(tf.initialize_all_variables(),
                            tf.initialize_local_variables())
  
      with self.test_session() as sess:
        slim.evaluation.evaluation(
            sess, initial_op=initial_op, eval_op=update_op)
        self.assertAlmostEqual(accuracy.eval(), self._expected_accuracy)
  
    def testFinalOpsIsEvaluated(self):
      _, update_op = slim.metrics.streaming_accuracy(
          self._predictions, self._labels)
      initial_op = tf.group(tf.initialize_all_variables(),
                            tf.initialize_local_variables())
  
      with self.test_session() as sess:
        accuracy_value = slim.evaluation.evaluation(
            sess, initial_op=initial_op, final_op=update_op)
        self.assertAlmostEqual(accuracy_value, self._expected_accuracy)
  
    def testFinalOpsOnEvaluationLoop(self):
      value_op, update_op = slim.metrics.streaming_accuracy(
          self._predictions, self._labels)
      init_op = tf.group(tf.initialize_all_variables(),
                         tf.initialize_local_variables())
      # Create Checkpoint and log directories
      chkpt_dir = os.path.join(self.get_temp_dir(), 'tmp_logs/')
      gfile.MakeDirs(chkpt_dir)
      logdir = os.path.join(self.get_temp_dir(), 'tmp_logs2/')
      gfile.MakeDirs(logdir)
  
      # Save initialized variables to checkpoint directory
      saver = tf.train.Saver()
      with self.test_session() as sess:
        init_op.run()
        saver.save(sess, os.path.join(chkpt_dir, 'chkpt'))
  
      # Now, run the evaluation loop:
      accuracy_value = slim.evaluation.evaluation_loop(
          '', chkpt_dir, logdir, eval_op=update_op, final_op=value_op,
          max_number_of_evaluations=1)
      self.assertAlmostEqual(accuracy_value, self._expected_accuracy)
  
    def _create_names_to_metrics(self, predictions, labels):
      accuracy0, update_op0 = tf.contrib.metrics.streaming_accuracy(
          predictions, labels)
      accuracy1, update_op1 = tf.contrib.metrics.streaming_accuracy(
          predictions+1, labels)
  
      names_to_values = {'Accuracy': accuracy0, 'Another accuracy': accuracy1}
      names_to_updates = {'Accuracy': update_op0, 'Another accuracy': update_op1}
      return names_to_values, names_to_updates
  
    def _verify_summaries(self, output_dir, names_to_values):
      """Verifies that the given `names_to_values` are found in the summaries.
  
      Args:
        output_dir: An existing directory where summaries are found.
        names_to_values: A dictionary of strings to values.
      """
      # Check that the results were saved. The events file may have additional
      # entries, e.g. the event version stamp, so have to parse things a bit.
      output_filepath = glob.glob(os.path.join(output_dir, '*'))
      self.assertEqual(len(output_filepath), 1)
  
      events = tf.train.summary_iterator(output_filepath[0])
      summaries = [e.summary for e in events if e.summary.value]
      values = []
      for summary in summaries:
        for value in summary.value:
          values.append(value)
      saved_results = {v.tag: v.simple_value for v in values}
      for name in names_to_values:
        self.assertAlmostEqual(names_to_values[name], saved_results[name])
  
    def testSummariesAreFlushedToDisk(self):
      output_dir = os.path.join(self.get_temp_dir(), 'flush_test')
      if tf.gfile.Exists(output_dir):  # For running on jenkins.
        tf.gfile.DeleteRecursively(output_dir)
  
      names_to_metrics, names_to_updates = self._create_names_to_metrics(
          self._predictions, self._labels)
  
      for k in names_to_metrics:
        v = names_to_metrics[k]
        tf.scalar_summary(k, v)
  
      summary_writer = tf.train.SummaryWriter(output_dir)
  
      initial_op = tf.group(tf.initialize_all_variables(),
                            tf.initialize_local_variables())
      eval_op = tf.group(*names_to_updates.values())
  
      with self.test_session() as sess:
        slim.evaluation.evaluation(
            sess,
            initial_op=initial_op,
            eval_op=eval_op,
            summary_op=tf.merge_all_summaries(),
            summary_writer=summary_writer,
            global_step=self._global_step)
  
        names_to_values = {name: names_to_metrics[name].eval()
                           for name in names_to_metrics}
      self._verify_summaries(output_dir, names_to_values)
  
    def testSummariesAreFlushedToDiskWithoutGlobalStep(self):
      output_dir = os.path.join(self.get_temp_dir(), 'flush_test_no_global_step')
      if tf.gfile.Exists(output_dir):  # For running on jenkins.
        tf.gfile.DeleteRecursively(output_dir)
  
      names_to_metrics, names_to_updates = self._create_names_to_metrics(
          self._predictions, self._labels)
  
      for k in names_to_metrics:
        v = names_to_metrics[k]
        tf.scalar_summary(k, v)
  
      summary_writer = tf.train.SummaryWriter(output_dir)
  
      initial_op = tf.group(tf.initialize_all_variables(),
                            tf.initialize_local_variables())
      eval_op = tf.group(*names_to_updates.values())
  
      with self.test_session() as sess:
        slim.evaluation.evaluation(
            sess,
            initial_op=initial_op,
            eval_op=eval_op,
            summary_op=tf.merge_all_summaries(),
            summary_writer=summary_writer)
  
        names_to_values = {name: names_to_metrics[name].eval()
                           for name in names_to_metrics}
      self._verify_summaries(output_dir, names_to_values)
  
    def testWithFeedDict(self):
      accuracy, update_op = slim.metrics.streaming_accuracy(
          self._predictions, self._labels)
      initial_op = tf.group(tf.initialize_all_variables(),
                            tf.initialize_local_variables())
  
      with self.test_session() as sess:
        slim.evaluation.evaluation(
            sess,
            initial_op=initial_op,
            eval_op=update_op,
            eval_op_feed_dict={self._scale: np.ones([], dtype=np.float32)})
        self.assertAlmostEqual(accuracy.eval(), self._expected_accuracy)
  
    def testWithQueueRunning(self):
      strings = ['the', 'cat', 'in', 'the', 'hat']
      _ = tf.train.string_input_producer(strings, capacity=5)
  
      accuracy, update_op = slim.metrics.streaming_accuracy(
          self._predictions, self._labels)
  
      initial_op = tf.group(tf.initialize_all_variables(),
                            tf.initialize_local_variables())
  
      with self.test_session() as sess:
        slim.evaluation.evaluation(
            sess, initial_op=initial_op, eval_op=update_op)
        self.assertAlmostEqual(accuracy.eval(), self._expected_accuracy)
  
    def testLatestCheckpointReturnsNoneAfterTimeout(self):
      start = time.time()
      ret = slim.evaluation.wait_for_new_checkpoint(
          '/non-existent-dir', 'foo', timeout=1.0, seconds_to_sleep=0.5)
      end = time.time()
      self.assertIsNone(ret)
      # We've waited one time.
      self.assertGreater(end, start + 0.5)
      # The timeout kicked in.
      self.assertLess(end, start + 1.1)
  
    def testMonitorCheckpointsLoopTimeout(self):
      ret = list(slim.evaluation.checkpoints_iterator(
          '/non-existent-dir', timeout=0))
      self.assertEqual(ret, [])
  
  
  class SingleEvaluationTest(tf.test.TestCase):
  
    def setUp(self):
      super(SingleEvaluationTest, self).setUp()
  
      num_classes = 8
      batch_size = 16
      inputs, labels = GenerateTestData(num_classes, batch_size)
      self._expected_accuracy = GroundTruthAccuracy(inputs, labels, batch_size)
  
      self._global_step = slim.get_or_create_global_step()
      self._inputs = tf.constant(inputs, dtype=tf.float32)
      self._labels = tf.constant(labels, dtype=tf.int64)
      self._predictions, self._scale = TestModel(self._inputs)
  
    def testErrorRaisedIfCheckpointDoesntExist(self):
      checkpoint_path = os.path.join(self.get_temp_dir(),
                                     'this_file_doesnt_exist')
      log_dir = os.path.join(self.get_temp_dir(), 'error_raised')
      with self.assertRaises(ValueError):
        slim.evaluation.evaluate_once('', checkpoint_path, log_dir)
  
    def testRestoredModelPerformance(self):
      checkpoint_path = os.path.join(self.get_temp_dir(), 'model.ckpt')
      log_dir = os.path.join(self.get_temp_dir(), 'log_dir1/')
  
      # First, save out the current model to a checkpoint:
      init_op = tf.group(tf.initialize_all_variables(),
                         tf.initialize_local_variables())
      saver = tf.train.Saver()
      with self.test_session() as sess:
        sess.run(init_op)
        saver.save(sess, checkpoint_path)
  
      # Next, determine the metric to evaluate:
      value_op, update_op = slim.metrics.streaming_accuracy(
          self._predictions, self._labels)
  
      # Run the evaluation and verify the results:
      accuracy_value = slim.evaluation.evaluate_once(
          '',
          checkpoint_path,
          log_dir,
          eval_op=update_op,
          final_op=value_op)
      self.assertAlmostEqual(accuracy_value, self._expected_accuracy)
  
  
  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 layers.feature_column_ops."""
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import numpy as np
  import tensorflow as tf
  
  from tensorflow.contrib.layers.python.layers import feature_column_ops
  from tensorflow.python.ops import init_ops
  
  
  class TransformerTest(tf.test.TestCase):
  
    def testRealValuedColumnIsIdentityTransformation(self):
      real_valued = tf.contrib.layers.real_valued_column("price")
      features = {"price": tf.constant([[20.], [110], [-3]])}
      output = feature_column_ops._Transformer(features).transform(real_valued)
      with self.test_session():
        self.assertAllEqual(output.eval(), [[20.], [110], [-3]])
  
    def testBucketizedColumn(self):
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price"),
          boundaries=[0., 10., 100.])
      # buckets 2, 3, 0
      features = {"price": tf.constant([[20.], [110], [-3]])}
      output = feature_column_ops._Transformer(features).transform(bucket)
      with self.test_session():
        self.assertAllEqual(output.eval(), [[2], [3], [0]])
  
    def testBucketizedColumnWithMultiDimensions(self):
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price", 2),
          boundaries=[0., 10., 100.])
      # buckets 2, 3, 0
      features = {"price": tf.constant([[20., 110], [110., 20], [-3, -3]])}
      output = feature_column_ops._Transformer(features).transform(bucket)
      with self.test_session():
        self.assertAllEqual(output.eval(), [[2, 3], [3, 2], [0, 0]])
  
    def testCachedTransformation(self):
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price"),
          boundaries=[0., 10., 100.])
      # buckets 2, 3, 0
      features = {"price": tf.constant([[20.], [110], [-3]])}
      transformer = feature_column_ops._Transformer(features)
      with self.test_session() as sess:
        transformer.transform(bucket)
        num_of_ops = len(sess.graph.get_operations())
        # Verify that the second call to transform the same feature
        # doesn't increase the number of ops.
        transformer.transform(bucket)
        self.assertEqual(num_of_ops, len(sess.graph.get_operations()))
  
    def testSparseColumnWithHashBucket(self):
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"wire": wire_tensor}
      output = feature_column_ops._Transformer(features).transform(hashed_sparse)
      with self.test_session():
        self.assertEqual(output.values.dtype, tf.int64)
        self.assertTrue(all(x < 10 and x >= 0 for x in output.values.eval()))
        self.assertAllEqual(output.indices.eval(), wire_tensor.indices.eval())
        self.assertAllEqual(output.shape.eval(), wire_tensor.shape.eval())
  
    def testSparseIntColumnWithHashBucket(self):
      """Tests a sparse column with int values."""
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket(
          "wire", 10, dtype=tf.int64)
      wire_tensor = tf.SparseTensor(values=[101, 201, 301],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"wire": wire_tensor}
      output = feature_column_ops._Transformer(features).transform(hashed_sparse)
      with self.test_session():
        self.assertEqual(output.values.dtype, tf.int64)
        self.assertTrue(all(x < 10 and x >= 0 for x in output.values.eval()))
        self.assertAllEqual(output.indices.eval(), wire_tensor.indices.eval())
        self.assertAllEqual(output.shape.eval(), wire_tensor.shape.eval())
  
    def testEmbeddingColumn(self):
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"wire": wire_tensor}
      output = feature_column_ops._Transformer(features).transform(
          tf.contrib.layers.embedding_column(hashed_sparse, 10))
      expected = feature_column_ops._Transformer(features).transform(
          hashed_sparse)
      with self.test_session():
        self.assertAllEqual(output.values.eval(), expected.values.eval())
        self.assertAllEqual(output.indices.eval(), expected.indices.eval())
        self.assertAllEqual(output.shape.eval(), expected.shape.eval())
  
    def testSparseColumnWithKeys(self):
      keys_sparse = tf.contrib.layers.sparse_column_with_keys(
          "wire", ["marlo", "omar", "stringer"])
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"wire": wire_tensor}
      output = feature_column_ops._Transformer(features).transform(keys_sparse)
      with self.test_session():
        tf.initialize_all_tables().run()
        self.assertEqual(output.values.dtype, tf.int64)
        self.assertAllEqual(output.values.eval(), [1, 2, 0])
        self.assertAllEqual(output.indices.eval(), wire_tensor.indices.eval())
        self.assertAllEqual(output.shape.eval(), wire_tensor.shape.eval())
  
    def testSparseColumnWithHashBucket_IsIntegerized(self):
      hashed_sparse = tf.contrib.layers.sparse_column_with_integerized_feature(
          "wire", 10)
      wire_tensor = tf.SparseTensor(values=[100, 1, 25],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"wire": wire_tensor}
      output = feature_column_ops._Transformer(features).transform(hashed_sparse)
      with self.test_session():
        self.assertEqual(output.values.dtype, tf.int32)
        self.assertTrue(all(x < 10 and x >= 0 for x in output.values.eval()))
        self.assertAllEqual(output.indices.eval(), wire_tensor.indices.eval())
        self.assertAllEqual(output.shape.eval(), wire_tensor.shape.eval())
  
    def testWeightedSparseColumn(self):
      ids = tf.contrib.layers.sparse_column_with_keys(
          "ids", ["marlo", "omar", "stringer"])
      ids_tensor = tf.SparseTensor(values=["stringer", "stringer", "marlo"],
                                   indices=[[0, 0], [1, 0], [1, 1]],
                                   shape=[2, 2])
      weighted_ids = tf.contrib.layers.weighted_sparse_column(ids, "weights")
      weights_tensor = tf.SparseTensor(values=[10.0, 20.0, 30.0],
                                       indices=[[0, 0], [1, 0], [1, 1]],
                                       shape=[2, 2])
      features = {"ids": ids_tensor,
                  "weights": weights_tensor}
      output = feature_column_ops._Transformer(features).transform(weighted_ids)
      with self.test_session():
        tf.initialize_all_tables().run()
        self.assertAllEqual(output[0].shape.eval(), ids_tensor.shape.eval())
        self.assertAllEqual(output[0].indices.eval(), ids_tensor.indices.eval())
        self.assertAllEqual(output[0].values.eval(), [2, 2, 0])
        self.assertAllEqual(output[1].shape.eval(), weights_tensor.shape.eval())
        self.assertAllEqual(output[1].indices.eval(),
                            weights_tensor.indices.eval())
        self.assertEqual(output[1].values.dtype, tf.float32)
        self.assertAllEqual(output[1].values.eval(), weights_tensor.values.eval())
  
    def testCrossColumn(self):
      language = tf.contrib.layers.sparse_column_with_hash_bucket(
          "language", hash_bucket_size=3)
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      country_language = tf.contrib.layers.crossed_column(
          [language, country], hash_bucket_size=15)
      features = {
          "language": tf.SparseTensor(values=["english", "spanish"],
                                      indices=[[0, 0], [1, 0]],
                                      shape=[2, 1]),
          "country": tf.SparseTensor(values=["US", "SV"],
                                     indices=[[0, 0], [1, 0]],
                                     shape=[2, 1])
      }
      output = feature_column_ops._Transformer(features).transform(
          country_language)
      with self.test_session():
        self.assertEqual(output.values.dtype, tf.int64)
        self.assertTrue(all(x < 15 and x >= 0 for x in output.values.eval()))
  
    def testCrossWithBucketizedColumn(self):
      price_bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price"),
          boundaries=[0., 10., 100.])
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      country_price = tf.contrib.layers.crossed_column(
          [country, price_bucket], hash_bucket_size=15)
      features = {
          "price": tf.constant([[20.]]),
          "country": tf.SparseTensor(values=["US", "SV"],
                                     indices=[[0, 0], [0, 1]],
                                     shape=[1, 2])
      }
      output = feature_column_ops._Transformer(features).transform(country_price)
      with self.test_session():
        self.assertEqual(output.values.dtype, tf.int64)
        self.assertTrue(all(x < 15 and x >= 0 for x in output.values.eval()))
  
    def testCrossWithMultiDimensionBucketizedColumn(self):
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      price_bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price", 2),
          boundaries=[0., 10., 100.])
      country_price = tf.contrib.layers.crossed_column(
          [country, price_bucket], hash_bucket_size=1000)
  
      with tf.Graph().as_default():
        features = {"price": tf.constant([[20., 210.], [110., 50.], [-3., -30.]]),
                    "country": tf.SparseTensor(values=["US", "SV", "US"],
                                               indices=[[0, 0], [1, 0], [2, 0]],
                                               shape=[3, 2])}
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [country_price],
                                                                num_outputs=1))
  
        weights = column_to_variable[country_price][0]
        grad = tf.squeeze(tf.gradients(output, weights)[0].values)
        with self.test_session():
          tf.initialize_all_variables().run()
          self.assertEqual(len(grad.eval()), 6)
  
    def testCrossWithCrossedColumn(self):
      price_bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price"),
          boundaries=[0., 10., 100.])
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      country_price = tf.contrib.layers.crossed_column(
          [country, price_bucket], hash_bucket_size=15)
      wire = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      wire_country_price = tf.contrib.layers.crossed_column(
          [wire, country_price], hash_bucket_size=15)
      features = {
          "price": tf.constant([[20.]]),
          "country": tf.SparseTensor(values=["US", "SV"],
                                     indices=[[0, 0], [0, 1]],
                                     shape=[1, 2]),
          "wire": tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                  indices=[[0, 0], [0, 1], [0, 2]],
                                  shape=[1, 3])
      }
      output = feature_column_ops._Transformer(features).transform(
          wire_country_price)
      with self.test_session():
        self.assertEqual(output.values.dtype, tf.int64)
        self.assertTrue(all(x < 15 and x >= 0 for x in output.values.eval()))
  
    def testIfFeatureTableContainsTransformationReturnIt(self):
      any_column = tf.contrib.layers.sparse_column_with_hash_bucket("sparse", 10)
      features = {any_column: "any-thing-even-not-a-tensor"}
      output = feature_column_ops._Transformer(features).transform(any_column)
      self.assertEqual(output, "any-thing-even-not-a-tensor")
  
  
  class CreateInputLayersForDNNsTest(tf.test.TestCase):
  
    def testAllDNNColumns(self):
      sparse_column = tf.contrib.layers.sparse_column_with_keys(
          "ids", ["a", "b", "c", "unseen"])
  
      real_valued_column = tf.contrib.layers.real_valued_column("income", 2)
      one_hot_column = tf.contrib.layers.one_hot_column(sparse_column)
      embedding_column = tf.contrib.layers.embedding_column(sparse_column, 10)
      features = {
          "ids": tf.SparseTensor(
              values=["c", "b", "a"],
              indices=[[0, 0], [1, 0], [2, 0]],
              shape=[3, 1]),
          "income": tf.constant([[20.3, 10], [110.3, 0.4], [-3.0, 30.4]])
      }
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [one_hot_column,
                                                             embedding_column,
                                                             real_valued_column])
      with self.test_session():
        tf.initialize_all_variables().run()
        tf.initialize_all_tables().run()
        self.assertAllEqual(output.eval().shape, [3, 2 + 4 + 10])
  
    def testRealValuedColumn(self):
      real_valued = tf.contrib.layers.real_valued_column("price")
      features = {"price": tf.constant([[20.], [110], [-3]])}
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [real_valued])
      with self.test_session():
        self.assertAllClose(output.eval(), features["price"].eval())
  
    def testRealValuedColumnWithMultiDimensions(self):
      real_valued = tf.contrib.layers.real_valued_column("price", 2)
      features = {"price": tf.constant([[20., 10.],
                                        [110, 0.],
                                        [-3, 30]])}
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [real_valued])
      with self.test_session():
        self.assertAllClose(output.eval(), features["price"].eval())
  
    def testRealValuedColumnWithNormalizer(self):
      real_valued = tf.contrib.layers.real_valued_column(
          "price", normalizer=lambda x: x - 2)
      features = {"price": tf.constant([[20.], [110], [-3]])}
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [real_valued])
      with self.test_session():
        self.assertAllClose(output.eval(), features["price"].eval() - 2)
  
    def testRealValuedColumnWithMultiDimensionsAndNormalizer(self):
      real_valued = tf.contrib.layers.real_valued_column(
          "price", 2, normalizer=lambda x: x - 2)
      features = {"price": tf.constant([[20., 10.], [110, 0.], [-3, 30]])}
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [real_valued])
      with self.test_session():
        self.assertAllClose(output.eval(), features["price"].eval() - 2)
  
    def testBucketizedColumnSucceedsForDNN(self):
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price"),
          boundaries=[0., 10., 100.])
      # buckets 2, 3, 0
      features = {"price": tf.constant([[20.], [110], [-3]])}
      output = tf.contrib.layers.input_from_feature_columns(features, [bucket])
      expected = [[0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0]]
      with self.test_session():
        self.assertAllClose(output.eval(), expected)
  
    def testBucketizedColumnWithNormalizerSucceedsForDNN(self):
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column(
              "price", normalizer=lambda x: x - 15),
          boundaries=[0., 10., 100.])
      # buckets 2, 3, 0
      features = {"price": tf.constant([[20.], [110], [-3]])}
      output = tf.contrib.layers.input_from_feature_columns(features, [bucket])
      expected = [[0, 1, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]]
      with self.test_session():
        self.assertAllClose(output.eval(), expected)
  
    def testBucketizedColumnWithMultiDimensionsSucceedsForDNN(self):
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price", 2),
          boundaries=[0., 10., 100.])
      # buckets [2, 3], [3, 2], [0, 0]. dimension = 2
      features = {"price": tf.constant([[20., 200],
                                        [110, 50],
                                        [-3, -3]])}
      output = tf.contrib.layers.input_from_feature_columns(features, [bucket])
      expected = [[0, 0, 1, 0, 0, 0, 0, 1],
                  [0, 0, 0, 1, 0, 0, 1, 0],
                  [1, 0, 0, 0, 1, 0, 0, 0]]
      with self.test_session():
        self.assertAllClose(output.eval(), expected)
  
    def testOneHotColumnFromWeightedSparseColumnFails(self):
      ids_column = tf.contrib.layers.sparse_column_with_keys(
          "ids", ["a", "b", "c", "unseen"])
      ids_tensor = tf.SparseTensor(
          values=["c", "b", "a", "c"],
          indices=[[0, 0], [1, 0], [2, 0], [2, 1]],
          shape=[3, 2])
      weighted_ids_column = tf.contrib.layers.weighted_sparse_column(ids_column,
                                                                     "weights")
      weights_tensor = tf.SparseTensor(
          values=[10.0, 20.0, 30.0, 40.0],
          indices=[[0, 0], [1, 0], [2, 0], [2, 1]],
          shape=[3, 2])
      features = {"ids": ids_tensor, "weights": weights_tensor}
      one_hot_column = tf.contrib.layers.one_hot_column(weighted_ids_column)
      with self.test_session():
        tf.initialize_all_variables().run()
        tf.initialize_all_tables().run()
        with self.assertRaisesRegexp(
            ValueError,
            "one_hot_column does not yet support weighted_sparse_column"):
          _ = tf.contrib.layers.input_from_feature_columns(features,
                                                           [one_hot_column])
  
    def testOneHotColumnFromSparseColumnWithKeysSucceedsForDNN(self):
      ids_column = tf.contrib.layers.sparse_column_with_keys(
          "ids", ["a", "b", "c", "unseen"])
      ids_tensor = tf.SparseTensor(
          values=["c", "b", "a"], indices=[[0, 0], [1, 0], [2, 0]], shape=[3, 1])
      one_hot_sparse = tf.contrib.layers.one_hot_column(ids_column)
      features = {"ids": ids_tensor}
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [one_hot_sparse])
  
      with self.test_session():
        tf.initialize_all_variables().run()
        tf.initialize_all_tables().run()
        self.assertAllEqual([[0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]],
                            output.eval())
  
    def testOneHotColumnFromMultivalentSparseColumnWithKeysSucceedsForDNN(self):
      ids_column = tf.contrib.layers.sparse_column_with_keys(
          "ids", ["a", "b", "c", "unseen"])
      ids_tensor = tf.SparseTensor(
          values=["c", "b", "a", "c"],
          indices=[[0, 0], [1, 0], [2, 0], [2, 1]],
          shape=[3, 2])
      one_hot_sparse = tf.contrib.layers.one_hot_column(ids_column)
      features = {"ids": ids_tensor}
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [one_hot_sparse])
  
      with self.test_session():
        tf.initialize_all_variables().run()
        tf.initialize_all_tables().run()
        self.assertAllEqual([[0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 1, 0]],
                            output.eval())
  
    def testOneHotColumnFromSparseColumnWithIntegerizedFeaturePassesForDNN(self):
      ids_column = tf.contrib.layers.sparse_column_with_integerized_feature(
          "ids", bucket_size=4)
      one_hot_sparse = tf.contrib.layers.one_hot_column(ids_column)
      features = {"ids": tf.SparseTensor(
          values=[2, 1, 0, 2],
          indices=[[0, 0], [1, 0], [2, 0], [2, 1]],
          shape=[3, 2])}
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [one_hot_sparse])
      with self.test_session():
        tf.initialize_all_variables().run()
        self.assertAllEqual([[0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 1, 0]],
                            output.eval())
  
    def testOneHotColumnFromSparseColumnWithHashBucketSucceedsForDNN(self):
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("feat", 10)
      wire_tensor = tf.SparseTensor(
          values=["a", "b", "c1", "c2"],
          indices=[[0, 0], [1, 0], [2, 0], [2, 1]],
          shape=[3, 2])
      features = {"feat": wire_tensor}
      one_hot_sparse = tf.contrib.layers.one_hot_column(hashed_sparse)
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [one_hot_sparse])
      with self.test_session():
        tf.initialize_all_variables().run()
        tf.initialize_all_tables().run()
        self.assertAllEqual([3, 10], output.eval().shape)
  
    def testEmbeddingColumnSucceedsForDNN(self):
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      wire_tensor = tf.SparseTensor(
          values=["omar", "stringer", "marlo", "xx", "yy"],
          indices=[[0, 0], [1, 0], [1, 1], [2, 0], [3, 0]],
          shape=[4, 2])
      features = {"wire": wire_tensor}
      embeded_sparse = tf.contrib.layers.embedding_column(hashed_sparse, 10)
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [embeded_sparse])
      with self.test_session():
        tf.initialize_all_variables().run()
        self.assertAllEqual(output.eval().shape, [4, 10])
  
    def testHashedEmbeddingColumnSucceedsForDNN(self):
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo", "omar"],
                                    indices=[[0, 0], [1, 0], [1, 1], [2, 0]],
                                    shape=[3, 2])
  
      features = {"wire": wire_tensor}
      # Big enough hash space so that hopefully there is no collision
      embedded_sparse = tf.contrib.layers.hashed_embedding_column("wire", 1000, 3)
      output = tf.contrib.layers.input_from_feature_columns(
          features, [embedded_sparse], weight_collections=["my_collection"])
      weights = tf.get_collection("my_collection")
      grad = tf.gradients(output, weights)
      with self.test_session():
        tf.initialize_all_variables().run()
        gradient_values = []
        # Collect the gradient from the different partitions (one in this test)
        for p in range(len(grad)):
          gradient_values.extend(grad[p].values.eval())
        gradient_values.sort()
        self.assertAllEqual(gradient_values, [0.5]*6 + [2]*3)
  
    def testEmbeddingColumnWithInitializerSucceedsForDNN(self):
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"wire": wire_tensor}
      init_value = 133.7
      embeded_sparse = tf.contrib.layers.embedding_column(
          hashed_sparse,
          10, initializer=tf.constant_initializer(init_value))
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [embeded_sparse])
  
      with self.test_session():
        tf.initialize_all_variables().run()
        output_eval = output.eval()
        self.assertAllEqual(output_eval.shape, [2, 10])
        self.assertAllClose(output_eval, np.tile(init_value, [2, 10]))
  
    def testEmbeddingColumnWithMultipleInitializersFails(self):
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"wire": wire_tensor}
      embedded_sparse = tf.contrib.layers.embedding_column(
          hashed_sparse,
          10,
          initializer=tf.truncated_normal_initializer(mean=42,
                                                      stddev=1337))
      embedded_sparse_alternate = tf.contrib.layers.embedding_column(
          hashed_sparse,
          10,
          initializer=tf.truncated_normal_initializer(mean=1337,
                                                      stddev=42))
  
      # Makes sure that trying to use different initializers with the same
      # embedding column explicitly fails.
      with self.test_session():
        with self.assertRaisesRegexp(
            ValueError,
            "Duplicate feature column key found for column: wire_embedding"):
          tf.contrib.layers.input_from_feature_columns(
              features, [embedded_sparse, embedded_sparse_alternate])
  
    def testEmbeddingColumnWithWeightedSparseColumnSucceedsForDNN(self):
      ids = tf.contrib.layers.sparse_column_with_keys(
          "ids", ["marlo", "omar", "stringer"])
      ids_tensor = tf.SparseTensor(values=["stringer", "stringer", "marlo"],
                                   indices=[[0, 0], [1, 0], [1, 1]],
                                   shape=[2, 2])
      weighted_ids = tf.contrib.layers.weighted_sparse_column(ids, "weights")
      weights_tensor = tf.SparseTensor(values=[10.0, 20.0, 30.0],
                                       indices=[[0, 0], [1, 0], [1, 1]],
                                       shape=[2, 2])
      features = {"ids": ids_tensor,
                  "weights": weights_tensor}
      embeded_sparse = tf.contrib.layers.embedding_column(weighted_ids, 10)
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [embeded_sparse])
      with self.test_session():
        tf.initialize_all_variables().run()
        tf.initialize_all_tables().run()
        self.assertAllEqual(output.eval().shape, [2, 10])
  
    def testEmbeddingColumnWithCrossedColumnSucceedsForDNN(self):
      a = tf.contrib.layers.sparse_column_with_hash_bucket("aaa",
                                                           hash_bucket_size=100)
      b = tf.contrib.layers.sparse_column_with_hash_bucket("bbb",
                                                           hash_bucket_size=100)
      crossed = tf.contrib.layers.crossed_column(
          set([a, b]), hash_bucket_size=10000)
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"aaa": wire_tensor, "bbb": wire_tensor}
      embeded_sparse = tf.contrib.layers.embedding_column(crossed, 10)
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [embeded_sparse])
      with self.test_session():
        tf.initialize_all_variables().run()
        self.assertAllEqual(output.eval().shape, [2, 10])
  
    def testSparseColumnFailsForDNN(self):
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"wire": wire_tensor}
      with self.test_session():
        with self.assertRaisesRegexp(
            ValueError, "Error creating input layer for column: wire"):
          tf.initialize_all_variables().run()
          tf.contrib.layers.input_from_feature_columns(features, [hashed_sparse])
  
    def testWeightedSparseColumnFailsForDNN(self):
      ids = tf.contrib.layers.sparse_column_with_keys(
          "ids", ["marlo", "omar", "stringer"])
      ids_tensor = tf.SparseTensor(values=["stringer", "stringer", "marlo"],
                                   indices=[[0, 0], [1, 0], [1, 1]],
                                   shape=[2, 2])
      weighted_ids = tf.contrib.layers.weighted_sparse_column(ids, "weights")
      weights_tensor = tf.SparseTensor(values=[10.0, 20.0, 30.0],
                                       indices=[[0, 0], [1, 0], [1, 1]],
                                       shape=[2, 2])
      features = {"ids": ids_tensor,
                  "weights": weights_tensor}
      with self.test_session():
        with self.assertRaisesRegexp(
            ValueError,
            "Error creating input layer for column: ids_weighted_by_weights"):
          tf.initialize_all_tables().run()
          tf.contrib.layers.input_from_feature_columns(features, [weighted_ids])
  
    def testCrossedColumnFailsForDNN(self):
      a = tf.contrib.layers.sparse_column_with_hash_bucket("aaa",
                                                           hash_bucket_size=100)
      b = tf.contrib.layers.sparse_column_with_hash_bucket("bbb",
                                                           hash_bucket_size=100)
      crossed = tf.contrib.layers.crossed_column(
          set([a, b]), hash_bucket_size=10000)
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"aaa": wire_tensor, "bbb": wire_tensor}
      with self.test_session():
        with self.assertRaisesRegexp(
            ValueError, "Error creating input layer for column: aaa_X_bbb"):
          tf.initialize_all_variables().run()
          tf.contrib.layers.input_from_feature_columns(features, [crossed])
  
    def testDeepColumnsSucceedForDNN(self):
      real_valued = tf.contrib.layers.real_valued_column("income", 3)
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price", 2),
          boundaries=[0., 10., 100.])
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      features = {
          "income": tf.constant([[20., 10, -5], [110, 0, -7], [-3, 30, 50]]),
          "price": tf.constant([[20., 200], [110, 2], [-20, -30]]),
          "wire": tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                  indices=[[0, 0], [1, 0], [2, 0]],
                                  shape=[3, 1])
      }
      embeded_sparse = tf.contrib.layers.embedding_column(
          hashed_sparse,
          10, initializer=tf.constant_initializer(133.7))
      output = tf.contrib.layers.input_from_feature_columns(
          features, [real_valued, bucket, embeded_sparse])
      with self.test_session():
        tf.initialize_all_variables().run()
        # size of output = 3 (real_valued) + 2 * 4 (bucket) + 10 (embedding) = 21
        self.assertAllEqual(output.eval().shape, [3, 21])
  
    def testEmbeddingColumnForDNN(self):
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"wire": wire_tensor}
      embeded_sparse = tf.contrib.layers.embedding_column(
          hashed_sparse, 1, combiner="sum", initializer=init_ops.ones_initializer)
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [embeded_sparse])
      with self.test_session():
        tf.initialize_all_variables().run()
        # score: (number of values)
        self.assertAllEqual(output.eval(), [[1.], [2.]])
  
    def testEmbeddingColumnWithWeightedSparseColumnForDNN(self):
      ids = tf.contrib.layers.sparse_column_with_keys(
          "ids", ["marlo", "omar", "stringer"])
      ids_tensor = tf.SparseTensor(values=["stringer", "stringer", "marlo"],
                                   indices=[[0, 0], [1, 0], [1, 1]],
                                   shape=[2, 2])
      weighted_ids = tf.contrib.layers.weighted_sparse_column(ids, "weights")
      weights_tensor = tf.SparseTensor(values=[10.0, 20.0, 30.0],
                                       indices=[[0, 0], [1, 0], [1, 1]],
                                       shape=[2, 2])
      features = {"ids": ids_tensor,
                  "weights": weights_tensor}
      embeded_sparse = tf.contrib.layers.embedding_column(
          weighted_ids, 1, combiner="sum", initializer=init_ops.ones_initializer)
      output = tf.contrib.layers.input_from_feature_columns(features,
                                                            [embeded_sparse])
      with self.test_session():
        tf.initialize_all_variables().run()
        tf.initialize_all_tables().run()
        # score: (sum of weights)
        self.assertAllEqual(output.eval(), [[10.], [50.]])
  
    def testInputLayerWithCollectionsForDNN(self):
      real_valued = tf.contrib.layers.real_valued_column("price")
      bucket = tf.contrib.layers.bucketized_column(real_valued,
                                                   boundaries=[0., 10., 100.])
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      features = {
          "price": tf.constant([[20.], [110], [-3]]),
          "wire": tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                  indices=[[0, 0], [1, 0], [2, 0]],
                                  shape=[3, 1])
      }
      embeded_sparse = tf.contrib.layers.embedding_column(hashed_sparse, 10)
      tf.contrib.layers.input_from_feature_columns(
          features, [real_valued, bucket, embeded_sparse],
          weight_collections=["my_collection"])
      weights = tf.get_collection("my_collection")
      # one variable for embeded sparse
      self.assertEqual(1, len(weights))
  
    def testInputLayerWithTrainableArgForDNN(self):
      real_valued = tf.contrib.layers.real_valued_column("price")
      bucket = tf.contrib.layers.bucketized_column(real_valued,
                                                   boundaries=[0., 10., 100.])
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      features = {
          "price": tf.constant([[20.], [110], [-3]]),
          "wire": tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                  indices=[[0, 0], [1, 0], [2, 0]],
                                  shape=[3, 1])
      }
      embeded_sparse = tf.contrib.layers.embedding_column(hashed_sparse, 10)
      tf.contrib.layers.input_from_feature_columns(
          features, [real_valued, bucket, embeded_sparse],
          weight_collections=["my_collection"],
          trainable=False)
      # There should not be any trainable variables
      self.assertEqual(0, len(tf.trainable_variables()))
  
      tf.contrib.layers.input_from_feature_columns(
          features, [real_valued, bucket, embeded_sparse],
          weight_collections=["my_collection"],
          trainable=True)
      # There should  one trainable variable for embeded sparse
      self.assertEqual(1, len(tf.trainable_variables()))
  
  
  class WeightedSumTest(tf.test.TestCase):
  
    def testSparseColumn(self):
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"wire": wire_tensor}
      logits, _, _ = tf.contrib.layers.weighted_sum_from_feature_columns(
          features, [hashed_sparse], num_outputs=5)
      with self.test_session():
        tf.initialize_all_variables().run()
        self.assertAllEqual(logits.eval().shape, [2, 5])
  
    def testSparseIntColumn(self):
      """Tests a sparse column with int values."""
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket(
          "wire", 10, dtype=tf.int64)
      wire_tensor = tf.SparseTensor(values=[101, 201, 301],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"wire": wire_tensor}
      logits, _, _ = tf.contrib.layers.weighted_sum_from_feature_columns(
          features, [hashed_sparse], num_outputs=5)
      with self.test_session():
        tf.initialize_all_variables().run()
        self.assertAllEqual(logits.eval().shape, [2, 5])
  
    def testWeightedSparseColumn(self):
      ids = tf.contrib.layers.sparse_column_with_keys(
          "ids", ["marlo", "omar", "stringer"])
      ids_tensor = tf.SparseTensor(values=["stringer", "stringer", "marlo"],
                                   indices=[[0, 0], [1, 0], [1, 1]],
                                   shape=[2, 2])
      weighted_ids = tf.contrib.layers.weighted_sparse_column(ids, "weights")
      weights_tensor = tf.SparseTensor(values=[10.0, 20.0, 30.0],
                                       indices=[[0, 0], [1, 0], [1, 1]],
                                       shape=[2, 2])
      features = {"ids": ids_tensor,
                  "weights": weights_tensor}
      logits, _, _ = tf.contrib.layers.weighted_sum_from_feature_columns(
          features, [weighted_ids], num_outputs=5)
      with self.test_session():
        tf.initialize_all_variables().run()
        tf.initialize_all_tables().run()
        self.assertAllEqual(logits.eval().shape, [2, 5])
  
    def testCrossedColumn(self):
      a = tf.contrib.layers.sparse_column_with_hash_bucket("aaa",
                                                           hash_bucket_size=100)
      b = tf.contrib.layers.sparse_column_with_hash_bucket("bbb",
                                                           hash_bucket_size=100)
      crossed = tf.contrib.layers.crossed_column(
          set([a, b]), hash_bucket_size=10000)
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"aaa": wire_tensor, "bbb": wire_tensor}
      logits, _, _ = tf.contrib.layers.weighted_sum_from_feature_columns(
          features, [crossed], num_outputs=5)
      with self.test_session():
        tf.initialize_all_variables().run()
        self.assertAllEqual(logits.eval().shape, [2, 5])
  
    def testEmbeddingColumn(self):
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      wire_tensor = tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                    indices=[[0, 0], [1, 0], [1, 1]],
                                    shape=[2, 2])
      features = {"wire": wire_tensor}
      embeded_sparse = tf.contrib.layers.embedding_column(hashed_sparse, 10)
      with self.test_session():
        with self.assertRaisesRegexp(
            ValueError, "Error creating weighted sum for column: wire_embedding"):
          tf.initialize_all_variables().run()
          tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                              [embeded_sparse],
                                                              num_outputs=5)
  
    def testRealValuedColumnWithMultiDimensions(self):
      real_valued = tf.contrib.layers.real_valued_column("price", 2)
      features = {"price": tf.constant([[20., 10.], [110, 0.], [-3, 30]])}
      logits, _, _ = tf.contrib.layers.weighted_sum_from_feature_columns(
          features, [real_valued], num_outputs=5)
      with self.test_session():
        tf.initialize_all_variables().run()
        self.assertAllEqual(logits.eval().shape, [3, 5])
  
    def testBucketizedColumnWithMultiDimensions(self):
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price", 2),
          boundaries=[0., 10., 100.])
      features = {"price": tf.constant([[20., 10.], [110, 0.], [-3, 30]])}
      logits, _, _ = tf.contrib.layers.weighted_sum_from_feature_columns(
          features, [bucket], num_outputs=5)
      with self.test_session():
        tf.initialize_all_variables().run()
        self.assertAllEqual(logits.eval().shape, [3, 5])
  
    def testAllWideColumns(self):
      real_valued = tf.contrib.layers.real_valued_column("income", 2)
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price"),
          boundaries=[0., 10., 100.])
      hashed_sparse = tf.contrib.layers.sparse_column_with_hash_bucket("wire", 10)
      crossed = tf.contrib.layers.crossed_column([bucket, hashed_sparse], 100)
      features = {
          "income": tf.constant([[20., 10], [110, 0], [-3, 30]]),
          "price": tf.constant([[20.], [110], [-3]]),
          "wire": tf.SparseTensor(values=["omar", "stringer", "marlo"],
                                  indices=[[0, 0], [1, 0], [2, 0]],
                                  shape=[3, 1])
      }
      output, _, _ = tf.contrib.layers.weighted_sum_from_feature_columns(
          features, [real_valued, bucket, hashed_sparse, crossed],
          num_outputs=5)
      with self.test_session():
        tf.initialize_all_variables().run()
        self.assertAllEqual(output.eval().shape, [3, 5])
  
    def testPredictions(self):
      language = tf.contrib.layers.sparse_column_with_keys(
          column_name="language",
          keys=["english", "finnish", "hindi"])
      age = tf.contrib.layers.real_valued_column("age")
      with tf.Graph().as_default():
        features = {
            "age": tf.constant([[1], [2]]),
            "language": tf.SparseTensor(values=["hindi", "english"],
                                        indices=[[0, 0], [1, 0]],
                                        shape=[2, 1]),
        }
        output, column_to_variable, bias = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [age, language],
                                                                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          self.assertAllClose(output.eval(), [[0.], [0.]])
  
          sess.run(bias.assign([0.1]))
          self.assertAllClose(output.eval(), [[0.1], [0.1]])
  
          # score: 0.1 + age*0.1
          sess.run(column_to_variable[age][0].assign([[0.2]]))
          self.assertAllClose(output.eval(), [[0.3], [0.5]])
  
          # score: 0.1 + age*0.1 + language_weight[language_index]
          sess.run(column_to_variable[language][0].assign([[0.1], [0.3], [0.2]]))
          self.assertAllClose(output.eval(), [[0.5], [0.6]])
  
    def testJointPredictions(self):
      country = tf.contrib.layers.sparse_column_with_keys(
          column_name="country",
          keys=["us", "finland"])
      language = tf.contrib.layers.sparse_column_with_keys(
          column_name="language",
          keys=["english", "finnish", "hindi"])
      with tf.Graph().as_default():
        features = {
            "country": tf.SparseTensor(values=["finland", "us"],
                                       indices=[[0, 0], [1, 0]],
                                       shape=[2, 1]),
            "language": tf.SparseTensor(values=["hindi", "english"],
                                        indices=[[0, 0], [1, 0]],
                                        shape=[2, 1]),
        }
        output, variables, bias = (
            tf.contrib.layers.joint_weighted_sum_from_feature_columns(
                features, [country, language], num_outputs=1))
        # Assert that only a single weight is created.
        self.assertEqual(len(variables), 1)
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          self.assertAllClose(output.eval(), [[0.], [0.]])
  
          sess.run(bias.assign([0.1]))
          self.assertAllClose(output.eval(), [[0.1], [0.1]])
  
          # shape is [5,1] because 1 class and 2 + 3 features.
          self.assertEquals(variables[0].get_shape().as_list(), [5, 1])
  
          # score: bias + country_weight + language_weight
          sess.run(variables[0].assign([[0.1], [0.2], [0.3], [0.4], [0.5]]))
          self.assertAllClose(output.eval(), [[0.8], [0.5]])
  
    def testJointPredictionsWeightedFails(self):
      language = tf.contrib.layers.weighted_sparse_column(
          tf.contrib.layers.sparse_column_with_keys(
              column_name="language",
              keys=["english", "finnish", "hindi"]),
          "weight")
      with tf.Graph().as_default():
        features = {
            "weight": tf.constant([[1], [2]]),
            "language": tf.SparseTensor(values=["hindi", "english"],
                                        indices=[[0, 0], [1, 0]],
                                        shape=[2, 1]),
        }
        with self.assertRaises(AssertionError):
          tf.contrib.layers.joint_weighted_sum_from_feature_columns(
              features, [language], num_outputs=1)
  
    def testJointPredictionsRealFails(self):
      age = tf.contrib.layers.real_valued_column("age")
      with tf.Graph().as_default():
        features = {
            "age": tf.constant([[1], [2]]),
        }
        with self.assertRaises(NotImplementedError):
          tf.contrib.layers.joint_weighted_sum_from_feature_columns(
              features, [age], num_outputs=1)
  
    def testPredictionsWithWeightedSparseColumn(self):
      language = tf.contrib.layers.sparse_column_with_keys(
          column_name="language",
          keys=["english", "finnish", "hindi"])
      weighted_language = tf.contrib.layers.weighted_sparse_column(
          sparse_id_column=language,
          weight_column_name="age")
      with tf.Graph().as_default():
        features = {
            "language": tf.SparseTensor(values=["hindi", "english"],
                                        indices=[[0, 0], [1, 0]],
                                        shape=[2, 1]),
            "age": tf.SparseTensor(values=[10.0, 20.0],
                                   indices=[[0, 0], [1, 0]],
                                   shape=[2, 1])
        }
        output, column_to_variable, bias = (
            tf.contrib.layers.weighted_sum_from_feature_columns(
                features, [weighted_language], num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          self.assertAllClose(output.eval(), [[0.], [0.]])
  
          sess.run(bias.assign([0.1]))
          self.assertAllClose(output.eval(), [[0.1], [0.1]])
  
          # score: bias + age*language_weight[index]
          sess.run(column_to_variable[weighted_language][0].assign(
              [[0.1], [0.2], [0.3]]))
          self.assertAllClose(output.eval(), [[3.1], [2.1]])
  
    def testPredictionsWithMultivalentColumnButNoCross(self):
      language = tf.contrib.layers.sparse_column_with_keys(
          column_name="language",
          keys=["english", "turkish", "hindi"])
      with tf.Graph().as_default():
        features = {
            "language": tf.SparseTensor(values=["hindi", "english"],
                                        indices=[[0, 0], [0, 1]],
                                        shape=[1, 2])
        }
        output, column_to_variable, bias = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [language],
                                                                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          # score: 0.1 + language_weight['hindi'] + language_weight['english']
          sess.run(bias.assign([0.1]))
          sess.run(column_to_variable[language][0].assign([[0.1], [0.3], [0.2]]))
          self.assertAllClose(output.eval(), [[0.4]])
  
    def testSparseFeatureColumnWithHashedBucketSize(self):
      movies = tf.contrib.layers.sparse_column_with_hash_bucket(
          column_name="movies", hash_bucket_size=15)
      with tf.Graph().as_default():
        features = {
            "movies": tf.SparseTensor(
                values=["matrix", "head-on", "winter sleep"],
                indices=[[0, 0], [0, 1], [1, 0]],
                shape=[2, 2])
        }
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [movies],
                                                                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          weights = column_to_variable[movies][0]
          self.assertEqual(weights.get_shape(), (15, 1))
          sess.run(weights.assign(weights + 0.4))
          # score for first example = 0.4 (matrix) + 0.4 (head-on) = 0.8
          # score for second example = 0.4 (winter sleep)
          self.assertAllClose(output.eval(), [[0.8], [0.4]])
  
    def testCrossUsageInPredictions(self):
      language = tf.contrib.layers.sparse_column_with_hash_bucket(
          "language", hash_bucket_size=3)
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      country_language = tf.contrib.layers.crossed_column(
          [language, country], hash_bucket_size=10)
      with tf.Graph().as_default():
        features = {
            "language": tf.SparseTensor(values=["english", "spanish"],
                                        indices=[[0, 0], [1, 0]],
                                        shape=[2, 1]),
            "country": tf.SparseTensor(values=["US", "SV"],
                                       indices=[[0, 0], [1, 0]],
                                       shape=[2, 1])
        }
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(
                features, [country_language],
                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          weights = column_to_variable[country_language][0]
          sess.run(weights.assign(weights + 0.4))
          self.assertAllClose(output.eval(), [[0.4], [0.4]])
  
    def testCrossColumnByItself(self):
      language = tf.contrib.layers.sparse_column_with_hash_bucket(
          "language", hash_bucket_size=3)
      language_language = tf.contrib.layers.crossed_column(
          [language, language], hash_bucket_size=10)
      with tf.Graph().as_default():
        features = {
            "language": tf.SparseTensor(values=["english", "spanish"],
                                        indices=[[0, 0], [0, 1]],
                                        shape=[1, 2]),
        }
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(
                features, [language_language],
                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          weights = column_to_variable[language_language][0]
          sess.run(weights.assign(weights + 0.4))
          # There are two features inside language. If we cross it by itself we'll
          # have four crossed features.
          self.assertAllClose(output.eval(), [[1.6]])
  
    def testMultivalentCrossUsageInPredictions(self):
      language = tf.contrib.layers.sparse_column_with_hash_bucket(
          "language", hash_bucket_size=3)
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      country_language = tf.contrib.layers.crossed_column(
          [language, country], hash_bucket_size=10)
      with tf.Graph().as_default():
        features = {
            "language": tf.SparseTensor(values=["english", "spanish"],
                                        indices=[[0, 0], [0, 1]],
                                        shape=[1, 2]),
            "country": tf.SparseTensor(values=["US", "SV"],
                                       indices=[[0, 0], [0, 1]],
                                       shape=[1, 2])
        }
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(
                features, [country_language],
                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          weights = column_to_variable[country_language][0]
          sess.run(weights.assign(weights + 0.4))
          # There are four crosses each with 0.4 weight.
          # score = 0.4 + 0.4 + 0.4 + 0.4
          self.assertAllClose(output.eval(), [[1.6]])
  
    def testMultivalentCrossUsageInPredictionsWithPartition(self):
      # bucket size has to be big enough to allwo sharding.
      language = tf.contrib.layers.sparse_column_with_hash_bucket(
          "language", hash_bucket_size=64 << 19)
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=64 << 18)
      country_language = tf.contrib.layers.crossed_column(
          [language, country], hash_bucket_size=64 << 18)
      with tf.Graph().as_default():
        features = {
            "language": tf.SparseTensor(values=["english", "spanish"],
                                        indices=[[0, 0], [0, 1]],
                                        shape=[1, 2]),
            "country": tf.SparseTensor(values=["US", "SV"],
                                       indices=[[0, 0], [0, 1]],
                                       shape=[1, 2])
        }
        with tf.variable_scope(
            "weighted_sum_from_feature_columns",
            features.values(),
            partitioner=tf.min_max_variable_partitioner(
                max_partitions=10, min_slice_size=((64 << 20) - 1))) as scope:
          output, column_to_variable, _ = (
              tf.contrib.layers.weighted_sum_from_feature_columns(
                  features, [country, language, country_language],
                  num_outputs=1,
                  scope=scope))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          self.assertEqual(2, len(column_to_variable[country]))
          self.assertEqual(3, len(column_to_variable[language]))
          self.assertEqual(2, len(column_to_variable[country_language]))
  
          weights = column_to_variable[country_language]
          for partition_variable in weights:
            sess.run(partition_variable.assign(partition_variable + 0.4))
          # There are four crosses each with 0.4 weight.
          # score = 0.4 + 0.4 + 0.4 + 0.4
          self.assertAllClose(output.eval(), [[1.6]])
  
    def testRealValuedColumnHavingMultiDimensions(self):
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      age = tf.contrib.layers.real_valued_column("age")
      # The following RealValuedColumn has 3 dimensions.
      incomes = tf.contrib.layers.real_valued_column("incomes", 3)
  
      with tf.Graph().as_default():
        features = {"age": tf.constant([[1], [1]]),
                    "incomes": tf.constant([[100., 200., 300.], [10., 20., 30.]]),
                    "country": tf.SparseTensor(values=["US", "SV"],
                                               indices=[[0, 0], [1, 0]],
                                               shape=[2, 2])}
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(
                features, [country, age, incomes],
                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          incomes_weights = column_to_variable[incomes][0]
          sess.run(incomes_weights.assign([[0.1], [0.2], [0.3]]))
          self.assertAllClose(output.eval(), [[140.], [14.]])
  
    def testMulticlassWithRealValuedColumnHavingMultiDimensions(self):
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      age = tf.contrib.layers.real_valued_column("age")
      # The following RealValuedColumn has 3 dimensions.
      incomes = tf.contrib.layers.real_valued_column("incomes", 3)
      with tf.Graph().as_default():
        features = {"age": tf.constant([[1], [1]]),
                    "incomes": tf.constant([[100., 200., 300.], [10., 20., 30.]]),
                    "country": tf.SparseTensor(values=["US", "SV"],
                                               indices=[[0, 0], [1, 0]],
                                               shape=[2, 2])}
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(
                features, [country, age, incomes],
                num_outputs=5))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          incomes_weights = column_to_variable[incomes][0]
          sess.run(incomes_weights.assign([[0.01, 0.1, 1., 10., 100.],
                                           [0.02, 0.2, 2., 20., 200.],
                                           [0.03, 0.3, 3., 30., 300.]]))
          self.assertAllClose(output.eval(), [[14., 140., 1400., 14000., 140000.],
                                              [1.4, 14., 140., 1400., 14000.]])
  
    def testBucketizedColumn(self):
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price"),
          boundaries=[0., 10., 100.])
      with tf.Graph().as_default():
        # buckets 2, 3, 0
        features = {"price": tf.constant([[20.], [110], [-3]])}
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [bucket],
                                                                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          sess.run(column_to_variable[bucket][0].assign([[0.1], [0.2], [0.3], [0.4
                                                                              ]]))
          self.assertAllClose(output.eval(), [[0.3], [0.4], [0.1]])
  
    def testBucketizedColumnHavingMultiDimensions(self):
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price", 2),
          boundaries=[0., 10., 100.])
      with tf.Graph().as_default():
        # buckets 2, 3, 0
        features = {"price": tf.constant([[20., 210], [110, 50], [-3, -30]]),
                    "country": tf.SparseTensor(values=["US", "SV"],
                                               indices=[[0, 0], [1, 0]],
                                               shape=[3, 2])}
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [bucket, country],
                                                                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          # dimension = 2, bucket_size = 4, num_classes = 1
          sess.run(column_to_variable[bucket][0].assign(
              [[0.1], [0.2], [0.3], [0.4], [1], [2], [3], [4]]))
          self.assertAllClose(output.eval(), [[0.3 + 4], [0.4 + 3], [0.1 + 1]])
  
    def testMulticlassWithBucketizedColumnHavingMultiDimensions(self):
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price", 2),
          boundaries=[0., 10., 100.])
      with tf.Graph().as_default():
        # buckets 2, 3, 0
        features = {"price": tf.constant([[20., 210], [110, 50], [-3, -30]]),
                    "country": tf.SparseTensor(values=["US", "SV"],
                                               indices=[[0, 0], [1, 0]],
                                               shape=[3, 2])}
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [bucket, country],
                                                                num_outputs=5))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          # dimension = 2, bucket_size = 4, num_classes = 5
          sess.run(column_to_variable[bucket][0].assign(
              [[0.1, 1, 10, 100, 1000], [0.2, 2, 20, 200, 2000],
               [0.3, 3, 30, 300, 3000], [0.4, 4, 40, 400, 4000],
               [5, 50, 500, 5000, 50000], [6, 60, 600, 6000, 60000],
               [7, 70, 700, 7000, 70000], [8, 80, 800, 8000, 80000]]))
          self.assertAllClose(
              output.eval(),
              [[0.3 + 8, 3 + 80, 30 + 800, 300 + 8000, 3000 + 80000],
               [0.4 + 7, 4 + 70, 40 + 700, 400 + 7000, 4000 + 70000],
               [0.1 + 5, 1 + 50, 10 + 500, 100 + 5000, 1000 + 50000]])
  
    def testCrossWithBucketizedColumn(self):
      price_bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price"),
          boundaries=[0., 10., 100.])
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      country_price = tf.contrib.layers.crossed_column(
          [country, price_bucket], hash_bucket_size=10)
      with tf.Graph().as_default():
        features = {
            "price": tf.constant([[20.]]),
            "country": tf.SparseTensor(values=["US", "SV"],
                                       indices=[[0, 0], [0, 1]],
                                       shape=[1, 2])
        }
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [country_price],
                                                                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          weights = column_to_variable[country_price][0]
          sess.run(weights.assign(weights + 0.4))
          # There are two crosses each with 0.4 weight.
          # score = 0.4 + 0.4
          self.assertAllClose(output.eval(), [[0.8]])
  
    def testCrossWithCrossedColumn(self):
      price_bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price"),
          boundaries=[0., 10., 100.])
      language = tf.contrib.layers.sparse_column_with_hash_bucket(
          "language", hash_bucket_size=3)
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      country_language = tf.contrib.layers.crossed_column(
          [language, country], hash_bucket_size=10)
      country_language_price = tf.contrib.layers.crossed_column(
          set([country_language, price_bucket]),
          hash_bucket_size=15)
      with tf.Graph().as_default():
        features = {
            "price": tf.constant([[20.]]),
            "country": tf.SparseTensor(values=["US", "SV"],
                                       indices=[[0, 0], [0, 1]],
                                       shape=[1, 2]),
            "language": tf.SparseTensor(values=["english", "spanish"],
                                        indices=[[0, 0], [0, 1]],
                                        shape=[1, 2])
        }
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(
                features, [country_language_price],
                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          weights = column_to_variable[country_language_price][0]
          sess.run(weights.assign(weights + 0.4))
          # There are two crosses each with 0.4 weight.
          # score = 0.4 + 0.4 + 0.4 + 0.4
          self.assertAllClose(output.eval(), [[1.6]])
  
    def testIntegerizedColumn(self):
      product = tf.contrib.layers.sparse_column_with_integerized_feature(
          "product", bucket_size=5)
      with tf.Graph().as_default():
        features = {"product": tf.SparseTensor(values=[0, 4, 2],
                                               indices=[[0, 0], [1, 0], [2, 0]],
                                               shape=[3, 1])}
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [product],
                                                                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
          product_weights = column_to_variable[product][0]
          sess.run(product_weights.assign([[0.1], [0.2], [0.3], [0.4], [0.5]]))
          self.assertAllClose(output.eval(), [[0.1], [0.5], [0.3]])
  
    def testIntegerizedColumnWithInvalidId(self):
      product = tf.contrib.layers.sparse_column_with_integerized_feature(
          "product", bucket_size=5)
      with tf.Graph().as_default():
        features = {"product": tf.SparseTensor(values=[5, 4, 7],
                                               indices=[[0, 0], [1, 0], [2, 0]],
                                               shape=[3, 1])}
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [product],
                                                                num_outputs=1))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
          product_weights = column_to_variable[product][0]
          sess.run(product_weights.assign([[0.1], [0.2], [0.3], [0.4], [0.5]]))
          self.assertAllClose(output.eval(), [[0.1], [0.5], [0.3]])
  
    def testMulticlassWithOnlyBias(self):
      with tf.Graph().as_default():
        features = {"age": tf.constant([[10.], [20.], [30.], [40.]])}
        output, _, bias = tf.contrib.layers.weighted_sum_from_feature_columns(
            features, [tf.contrib.layers.real_valued_column("age")],
            num_outputs=3)
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
          sess.run(bias.assign([0.1, 0.2, 0.3]))
          self.assertAllClose(output.eval(), [[0.1, 0.2, 0.3], [0.1, 0.2, 0.3],
                                              [0.1, 0.2, 0.3], [0.1, 0.2, 0.3]])
  
    def testMulticlassWithRealValuedColumn(self):
      with tf.Graph().as_default():
        column = tf.contrib.layers.real_valued_column("age")
        features = {"age": tf.constant([[10.], [20.], [30.], [40.]])}
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [column],
                                                                num_outputs=3))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
          weights = column_to_variable[column][0]
          self.assertEqual(weights.get_shape(), (1, 3))
          sess.run(weights.assign([[0.01, 0.03, 0.05]]))
          self.assertAllClose(output.eval(), [[0.1, 0.3, 0.5], [0.2, 0.6, 1.0],
                                              [0.3, 0.9, 1.5], [0.4, 1.2, 2.0]])
  
    def testMulticlassWithSparseColumn(self):
      with tf.Graph().as_default():
        column = tf.contrib.layers.sparse_column_with_keys(
            column_name="language",
            keys=["english", "arabic", "hindi", "russian", "swahili"])
        features = {
            "language": tf.SparseTensor(
                values=["hindi", "english", "arabic", "russian"],
                indices=[[0, 0], [1, 0], [2, 0], [3, 0]],
                shape=[4, 1])
        }
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [column],
                                                                num_outputs=3))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
          weights = column_to_variable[column][0]
          self.assertEqual(weights.get_shape(), (5, 3))
          sess.run(weights.assign([[0.1, 0.4, 0.7], [0.2, 0.5, 0.8],
                                   [0.3, 0.6, 0.9], [0.4, 0.7, 1.0], [0.5, 0.8,
                                                                      1.1]]))
          self.assertAllClose(output.eval(), [[0.3, 0.6, 0.9], [0.1, 0.4, 0.7],
                                              [0.2, 0.5, 0.8], [0.4, 0.7, 1.0]])
  
    def testMulticlassWithBucketizedColumn(self):
      column = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price"),
          boundaries=[0., 100., 500., 1000.])
      with tf.Graph().as_default():
        # buckets 0, 2, 1, 2
        features = {"price": tf.constant([[-3], [110], [20.], [210]])}
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [column],
                                                                num_outputs=3))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          weights = column_to_variable[column][0]
          self.assertEqual(weights.get_shape(), (5, 3))
          sess.run(weights.assign([[0.1, 0.4, 0.7], [0.2, 0.5, 0.8],
                                   [0.3, 0.6, 0.9], [0.4, 0.7, 1.0], [0.5, 0.8,
                                                                      1.1]]))
          self.assertAllClose(output.eval(), [[0.1, 0.4, 0.7], [0.3, 0.6, 0.9],
                                              [0.2, 0.5, 0.8], [0.3, 0.6, 0.9]])
  
    def testMulticlassWithCrossedColumn(self):
      language = tf.contrib.layers.sparse_column_with_hash_bucket(
          "language", hash_bucket_size=3)
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=2)
      column = tf.contrib.layers.crossed_column(
          {language, country}, hash_bucket_size=5)
      with tf.Graph().as_default():
        features = {
            "language": tf.SparseTensor(
                values=["english", "spanish", "russian", "swahili"],
                indices=[[0, 0], [1, 0], [2, 0], [3, 0]],
                shape=[4, 1]),
            "country": tf.SparseTensor(values=["US", "SV", "RU", "KE"],
                                       indices=[[0, 0], [1, 0], [2, 0], [3, 0]],
                                       shape=[4, 1])
        }
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [column],
                                                                num_outputs=3))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          weights = column_to_variable[column][0]
          self.assertEqual(weights.get_shape(), (5, 3))
          sess.run(weights.assign([[0.1, 0.4, 0.7], [0.2, 0.5, 0.8],
                                   [0.3, 0.6, 0.9], [0.4, 0.7, 1.0], [0.5, 0.8,
                                                                      1.1]]))
          self.assertAllClose(tf.shape(output).eval(), [4, 3])
  
    def testMulticlassWithMultivalentColumn(self):
      column = tf.contrib.layers.sparse_column_with_keys(
          column_name="language",
          keys=["english", "turkish", "hindi", "russian", "swahili"])
      with tf.Graph().as_default():
        features = {
            "language": tf.SparseTensor(
                values=["hindi", "english", "turkish", "turkish", "english"],
                indices=[[0, 0], [0, 1], [1, 0], [2, 0], [3, 0]],
                shape=[4, 2])
        }
        output, column_to_variable, _ = (
            tf.contrib.layers.weighted_sum_from_feature_columns(features,
                                                                [column],
                                                                num_outputs=3))
        with self.test_session() as sess:
          tf.initialize_all_variables().run()
          tf.initialize_all_tables().run()
  
          weights = column_to_variable[column][0]
          self.assertEqual(weights.get_shape(), (5, 3))
          sess.run(weights.assign([[0.1, 0.4, 0.7], [0.2, 0.5, 0.8],
                                   [0.3, 0.6, 0.9], [0.4, 0.7, 1.0], [0.5, 0.8,
                                                                      1.1]]))
          self.assertAllClose(output.eval(), [[0.4, 1.0, 1.6], [0.2, 0.5, 0.8],
                                              [0.2, 0.5, 0.8], [0.1, 0.4, 0.7]])
  
    def testVariablesAddedToCollection(self):
      price_bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price"),
          boundaries=[0., 10., 100.])
      country = tf.contrib.layers.sparse_column_with_hash_bucket(
          "country", hash_bucket_size=5)
      country_price = tf.contrib.layers.crossed_column(
          [country, price_bucket], hash_bucket_size=10)
      with tf.Graph().as_default():
        features = {
            "price": tf.constant([[20.]]),
            "country": tf.SparseTensor(values=["US", "SV"],
                                       indices=[[0, 0], [0, 1]],
                                       shape=[1, 2])
        }
        tf.contrib.layers.weighted_sum_from_feature_columns(
            features, [country_price, price_bucket],
            num_outputs=1,
            weight_collections=["my_collection"])
        weights = tf.get_collection("my_collection")
        # 3 = bias + price_bucket + country_price
        self.assertEqual(3, len(weights))
  
  
  class ParseExampleTest(tf.test.TestCase):
  
    def testParseExample(self):
      bucket = tf.contrib.layers.bucketized_column(
          tf.contrib.layers.real_valued_column("price", dimension=3),
          boundaries=[0., 10., 100.])
      wire_cast = tf.contrib.layers.sparse_column_with_keys(
          "wire_cast", ["marlo", "omar", "stringer"])
      # buckets 2, 3, 0
      data = tf.train.Example(features=tf.train.Features(feature={
          "price": tf.train.Feature(float_list=tf.train.FloatList(value=[20., 110,
                                                                         -3])),
          "wire_cast": tf.train.Feature(bytes_list=tf.train.BytesList(value=[
              b"stringer", b"marlo"
          ])),
      }))
      output = tf.contrib.layers.parse_feature_columns_from_examples(
          serialized=[data.SerializeToString()],
          feature_columns=[bucket, wire_cast])
      self.assertIn(bucket, output)
      self.assertIn(wire_cast, output)
      with self.test_session():
        tf.initialize_all_tables().run()
        self.assertAllEqual(output[bucket].eval(), [[2, 3, 0]])
        self.assertAllEqual(output[wire_cast].indices.eval(), [[0, 0], [0, 1]])
        self.assertAllEqual(output[wire_cast].values.eval(), [2, 0])
  
  
  class InferRealValuedColumnTest(tf.test.TestCase):
  
    def testTensorInt32(self):
      self.assertEqual(
          tf.contrib.layers.infer_real_valued_columns(
              tf.zeros(shape=[33, 4], dtype=tf.int32)),
          [tf.contrib.layers.real_valued_column("", dimension=4, dtype=tf.int32)])
  
    def testTensorInt64(self):
      self.assertEqual(
          tf.contrib.layers.infer_real_valued_columns(
              tf.zeros(shape=[33, 4], dtype=tf.int64)),
          [tf.contrib.layers.real_valued_column("", dimension=4, dtype=tf.int64)])
  
    def testTensorFloat32(self):
      self.assertEqual(
          tf.contrib.layers.infer_real_valued_columns(
              tf.zeros(shape=[33, 4], dtype=tf.float32)),
          [tf.contrib.layers.real_valued_column(
              "", dimension=4, dtype=tf.float32)])
  
    def testTensorFloat64(self):
      self.assertEqual(
          tf.contrib.layers.infer_real_valued_columns(
              tf.zeros(shape=[33, 4], dtype=tf.float64)),
          [tf.contrib.layers.real_valued_column(
              "", dimension=4, dtype=tf.float64)])
  
    def testDictionary(self):
      self.assertItemsEqual(
          tf.contrib.layers.infer_real_valued_columns({
              "a": tf.zeros(shape=[33, 4], dtype=tf.int32),
              "b": tf.zeros(shape=[3, 2], dtype=tf.float32)
          }),
          [tf.contrib.layers.real_valued_column(
              "a", dimension=4, dtype=tf.int32),
           tf.contrib.layers.real_valued_column(
               "b", dimension=2, dtype=tf.float32)])
  
    def testNotGoodDtype(self):
      with self.assertRaises(ValueError):
        tf.contrib.layers.infer_real_valued_columns(
            tf.constant([["a"]], dtype=tf.string))
  
    def testSparseTensor(self):
      with self.assertRaises(ValueError):
        tf.contrib.layers.infer_real_valued_columns(
            tf.SparseTensor(indices=[[0, 0]], values=["a"], shape=[1, 1]))
  
  
  if __name__ == "__main__":
    tf.test.main()
  
apis: tensorflow.python.summary.impl.gcs.IsDirectory tensorflow.python.summary.impl.event_file_loader.EventFileLoader tensorflow.python.platform.gfile.Walk tensorflow.python.summary.impl.gcs_file_loader.GCSFileLoader tensorflow.python.summary.impl.gcs.Exists tensorflow.python.platform.gfile.Exists tensorflow.python.summary.impl.gcs.IsGCSPath tensorflow.python.platform.gfile.IsDirectory tensorflow.python.summary.impl.gcs.ListDirectory tensorflow.python.platform.gfile.ListDirectory tensorflow.python.platform.gfile.Open tensorflow.python.summary.impl.gcs.ListRecursively
apis: tensorflow.python.ops.math_ops.reduce_sum tensorflow.python.ops.array_ops.shape tensorflow.python.ops.check_ops.assert_positive tensorflow.python.ops.check_ops.assert_non_negative tensorflow.contrib.distributions.python.ops.distribution_util.log_combinations tensorflow.python.ops.check_ops.assert_equal tensorflow.python.framework.ops.name_scope tensorflow.python.ops.array_ops.expand_dims tensorflow.python.ops.array_ops.ones_like tensorflow.contrib.distributions.python.ops.distribution_util.assert_integer_form tensorflow.contrib.distributions.python.ops.distribution_util.append_class_fun_doc tensorflow.python.ops.check_ops.assert_rank_at_least tensorflow.python.ops.special_math_ops.lbeta tensorflow.python.framework.ops.convert_to_tensor tensorflow.python.ops.array_ops.matrix_set_diag
apis: tensorflow.initialize_all_variables numpy.sum numpy.ones tensorflow.merge_all_summaries numpy.random.seed tensorflow.train.summary_iterator tensorflow.Variable tensorflow.python.platform.gfile.MakeDirs tensorflow.train.SummaryWriter tensorflow.contrib.metrics.streaming_accuracy numpy.random.rand tensorflow.constant tensorflow.test.main tensorflow.scalar_summary tensorflow.train.string_input_producer numpy.argmax tensorflow.mul tensorflow.train.Saver tensorflow.initialize_local_variables tensorflow.argmax tensorflow.gfile.Exists numpy.random.randint tensorflow.gfile.DeleteRecursively
apis: tensorflow.initialize_all_variables tensorflow.train.FloatList tensorflow.contrib.layers.real_valued_column tensorflow.train.BytesList tensorflow.contrib.layers.sparse_column_with_integerized_feature tensorflow.truncated_normal_initializer tensorflow.Graph tensorflow.min_max_variable_partitioner tensorflow.contrib.layers.weighted_sparse_column tensorflow.contrib.layers.joint_weighted_sum_from_feature_columns tensorflow.constant tensorflow.test.main tensorflow.contrib.layers.embedding_column tensorflow.contrib.layers.input_from_feature_columns numpy.tile tensorflow.constant_initializer tensorflow.shape tensorflow.contrib.layers.sparse_column_with_keys tensorflow.contrib.layers.bucketized_column tensorflow.contrib.layers.sparse_column_with_hash_bucket tensorflow.get_collection tensorflow.contrib.layers.weighted_sum_from_feature_columns tensorflow.SparseTensor tensorflow.contrib.layers.one_hot_column tensorflow.gradients tensorflow.contrib.layers.crossed_column tensorflow.contrib.layers.python.layers.feature_column_ops._Transformer tensorflow.contrib.layers.hashed_embedding_column tensorflow.zeros tensorflow.initialize_all_tables tensorflow.trainable_variables