TensorFlow函數(shù):tf.estimator.regressor_parse_example_spec

2018-05-07 11:24 更新

tf.estimator.regressor_parse_example_spec函數(shù)

tf.estimator.regressor_parse_example_spec(
    feature_columns,
    label_key,
    label_dtype=tf.float32,
    label_default=None,
    label_dimension=1,
    weight_column=None
)

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

為tf.parse_example生成解析規(guī)范以以便與回歸器(regressor)一起使用.

如果用戶將數(shù)據(jù)保存為tf.Example格式,則需要使用正確的功能規(guī)格調(diào)用tf.parse_example.這個(gè)工具有兩個(gè)主要的功能:

  • 用戶需要將功能的分析規(guī)范與標(biāo)簽和權(quán)重(如果有的話)結(jié)合起來(lái),因?yàn)樗鼈兌际菑南嗤膖f.Example實(shí)例中解析的.該實(shí)用程序結(jié)合了這些規(guī)格.
  • 要將回歸器(例如,DNNRegressor)中期望的標(biāo)簽映射到相應(yīng)的tf.parse_example規(guī)范是困難的.此實(shí)用程序通過(guò)從users (key, dtype)獲取相關(guān)信息對(duì)其進(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.regressor_parse_example_spec(
    feature_columns, label_key='my-label')

# For the above example, regressor_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.float32)
}

使用回歸器的示例用法:

feature_columns = # define features via tf.feature_column
estimator = DNNRegressor(
    hidden_units=[256, 64, 16],
    feature_columns=feature_columns,
    weight_column='example-weight',
    label_dimension=3)
# This label configuration tells the regressor the following:
# * weights are retrieved with key 'example-weight'
# * label is a 3 dimension tensor with float32 dtype.

# 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_dimension=3,
          weight_column='example-weight'),
      reader=tf.RecordIOReader)
   labels = features.pop('my-label')
   return features, labels

estimator.train(input_fn=input_fn_train)

函數(shù)參數(shù):

  • feature_columns:包含所有功能列的iterable,所有項(xiàng)目都應(yīng)該是從_FeatureColumn派生的類(lèi)的實(shí)例.
  • label_key:標(biāo)識(shí)標(biāo)簽的字符串.這意味著tf.Example使用此鍵存儲(chǔ)標(biāo)簽.
  • label_dtype:一個(gè)tf.dtype標(biāo)識(shí)標(biāo)簽的類(lèi)型.默認(rèn)情況下是tf.float32.
  • label_default:如果label_key不存在于給定的tf.Example中,則用作標(biāo)簽;默認(rèn)情況下,DEFAULT_VALUE為None,意味著tf.parse_example如果有任何缺少的標(biāo)簽,則將會(huì)出錯(cuò).
  • label_dimension:示例每個(gè)的回歸目標(biāo)數(shù)量;這是標(biāo)簽和logits張量對(duì)象的最后一個(gè)維度的大小(通常,這些對(duì)象具有形狀[batch_size, label_dimension]).
  • weight_column:通過(guò)tf.feature_column.numeric_column定義表示權(quán)重的特征列創(chuàng)建的字符串或_NumericColumn.它用于在訓(xùn)練過(guò)程中減輕權(quán)重或增強(qiáng)示例;它將乘以示例的損失.如果它是一個(gè)字符串,則它將被用作一個(gè)從features中獲取權(quán)重張量的關(guān)鍵字.如果它是a _NumericColumn,則原始張量由鍵weight_column.key提取,然后weight_column.normalizer_fn應(yīng)用于其上以獲得權(quán)重張量.

函數(shù)返回值:

tf.estimator.regressor_parse_example_spec函數(shù)返回一個(gè)字典將每個(gè)功能鍵映射到FixedLenFeature或VarLenFeature值.

可能引發(fā)的異常:

  • ValueError:如果使用標(biāo)簽feature_columns.
  • ValueError:如果使用weight_column feature_columns.
  • ValueError:如果給定的任何feature_columns不是_FeatureColumn實(shí)例.
  • ValueError:如果weight_column不是一個(gè)_NumericColumn實(shí)例.
  • ValueError:如果label_key是None.
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)