TensorFlow如何生成解析规范

2018-05-17 11:00 更新

tf.estimator.classifier_parse_example_spec

classifier_parse_example_spec(
    feature_columns,
    label_key,
    label_dtype=tf.int64,
    label_default=None,
    weight_column=None
)

定义在:tensorflow/python/estimator/canned/parsing_utils.py.

生成用于分类器的 tf.parse_example 的解析规范.

如果用户将数据保存在 tf.Example 格式中,则需要使用适当的函数参数调用 tf. parse_example.此实用程序有两个主要的帮助:

  • 用户需要将函数的解析规范与标签和权重(如果有的话)相结合,因为它们都是从相同的 tf.Example 实例中解析出来的.该实用程序组合了这些规范.
  • 通过分类器(如 DNNClassifie)将预期标签映射到相应的 tf.parse_example 规范是很困难的.该实用程序通过用户(key,dtype)获取相关信息对其进行编码.

解析规范示例输出:

# Define features and transformations
feature_b = tf.feature_column.numeric_column(...)
feature_c_bucketized = tf.feature_column.bucketized_column(
  tf.feature_column.numeric_column("feature_c"), ...)
feature_a_x_feature_c = tf.feature_column.crossed_column(
    columns=["feature_a", feature_c_bucketized], ...)

feature_columns = [feature_b, feature_c_bucketized, feature_a_x_feature_c]
parsing_spec = tf.estimator.classifier_parse_example_spec(
    feature_columns, label_key='my-label', label_dtype=tf.string)

# For the above example, classifier_parse_example_spec would return the dict:
assert parsing_spec == {
  "feature_a": parsing_ops.VarLenFeature(tf.string),
  "feature_b": parsing_ops.FixedLenFeature([1], dtype=tf.float32),
  "feature_c": parsing_ops.FixedLenFeature([1], dtype=tf.float32)
  "my-label" : parsing_ops.FixedLenFeature([1], dtype=tf.string)
}

分类器使用示例:

feature_columns = # define features via tf.feature_column
estimator = DNNClassifier(
    n_classes=1000,
    feature_columns=feature_columns,
    weight_column='example-weight',
    label_vocabulary=['photos', 'keep', ...],
    hidden_units=[256, 64, 16])
# This label configuration tells the classifier the following:
# * weights are retrieved with key 'example-weight'
# * label is string and can be one of the following ['photos', 'keep', ...]
# * integer id for label 'photos' is 0, 'keep' is 1, ...

# Input builders
def input_fn_train():  # Returns a tuple of features and labels.
  features = tf.contrib.learn.read_keyed_batch_features(
      file_pattern=train_files,
      batch_size=batch_size,
      # creates parsing configuration for tf.parse_example
      features=tf.estimator.classifier_parse_example_spec(
          feature_columns,
          label_key='my-label',
          label_dtype=tf.string,
          weight_column='example-weight'),
      reader=tf.RecordIOReader)
   labels = features.pop('my-label')
   return features, labels

estimator.train(input_fn=input_fn_train)

ARGS:

  • feature_columns:一个包含所有特征列的 iterable.所有项目都应该是从 _FeatureColumn 派生的类的实例.
  • label_key:标识标签的字符串.这意味着 tf.Example 使用这个键存储标签.
  • label_dtype:一个 tf.dtype 标识标签的类型.默认情况下是 tf.int64.如果用户定义了一个 label_vocabulary,则应将其设置为 tf.string.tf.float32 标签仅支持二进制分类.
  • label_default:如果在给定的 tf.Example 中不存在 label_key,则用作标签.一个示例用法:假设 label_key 是 “clicked”,并且 tf.Example 仅包含以下 key 格式的正示例的点击数据:clicked, value:1.这意味着如果没有键 “clicked” 的数据,应该通过设置 label_deafault=0 来计算为负的示例.该值的类型应与 label_dtype 兼容. 
  • weight_column:通过 tf.feature_column.numeric_column 创建的一个字符串或者 _NumericColumn,用来定义表示权重的特征列.在训练过程中,它用于降低权重或增加实例.它将乘以示例的损失.如果它是一个字符串,它被用作 key 并从特征中获取权重张量.如果是 _NumericColumn,则通过键weight_column.key 获取原始张量,然后在其上应用 weight_column.normalizer_fn 以获得权重张量.

返回:

返回一个字典将每个特征键映射到 FixedLenFeature 或 VarLenFeature 值.

注意:

  • ValueError:如果标签中使用 feature_columns.
  • ValueError:如果在 feature_columns 中使用 weight_column .
  • ValueError:如果给定的 feature_columns 不是一个 _FeatureColumn 实例.
  • ValueError:如果 weight_column 不是一个 _NumericColumn 实例.
  • ValueError:如果 label_key 为 None.


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号