Example #1
0
def scale_to_z_score(x, elementwise=False, name=None, output_dtype=None):
  """Returns a standardized column with mean 0 and variance 1.

  Scaling to z-score subtracts out the mean and divides by standard deviation.
  Note that the standard deviation computed here is based on the biased variance
  (0 delta degrees of freedom), as computed by analyzers.var.

  Args:
    x: A numeric `Tensor`.
    elementwise: If true, scales each element of the tensor independently;
        otherwise uses the mean and variance of the whole tensor.
    name: (Optional) A name for this operation.
    output_dtype: (Optional) If not None, casts the output tensor to this type.

  Returns:
    A `Tensor` containing the input column scaled to mean 0 and variance 1
    (standard deviation 1), given by: (x - mean(x)) / std_dev(x).
    If `x` is floating point, the mean will have the same type as `x`. If `x` is
    integral, the output is cast to tf.float32.

    Note that TFLearn generally permits only tf.int64 and tf.float32, so casting
    this scaler's output may be necessary.
  """
  with tf.name_scope(name, 'scale_to_z_score'):
    # x_mean will be float16, float32, or float64, depending on type of x.
    x_mean, x_var = analyzers._mean_and_var(  # pylint: disable=protected-access
        x, reduce_instance_dims=not elementwise, output_dtype=output_dtype)
    return (tf.cast(x, x_mean.dtype) - x_mean) / tf.sqrt(x_var)
Example #2
0
def scale_to_z_score(x, name=None):
    """Returns a standardized column with mean 0 and variance 1.

  Scaling to z-score subtracts out the mean and divides by standard deviation.
  Note that the standard deviation computed here is based on the biased variance
  (0 delta degrees of freedom), as computed by analyzers.var.

  Args:
    x: A numeric `Tensor`.
    name: (Optional) A name for this operation.

  Returns:
    A `Tensor` containing the input column scaled to mean 0 and variance 1
    (standard deviation 1), given by: (x - mean(x)) / std_dev(x).
    If `x` is floating point, the mean will have the same type as `x`. If `x` is
    integral, the output is cast to float32 for int8 and int16 and float64 for
    int32 and int64 (similar to the behavior of tf.truediv).

    Note that TFLearn generally permits only tf.int64 and tf.float32, so casting
    this scaler's output may be necessary. In particular, scaling an int64
    tensor yields a float64 tensor, which would need a cast to float32 to be
    used in TFLearn.
  """
    with tf.name_scope(name, 'scale_to_z_score'):
        # x_mean will be float32 or float64, depending on type of x.
        x_mean, x_var = analyzers._mean_and_var(x)  # pylint: disable=protected-access
        return (tf.cast(x, x_mean.dtype) - x_mean) / tf.sqrt(x_var)
Example #3
0
def scale_to_z_score(x, elementwise=False, name=None, output_dtype=None):
  """Returns a standardized column with mean 0 and variance 1.

  Scaling to z-score subtracts out the mean and divides by standard deviation.
  Note that the standard deviation computed here is based on the biased variance
  (0 delta degrees of freedom), as computed by analyzers.var.

  Args:
    x: A numeric `Tensor` or `SparseTensor`.
    elementwise: If true, scales each element of the tensor independently;
        otherwise uses the mean and variance of the whole tensor.
    name: (Optional) A name for this operation.
    output_dtype: (Optional) If not None, casts the output tensor to this type.

  Returns:
    A `Tensor` or `SparseTensor` containing the input column scaled to mean 0
    and variance 1 (standard deviation 1), given by: (x - mean(x)) / std_dev(x).
    If `x` is floating point, the mean will have the same type as `x`. If `x` is
    integral, the output is cast to tf.float32.

    Note that TFLearn generally permits only tf.int64 and tf.float32, so casting
    this scaler's output may be necessary.
  """
  with tf.compat.v1.name_scope(name, 'scale_to_z_score'):
    # x_mean will be float16, float32, or float64, depending on type of x.
    x_mean, x_var = analyzers._mean_and_var(  # pylint: disable=protected-access
        x, reduce_instance_dims=not elementwise, output_dtype=output_dtype)
    compose_result_fn = lambda values: values
    x_values = x

    if isinstance(x, tf.SparseTensor):

      x_values = x.values
      compose_result_fn = (lambda values: tf.SparseTensor(  # pylint: disable=g-long-lambda
          indices=x.indices, values=values, dense_shape=x.dense_shape))
      if elementwise:
        # Only supports SparseTensors with rank 2.
        x.get_shape().assert_has_rank(2)

        x_mean = tf.gather(x_mean, x.indices[:, 1])
        x_var = tf.gather(x_var, x.indices[:, 1])

    numerator = tf.cast(x_values, x_mean.dtype) - x_mean
    denominator = tf.sqrt(x_var)
    cond = tf.not_equal(denominator, 0)

    if elementwise and isinstance(x, tf.Tensor):
      # Repeats cond when necessary across the batch dimension for it to be
      # compatible with the shape of numerator.
      cond = tf.cast(
          tf.zeros_like(numerator) + tf.cast(cond, numerator.dtype),
          dtype=tf.bool)

    deviation_values = tf.where(cond, tf.divide(numerator, denominator),
                                numerator)
    return compose_result_fn(deviation_values)