Ejemplo n.º 1
0
def scale_by_min_max(x, output_min=0.0, output_max=1.0, name=None):
    """Scale a numerical column into the range [output_min, output_max].

  Args:
    x: A numeric `Tensor`.
    output_min: The minimum of the range of output values.
    output_max: The maximum of the range of output values.
    name: (Optional) A name for this operation.

  Returns:
    A `Tensor` containing the input column scaled to [output_min, output_max].

  Raises:
    ValueError: If output_min, output_max have the wrong order.
  """
    with tf.name_scope(name, 'scale_by_min_max'):
        if output_min >= output_max:
            raise ValueError('output_min must be less than output_max')

        x = tf.to_float(x)
        min_x_value = analyzers.min(x)
        max_x_value = analyzers.max(x)

        x_shape = tf.shape(x)

        # If min==max, the result will be the mean of the requested range.
        # Note that both the options of tf.where are computed, which means that this
        # will compute unused NaNs.
        scaled_result = tf.where(tf.fill(x_shape, min_x_value < max_x_value),
                                 (x - min_x_value) /
                                 (max_x_value - min_x_value),
                                 tf.fill(x_shape, 0.5))

        return (scaled_result * (output_max - output_min)) + output_min
Ejemplo n.º 2
0
    def testCreatePhasesWithMultipleLevelsOfAnalyzers(self):
        # Create graph similar to calling scale_to_0_1 except involving multiple
        # interleavings of analyzers and transforms.
        float_placeholder = tf.placeholder(tf.float32, shape=(None, ))
        scaled_to_0 = float_placeholder - analyzers.min(float_placeholder)
        scaled_to_0 / analyzers.max(scaled_to_0)  # pylint: disable=expression-not-assigned

        phases = impl_helper.create_phases()
        self.assertEqual(len(phases), 2)
        self.assertEqual(len(phases[0].analyzer_infos), 1)
        self.assertEqual(len(phases[1].analyzer_infos), 1)
Ejemplo n.º 3
0
    def testCreatePhasesWithTable(self):
        # Create a graph with table that can only be run after the first analyzer
        # has run.  Note converting an integerized string into a float doesn't make
        # much sense, but is a legal tensorflow computation.
        string_placeholder = tf.placeholder(tf.string, shape=(None, ))
        integerized = mappers.string_to_int(string_placeholder)
        integerized = tf.to_float(integerized)
        integerized / analyzers.max(integerized)  # pylint: disable=expression-not-assigned

        phases = impl_helper.create_phases()
        self.assertEqual(len(phases), 2)
        self.assertEqual(len(phases[0].analyzer_infos), 1)
        self.assertEqual(len(phases[1].analyzer_infos), 1)
        self.assertEqual(len(phases[0].table_initializers), 0)
        self.assertEqual(len(phases[1].table_initializers), 1)
Ejemplo n.º 4
0
def scale_to_0_1(x):
    """Returns a column which is the input column scaled to have range [0,1].

  Args:
    x: A `Column` representing a numeric value.

  Returns:
    A `Column` representing the input column scaled to [0, 1].
  """

    # A TITO function that scales x.
    def scale(x, min_value, max_value):
        return (x - min_value) / (max_value - min_value)

    return api.map(scale, x, analyzers.min(x), analyzers.max(x))
Ejemplo n.º 5
0
def scale_by_min_max(x, output_min=0.0, output_max=1.0):
    """Scale a numerical column into the range [output_min, output_max].

  Args:
    x: A numeric `Tensor`.
    output_min: The minimum of the range of output values.
    output_max: The maximum of the range of output values.

  Returns:
    A `Tensor` containing the input column scaled to [output_min, output_max].

  Raises:
    ValueError: If output_min, output_max have the wrong order.
  """
    if output_min >= output_max:
        raise ValueError('output_min must be less than output_max')

    min_x_value = analyzers.min(x)
    max_x_value = analyzers.max(x)
    return ((((x - min_x_value) * (output_max - output_min)) /
             (max_x_value - min_x_value)) + output_min)
Ejemplo n.º 6
0
def scale_by_min_max(x, output_min=0.0, output_max=1.0):
    """Scale a numerical column into the range [output_min, output_max].

  Args:
    x: A `Column` representing a numeric value.
    output_min: The minimum of the range of output values.
    output_max: The maximum of the range of output values.

  Returns:
    A `Column` representing the input column scaled to [output_min, output_max].

  Raises:
    ValueError: If output_min, output_max have the wrong order.
  """
    if output_min >= output_max:
        raise ValueError('output_min must be less than output_max')

    # A TITO function that scales x.
    def _scale(x, min_x_value, max_x_value):
        return ((((x - min_x_value) * (output_max - output_min)) /
                 (max_x_value - min_x_value)) + output_min)

    return api.map(_scale, x, analyzers.min(x), analyzers.max(x))
Ejemplo n.º 7
0
 def preprocessing_fn(inputs):
   integerized = mappers.string_to_int(inputs['x'])
   integerized = tf.to_float(integerized)
   scaled_to_0_1 = integerized / analyzers.max(integerized)
   return {'x_scaled': scaled_to_0_1}
Ejemplo n.º 8
0
 def preprocessing_fn(inputs):
   scaled_to_0 = inputs['x'] - analyzers.min(inputs['x'])
   scaled_to_0_1 = scaled_to_0 / analyzers.max(scaled_to_0)
   return {'x_scaled': scaled_to_0_1}