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
评论列表
文章目录