本文整理汇总了Python中tensorflow_transform.TFTransformOutput方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow_transform.TFTransformOutput方法的具体用法?Python tensorflow_transform.TFTransformOutput怎么用?Python tensorflow_transform.TFTransformOutput使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow_transform
的用法示例。
在下文中一共展示了tensorflow_transform.TFTransformOutput方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_feature_columns
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def get_feature_columns(tf_transform_output):
"""Returns the FeatureColumns for the model.
Args:
tf_transform_output: A `TFTransformOutput` object.
Returns:
A list of FeatureColumns.
"""
# Wrap scalars as real valued columns.
real_valued_columns = [tf.feature_column.numeric_column(key, shape=())
for key in NUMERIC_FEATURE_KEYS]
# Wrap categorical columns.
one_hot_columns = [
tf.feature_column.indicator_column( # pylint: disable=g-complex-comprehension
tf.feature_column.categorical_column_with_vocabulary_file(
key=key,
vocabulary_file=tf_transform_output.vocabulary_file_by_name(
vocab_filename=key)))
for key in CATEGORICAL_FEATURE_KEYS]
return real_valued_columns + one_hot_columns
示例2: get_feature_columns
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def get_feature_columns(tf_transform_output):
"""Returns the FeatureColumns for the model.
Args:
tf_transform_output: A `TFTransformOutput` object.
Returns:
A list of FeatureColumns.
"""
del tf_transform_output # unused
# Unrecognized tokens are represented by -1, but
# categorical_column_with_identity uses the mod operator to map integers
# to the range [0, bucket_size). By choosing bucket_size=VOCAB_SIZE + 1, we
# represent unrecognized tokens as VOCAB_SIZE.
review_column = tf.feature_column.categorical_column_with_identity(
REVIEW_KEY, num_buckets=VOCAB_SIZE + 1)
weighted_reviews = tf.feature_column.weighted_categorical_column(
review_column, REVIEW_WEIGHT_KEY)
return [weighted_reviews]
示例3: testReadTransformFn
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def testReadTransformFn(self):
path = self.get_temp_dir()
# NOTE: we don't need to create or write to the transform_fn directory since
# ReadTransformFn never inspects this directory.
transform_fn_dir = os.path.join(
path, tft.TFTransformOutput.TRANSFORM_FN_DIR)
transformed_metadata_dir = os.path.join(
path, tft.TFTransformOutput.TRANSFORMED_METADATA_DIR)
metadata_io.write_metadata(test_metadata.COMPLETE_METADATA,
transformed_metadata_dir)
with beam.Pipeline() as pipeline:
saved_model_dir_pcoll, metadata = (
pipeline | transform_fn_io.ReadTransformFn(path))
beam_test_util.assert_that(
saved_model_dir_pcoll,
beam_test_util.equal_to([transform_fn_dir]),
label='AssertSavedModelDir')
# NOTE: metadata is currently read in a non-deferred manner.
self.assertEqual(metadata, test_metadata.COMPLETE_METADATA)
示例4: _example_serving_receiver_fn
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def _example_serving_receiver_fn(tf_transform_output, schema):
"""Build the serving in inputs.
Args:
tf_transform_output: A TFTransformOutput.
schema: the schema of the input data.
Returns:
Tensorflow graph which parses examples, applying tf-transform to them.
"""
raw_feature_spec = _get_raw_feature_spec(schema)
raw_feature_spec.pop(_LABEL_KEY)
raw_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
raw_feature_spec, default_batch_size=None)
serving_input_receiver = raw_input_fn()
transformed_features = tf_transform_output.transform_raw_features(
serving_input_receiver.features)
return tf.estimator.export.ServingInputReceiver(
transformed_features, serving_input_receiver.receiver_tensors)
示例5: _flat_input_serving_receiver_fn
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def _flat_input_serving_receiver_fn(tf_transform_output, schema):
"""Build the serving function for flat list of Dense tensors as input.
Args:
tf_transform_output: A TFTransformOutput.
schema: the schema of the input data.
Returns:
Tensorflow graph which parses examples, applying tf-transform to them.
"""
raw_feature_spec = _get_raw_feature_spec(schema)
raw_feature_spec.pop(_LABEL_KEY)
raw_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
raw_feature_spec, default_batch_size=None)
serving_input_receiver = raw_input_fn()
transformed_features = tf_transform_output.transform_raw_features(
serving_input_receiver.features)
# We construct a receiver function that receives flat list of Dense tensors as
# features. This is as per BigQuery ML serving requirements.
return tf.estimator.export.ServingInputReceiver(
transformed_features, serving_input_receiver.features)
示例6: _input_fn
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def _input_fn(file_pattern, tf_transform_output, batch_size=200):
"""Generates features and label for tuning/training.
Args:
file_pattern: input tfrecord file pattern.
tf_transform_output: A TFTransformOutput.
batch_size: representing the number of consecutive elements of returned
dataset to combine in a single batch
Returns:
A dataset that contains (features, indices) tuple where features is a
dictionary of Tensors, and indices is a single Tensor of label indices.
"""
transformed_feature_spec = (
tf_transform_output.transformed_feature_spec().copy())
dataset = tf.data.experimental.make_batched_features_dataset(
file_pattern=file_pattern,
batch_size=batch_size,
features=transformed_feature_spec,
reader=_gzip_reader_fn,
label_key=features.transformed_name(features.LABEL_KEY))
return dataset
示例7: _example_serving_receiver_fn
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def _example_serving_receiver_fn(tf_transform_output, schema):
"""Build the serving in inputs.
Args:
tf_transform_output: A TFTransformOutput.
schema: the schema of the input data.
Returns:
Tensorflow graph which parses examples, applying tf-transform to them.
"""
raw_feature_spec = _get_raw_feature_spec(schema)
raw_feature_spec.pop(features.LABEL_KEY)
raw_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(
raw_feature_spec, default_batch_size=None)
serving_input_receiver = raw_input_fn()
transformed_features = tf_transform_output.transform_raw_features(
serving_input_receiver.features)
return tf.estimator.export.ServingInputReceiver(
transformed_features, serving_input_receiver.receiver_tensors)
示例8: _input_fn
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def _input_fn(filenames, tf_transform_output, batch_size=200):
"""Generates features and labels for training or evaluation.
Args:
filenames: [str] list of CSV files to read data from.
tf_transform_output: A TFTransformOutput.
batch_size: int First dimension size of the Tensors returned by input_fn
Returns:
A (features, indices) tuple where features is a dictionary of
Tensors, and indices is a single Tensor of label indices.
"""
transformed_feature_spec = (
tf_transform_output.transformed_feature_spec().copy())
dataset = tf.data.experimental.make_batched_features_dataset(
filenames, batch_size, transformed_feature_spec, reader=_gzip_reader_fn)
transformed_features = tf.compat.v1.data.make_one_shot_iterator(
dataset).get_next()
# We pop the label because we do not want to use it as a feature while we're
# training.
return transformed_features, transformed_features.pop(
features.transformed_name(features.LABEL_KEY))
示例9: _sample_vocab
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def _sample_vocab(tft_output, vocab_name, label, k):
"""Samples the given vocab and returns the indices and samples.
Args:
tft_output: a TFTransformOutput object.
vocab_name: the name of the embedding vocabulary made with tft.
label: a label to assign each sample of the vocab.
k: the maximum number of samples to take.
Returns:
A tuple of (indices, metadata):
indices: a list of indices for the vocab sample.
metadata: a list of lists of data corresponding to the indices.
"""
vocab = tft_output.vocabulary_by_name(vocab_name)
num_indices = min(k, len(vocab))
indices = random.sample(range(len(vocab)), num_indices)
return indices, [[label, vocab[i]] for i in indices]
示例10: _make_embedding_col
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def _make_embedding_col(feature_name, vocab_name, tft_output, mult=1):
"""Creates an embedding column.
Args:
feature_name: a attribute of features to get embedding vectors for.
vocab_name: the name of the embedding vocabulary made with tft.
tft_output: a TFTransformOutput object.
mult: a multiplier on the embedding size.
Returns:
A tuple of (embedding_col, embedding_size):
embedding_col: an n x d tensor, where n is the batch size and d is the
length of all the features concatenated together.
embedding_size: the embedding dimension.
"""
vocab_size = tft_output.vocabulary_size_by_name(vocab_name)
embedding_size = int(_default_embedding_size(vocab_size) * mult)
cat_col = tf.feature_column.categorical_column_with_identity(
key=feature_name, num_buckets=vocab_size + 1, default_value=vocab_size)
embedding_col = tf.feature_column.embedding_column(cat_col, embedding_size)
return embedding_col, embedding_size
示例11: testWriteTransformFn
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def testWriteTransformFn(self):
transform_output_dir = os.path.join(self.get_temp_dir(), 'output')
with beam.Pipeline() as pipeline:
# Create an empty directory for the source saved model dir.
saved_model_dir = os.path.join(self.get_temp_dir(), 'source')
file_io.recursive_create_dir(saved_model_dir)
saved_model_dir_pcoll = (
pipeline | 'CreateSavedModelDir' >> beam.Create([saved_model_dir]))
# Combine test metadata with a dict of PCollections resolving futures.
deferred_metadata = pipeline | 'CreateDeferredMetadata' >> beam.Create(
[test_metadata.COMPLETE_METADATA])
metadata = beam_metadata_io.BeamDatasetMetadata(
test_metadata.INCOMPLETE_METADATA, deferred_metadata)
_ = ((saved_model_dir_pcoll, metadata)
| transform_fn_io.WriteTransformFn(transform_output_dir))
# Test reading with TFTransformOutput
tf_transform_output = tft.TFTransformOutput(transform_output_dir)
metadata = tf_transform_output.transformed_metadata
self.assertEqual(metadata, test_metadata.COMPLETE_METADATA)
transform_fn_dir = tf_transform_output.transform_savedmodel_dir
self.assertTrue(file_io.file_exists(transform_fn_dir))
self.assertTrue(file_io.is_directory(transform_fn_dir))
示例12: _eval_input_receiver_fn
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def _eval_input_receiver_fn(tf_transform_output, schema):
"""Build everything needed for the tf-model-analysis to run the model.
Args:
tf_transform_output: A TFTransformOutput.
schema: the schema of the input data.
Returns:
EvalInputReceiver function, which contains:
- Tensorflow graph which parses raw untransformed features, applies the
tf-transform preprocessing operators.
- Set of raw, untransformed features.
- Label against which predictions will be compared.
"""
# Notice that the inputs are raw features, not transformed features here.
raw_feature_spec = _get_raw_feature_spec(schema)
serialized_tf_example = tf.placeholder(
dtype=tf.string, shape=[None], name='input_example_tensor')
# Add a parse_example operator to the tensorflow graph, which will parse
# raw, untransformed, tf examples.
features = tf.parse_example(serialized_tf_example, raw_feature_spec)
# Now that we have our raw examples, process them through the tf-transform
# function computed during the preprocessing step.
transformed_features = tf_transform_output.transform_raw_features(
features)
# The key name MUST be 'examples'.
receiver_tensors = {'examples': serialized_tf_example}
# NOTE: Model is driven by transformed features (since training works on the
# materialized output of TFT, but slicing will happen on raw features.
features.update(transformed_features)
return tfma.export.EvalInputReceiver(
features=features,
receiver_tensors=receiver_tensors,
labels=transformed_features[_transformed_name(_LABEL_KEY)])
示例13: _input_fn
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def _input_fn(filenames, tf_transform_output, batch_size=200):
"""Generates features and labels for training or evaluation.
Args:
filenames: [str] list of CSV files to read data from.
tf_transform_output: A TFTransformOutput.
batch_size: int First dimension size of the Tensors returned by input_fn
Returns:
A (features, indices) tuple where features is a dictionary of
Tensors, and indices is a single Tensor of label indices.
"""
transformed_feature_spec = (
tf_transform_output.transformed_feature_spec().copy())
dataset = tf.data.experimental.make_batched_features_dataset(
filenames, batch_size, transformed_feature_spec, reader=_gzip_reader_fn)
transformed_features = dataset.make_one_shot_iterator().get_next()
# We pop the label because we do not want to use it as a feature while we're
# training.
return transformed_features, transformed_features.pop(
_transformed_name(_LABEL_KEY))
# TFX will call this function
示例14: _input_fn
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def _input_fn(file_pattern: Text, tf_transform_output: tft.TFTransformOutput,
) -> Tuple[np.ndarray, np.ndarray]:
"""Generates features and label for tuning/training.
Args:
file_pattern: input tfrecord file pattern.
tf_transform_output: A TFTransformOutput.
Returns:
A (features, indices) tuple where features is a matrix of features, and
indices is a single vector of label indices.
"""
def _parse_example(example):
"""Parses a tfrecord into a (features, indices) tuple of Tensors."""
parsed_example = tf.io.parse_single_example(
serialized=example,
features=tf_transform_output.transformed_feature_spec())
label = parsed_example.pop(_transformed_name(_LABEL_KEY))
return parsed_example, label
filenames = tf.data.Dataset.list_files(file_pattern)
dataset = tf.data.TFRecordDataset(filenames, compression_type='GZIP')
# TODO(b/157598676): Make AUTOTUNE the default.
dataset = dataset.map(
_parse_example,
num_parallel_calls=tf.data.experimental.AUTOTUNE)
dataset = dataset.shuffle(_SHUFFLE_BUFFER)
return _tf_dataset_to_numpy(dataset)
# TFX Transform will call this function.
示例15: run_fn
# 需要导入模块: import tensorflow_transform [as 别名]
# 或者: from tensorflow_transform import TFTransformOutput [as 别名]
def run_fn(fn_args: TrainerFnArgs):
"""Train the model based on given args.
Args:
fn_args: Holds args used to train the model as name/value pairs.
"""
tf_transform_output = tft.TFTransformOutput(fn_args.transform_output)
x_train, y_train = _input_fn(fn_args.train_files, tf_transform_output)
x_eval, y_eval = _input_fn(fn_args.eval_files, tf_transform_output)
steps_per_epoch = _TRAIN_DATA_SIZE / _TRAIN_BATCH_SIZE
model = MLPClassifier(
hidden_layer_sizes=[8, 8, 8],
activation='relu',
solver='adam',
batch_size=_TRAIN_BATCH_SIZE,
learning_rate_init=0.0005,
max_iter=int(fn_args.train_steps / steps_per_epoch),
verbose=True)
model.fit(x_train, y_train)
absl.logging.info(model)
score = model.score(x_eval, y_eval)
absl.logging.info('Accuracy: %f', score)
os.makedirs(fn_args.serving_model_dir)
# TODO(humichael): Export TFT graph for serving once a solution for serving is
# determined.
model_path = os.path.join(fn_args.serving_model_dir, 'model.joblib')
with tf.io.gfile.GFile(model_path, 'wb+') as f:
joblib.dump(model, f)