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
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)
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))
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)
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))
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}
def preprocessing_fn(inputs): return { 'z': api.map(lambda x, y: x * y + x + y, inputs['x'], analyzers.min(inputs['y'])) }