TensorFlow如何生成解析規(guī)范

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 的解析規(guī)范.

如果用戶將數(shù)據(jù)保存在 tf.Example 格式中,則需要使用適當(dāng)?shù)暮瘮?shù)參數(shù)調(diào)用 tf. parse_example.此實用程序有兩個主要的幫助:

  • 用戶需要將函數(shù)的解析規(guī)范與標(biāo)簽和權(quán)重(如果有的話)相結(jié)合,因為它們都是從相同的 tf.Example 實例中解析出來的.該實用程序組合了這些規(guī)范.
  • 通過分類器(如 DNNClassifie)將預(yù)期標(biāo)簽映射到相應(yīng)的 tf.parse_example 規(guī)范是很困難的.該實用程序通過用戶(key,dtype)獲取相關(guān)信息對其進(jìn)行編碼.

解析規(guī)范示例輸出:

# 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.所有項目都應(yīng)該是從 _FeatureColumn 派生的類的實例.
  • label_key:標(biāo)識標(biāo)簽的字符串.這意味著 tf.Example 使用這個鍵存儲標(biāo)簽.
  • label_dtype:一個 tf.dtype 標(biāo)識標(biāo)簽的類型.默認(rèn)情況下是 tf.int64.如果用戶定義了一個 label_vocabulary,則應(yīng)將其設(shè)置為 tf.string.tf.float32 標(biāo)簽僅支持二進(jìn)制分類.
  • label_default:如果在給定的 tf.Example 中不存在 label_key,則用作標(biāo)簽.一個示例用法:假設(shè) label_key 是 “clicked”,并且 tf.Example 僅包含以下 key 格式的正示例的點擊數(shù)據(jù):clicked, value:1.這意味著如果沒有鍵 “clicked” 的數(shù)據(jù),應(yīng)該通過設(shè)置 label_deafault=0 來計算為負(fù)的示例.該值的類型應(yīng)與 label_dtype 兼容. 
  • weight_column:通過 tf.feature_column.numeric_column 創(chuàng)建的一個字符串或者 _NumericColumn,用來定義表示權(quán)重的特征列.在訓(xùn)練過程中,它用于降低權(quán)重或增加實例.它將乘以示例的損失.如果它是一個字符串,它被用作 key 并從特征中獲取權(quán)重張量.如果是 _NumericColumn,則通過鍵weight_column.key 獲取原始張量,然后在其上應(yīng)用 weight_column.normalizer_fn 以獲得權(quán)重張量.

返回:

返回一個字典將每個特征鍵映射到 FixedLenFeature 或 VarLenFeature 值.

注意:

  • ValueError:如果標(biāo)簽中使用 feature_columns.
  • ValueError:如果在 feature_columns 中使用 weight_column .
  • ValueError:如果給定的 feature_columns 不是一個 _FeatureColumn 實例.
  • ValueError:如果 weight_column 不是一個 _NumericColumn 實例.
  • ValueError:如果 label_key 為 None.


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號