예제 #1
0
def sum(x):  # pylint: disable=redefined-builtin
    """Computes the sum of a `Column`.

  Args:
    x: An input `Column' wrapping a `Tensor`.

  Returns:
    A `Statistic`.
  """
    if not isinstance(x.tensor, tf.Tensor):
        raise TypeError('Expected a Tensor, but got %r' % x.tensor)

    # pylint: disable=protected-access
    return api._AnalyzerOutput(tf.placeholder(x.tensor.dtype, ()),
                               api.CanonicalAnalyzers.SUM, [x], {})
예제 #2
0
def uniques(x, top_k=None, frequency_threshold=None):
    """Returns the unique values of the input tensor.

  Computes The unique values taken by the input column `x`, which can be backed
  by a `Tensor` or `SparseTensor` of any size.  The unique values will be
  aggregated over all dimensions of `x` and all instances.

  The unique values are sorted by decreasing frequency and then decreasing
  value.

  Args:
    x: An input `Column` wrapping a `Tensor` or `SparseTensor`.
    top_k: Limit the generated vocabulary to the first `top_k` elements. If set
      to None, the full vocabulary is generated.
    frequency_threshold: Limit the generated vocabulary only to elements whose
      frequency is >= to the supplied threshold. If set to None, the full
      vocabulary is generated.

  Returns:
    The unique values of `x`.

  Raises:
    ValueError: If `top_k` or `frequency_threshold` is negative.
  """
    if top_k is not None:
        top_k = long(top_k)
        if top_k < 0:
            raise ValueError('top_k must be non-negative, but got: %r' % top_k)

    if frequency_threshold is not None:
        frequency_threshold = long(frequency_threshold)
        if frequency_threshold < 0:
            raise ValueError(
                'frequency_threshold must be non-negative, but got: %r' %
                frequency_threshold)

    if isinstance(x.tensor, tf.SparseTensor):
        values = x.tensor.values
    else:
        values = x.tensor
    arg_dict = {'top_k': top_k, 'frequency_threshold': frequency_threshold}
    # Create output placeholder whose shape is a 1-d tensor of unkown size.
    # pylint: disable=protected-access
    return api._AnalyzerOutput(tf.placeholder(values.dtype, (None, )),
                               api.CanonicalAnalyzers.UNIQUES, [x], arg_dict)
예제 #3
0
def sum(x, reduce_instance_dims=True):  # pylint: disable=redefined-builtin
    """Computes the sum of a `Column`.

  Args:
    x: An input `Column' wrapping a `Tensor`.
    reduce_instance_dims: By default collapses the batch and instance dimensions
        to arrive at a single scalar output. If False, only collapses the batch
        dimension and outputs a vector of the same shape as the output.

  Returns:
    A `Statistic`.
  """
    if not isinstance(x.tensor, tf.Tensor):
        raise TypeError('Expected a Tensor, but got %r' % x.tensor)

    arg_dict = {'reduce_instance_dims': reduce_instance_dims}
    # pylint: disable=protected-access
    return api._AnalyzerOutput(
        tf.placeholder(x.tensor.dtype,
                       _get_output_shape(x, reduce_instance_dims)),
        api.CanonicalAnalyzers.SUM, [x], arg_dict)