def _dummy_export_outputs(export_output_key, logits, predictions): """Returns a dummy export output dictionary for the given key.""" export_outputs = None if export_output_key == ExportOutputKeys.CLASSIFICATION_CLASSES: export_outputs = { export_output_key: tf.estimator.export.ClassificationOutput( classes=tf.as_string(logits)) } elif export_output_key == ExportOutputKeys.CLASSIFICATION_SCORES: export_outputs = { export_output_key: tf.estimator.export.ClassificationOutput(scores=logits) } elif export_output_key == ExportOutputKeys.REGRESSION: export_outputs = { export_output_key: tf.estimator.export.RegressionOutput(value=logits) } elif export_output_key == ExportOutputKeys.PREDICTION: export_outputs = { export_output_key: tf.estimator.export.PredictOutput(outputs=predictions) } elif export_output_key == ExportOutputKeys.INVALID: export_outputs = {export_output_key: predictions} return export_outputs
def _hash_values_to_bins(self, values): """Converts a non-sparse tensor of values to bin indices.""" hash_bins = self.num_bins mask = None # If mask_value is set, the zeroth bin is reserved for it. if self.mask_value is not None and hash_bins > 1: hash_bins -= 1 mask = tf.equal(values, self.mask_value) # Convert all values to strings before hashing. if values.dtype.is_integer: values = tf.as_string(values) # Hash the strings. if self.strong_hash: values = tf.strings.to_hash_bucket_strong(values, hash_bins, name='hash', key=self.salt) else: values = tf.strings.to_hash_bucket_fast(values, hash_bins, name='hash') if mask is not None: values = tf.add(values, tf.ones_like(values)) values = tf.where(mask, tf.zeros_like(values), values) return values
def apply_randomization(features, label, randomize_prob): """Randomize each categorical feature with some probability.""" rnd_tok = lambda: tf.as_string(tf.random.uniform([], 0, 99999999, tf.int32)) for idx in CAT_FEATURE_INDICES: key = feature_name(idx) # Ignore lint since tf.cond should evaluate lambda immediately. features[key] = tf.cond(tf.random.uniform([]) < randomize_prob, rnd_tok, lambda: features[key]) # pylint: disable=cell-var-from-loop return features, label
def _hash_values_to_bins(self, values): """Converts a non-sparse tensor of values to bin indices.""" str_to_hash_bucket = self._get_string_to_hash_bucket_fn() num_available_bins = self.num_bins mask = None # If mask_value is set, the zeroth bin is reserved for it. if self.mask_value is not None and num_available_bins > 1: num_available_bins -= 1 mask = tf.equal(values, self.mask_value) # Convert all values to strings before hashing. if values.dtype.is_integer: values = tf.as_string(values) values = str_to_hash_bucket(values, num_available_bins, name='hash') if mask is not None: values = tf.add(values, tf.compat.v1.ones_like(values)) values = tf.compat.v1.where(mask, tf.compat.v1.zeros_like(values), values) return values
def rnd_tok(): return tf.as_string( tf.random.uniform(tf.shape(features[key]), 0, 99999999, tf.int32)) # pylint: disable=cell-var-from-loop
def gather(self, string_values, indices): tf.print(tf.gather(tf.as_string(string_values), indices))
def rnd_tok(): return tf.as_string( tf.random.uniform(tf.shape(features[key]), 0, 99999999, tf.int32))