def json_serving_input_fn(): feature_columns = featurizer.create_feature_columns() input_feature_columns = [ feature_columns[feature_name] for feature_name in metadata.FEATURE_NAMES ] inputs = {} for column in input_feature_columns: inputs[column.name] = tf.placeholder(shape=[None], dtype=column.dtype) features = { key: tf.expand_dims(tensor, -1) for key, tensor in inputs.items() } if metadata.TASK_TYPE == "custom": return tf.estimator.export.ServingInputReceiver( features=preprocess.process_features(features), receiver_tensors=inputs) return tf.contrib.learn.InputFnOps(preprocess.process_features(features), None, inputs)
def example_serving_input_fn(): feature_columns = featurizer.create_feature_columns() input_feature_columns = [ feature_columns[feature_name] for feature_name in metadata.FEATURE_NAMES ] example_bytestring = tf.placeholder( shape=[None], dtype=tf.string, ) feature_scalars = tf.parse_example( example_bytestring, tf.feature_column.make_parse_example_spec(input_feature_columns)) features = { key: tf.expand_dims(tensor, -1) for key, tensor in feature_scalars.iteritems() } if metadata.TASK_TYPE == "custom": return tf.estimator.export.ServingInputReceiver( features=preprocess.process_features(features), receiver_tensors={'example_proto': example_bytestring}) return tf.contrib.learn.InputFnOps( preprocess.process_features(features), None, # labels {'example_proto': example_bytestring})
def csv_serving_input_fn(): csv_row = tf.placeholder(shape=[None], dtype=tf.string) features = parsers.parse_csv(csv_row) features.pop(metadata.TARGET_NAME) if metadata.TASK_TYPE == "custom": return tf.estimator.export.ServingInputReceiver( features=preprocess.process_features(features), receiver_tensors={'csv_row': csv_row}) return tf.contrib.learn.InputFnOps(preprocess.process_features(features), None, {'csv_row': csv_row})
def generate_text_input_fn(file_names, mode, parser_fn=parsers.parse_csv, skip_header_lines=0, num_epochs=None, batch_size=200 ): """Generates an input function for training or evaluation. This uses the input pipeline based approach using file name queue to read data so that entire data is not loaded in memory. Args: file_names: [str] - list of text files to read data from. mode: tf.contrib.learn.ModeKeys - either TRAIN or EVAL. Used to determine whether or not to randomize the order of data. parser_fn: A function that parses text files (e.g., csv parser, fixed-width parser, etc. skip_header_lines: int set to non-zero in order to skip header lines in CSV files. num_epochs: int - how many times through to read the data. If None will loop through data indefinitely batch_size: int - first dimension size of the Tensors returned by input_fn Returns: A function () -> (features, indices) where features is a dictionary of Tensors, and indices is a single Tensor of label indices. """ shuffle = mode == tf.contrib.learn.ModeKeys.TRAIN filename_queue = tf.train.string_input_producer( file_names, num_epochs=num_epochs, shuffle=shuffle) reader = tf.TextLineReader(skip_header_lines=skip_header_lines) _, rows = reader.read_up_to(filename_queue, num_records=batch_size) features = parser_fn(rows) if shuffle: features = tf.train.shuffle_batch( features, batch_size, min_after_dequeue=2 * batch_size + 1, capacity=batch_size * 10, num_threads=multiprocessing.cpu_count(), enqueue_many=True, allow_smaller_final_batch=True ) else: features = tf.train.batch( features, batch_size, capacity=batch_size * 10, num_threads=multiprocessing.cpu_count(), enqueue_many=True, allow_smaller_final_batch=True ) target = get_target(features.pop(metadata.TARGET_NAME)) return preprocess.process_features(features), target
def csv_serving_input_fn(): csv_row = tf.placeholder(shape=[None], dtype=tf.string) features = parsers.parse_csv(csv_row) features.pop(metadata.TARGET_NAME) return tf.contrib.learn.InputFnOps(preprocess.process_features(features), None, {'csv_row': csv_row})
def json_serving_input_fn(): feature_columns = featurizer.create_feature_columns() input_feature_columns = [ feature_columns[feature_name] for feature_name in metadata.FEATURE_NAMES ] inputs = {} for column in input_feature_columns: inputs[column.name] = tf.placeholder(shape=[None], dtype=column.dtype) features = { key: tf.expand_dims(tensor, -1) for key, tensor in inputs.items() } return tf.contrib.learn.InputFnOps(preprocess.process_features(features), None, inputs)