帶有詞匯文件的CategoricalColumn。
用法
tf.feature_column.categorical_column_with_vocabulary_file(
key, vocabulary_file, vocabulary_size=None, dtype=tf.dtypes.string,
default_value=None, num_oov_buckets=0, file_format=None
)參數
-
key標識輸入特征的唯一字符串。它用作特征解析配置、特征Tensor對象和特征列的列名和字典鍵。 -
vocabulary_file詞匯文件名。 -
vocabulary_size詞匯表中元素的數量。這必須不大於vocabulary_file的長度,如果小於長度,則忽略後麵的值。如果為 None,則設置為vocabulary_file的長度。 -
dtype特征的類型。僅支持字符串和整數類型。 -
default_value為詞匯外特征值返回的整數 ID 值,默認為-1。這不能用正的num_oov_buckets來指定。 -
num_oov_buckets非負整數,詞匯表外的桶數。所有超出詞匯表的輸入都將根據輸入值的散列分配[vocabulary_size, vocabulary_size+num_oov_buckets)範圍內的 ID。不能用default_value指定正的num_oov_buckets。 -
file_format詞匯文件的格式。格式默認為'text',除非vocabulary_file是以'tfrecord.gz'結尾的字符串。file_format的可接受替代值為 'tfrecord_gzip'。
返回
-
帶有詞匯文件的
CategoricalColumn。
拋出
-
ValueErrorvocabulary_file丟失或無法打開。 -
ValueErrorvocabulary_size缺失或 -
ValueErrornum_oov_buckets是一個負整數。 -
ValueErrornum_oov_buckets和default_value均已指定。 -
ValueErrordtype既不是字符串也不是整數。
當您的輸入是字符串或整數格式,並且您有一個將每個值映射到整數 ID 的詞匯表文件時,請使用此選項。默認情況下,詞匯表外的值被忽略。使用num_oov_buckets 和default_value 中的任何一個(但不能同時使用)來指定如何包含詞匯表外的值。
對於輸入字典 features , features[key] 是 Tensor 或 SparseTensor 。如果 Tensor ,缺失值可以用 -1 表示 int 和 '' 表示 string,這將被此特征列刪除。
num_oov_buckets 示例:文件 '/us/states.txt' 包含 50 行,每行帶有 2 個字符的美國州縮寫。該文件中具有值的所有輸入都分配了一個 ID 0-49,對應於其行號。所有其他值都經過哈希處理並分配了 ID 50-54。
states = categorical_column_with_vocabulary_file(
key='states', vocabulary_file='/us/states.txt', vocabulary_size=50,
num_oov_buckets=5)
columns = [states, ...]
features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
linear_prediction = linear_model(features, columns)
default_value 示例:文件 '/us/states.txt' 包含 51 行 - 第一行是 'XX' ,其他 50 行各有 2 個字符的美國州縮寫。輸入中的文字 'XX' 和文件中缺少的其他值都將被分配 ID 0。所有其他值都被分配相應的行號 1-50。
states = categorical_column_with_vocabulary_file(
key='states', vocabulary_file='/us/states.txt', vocabulary_size=51,
default_value=0)
columns = [states, ...]
features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
linear_prediction, _, _ = linear_model(features, columns)
並使用以下任一方式進行嵌入:
columns = [embedding_column(states, 3),...]
features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
dense_tensor = input_layer(features, columns)
相關用法
- Python tf.feature_column.categorical_column_with_vocabulary_list用法及代碼示例
- Python tf.feature_column.categorical_column_with_hash_bucket用法及代碼示例
- Python tf.feature_column.categorical_column_with_identity用法及代碼示例
- Python tf.feature_column.crossed_column用法及代碼示例
- Python tf.feature_column.sequence_categorical_column_with_identity用法及代碼示例
- Python tf.feature_column.bucketized_column用法及代碼示例
- Python tf.feature_column.sequence_numeric_column用法及代碼示例
- Python tf.feature_column.sequence_categorical_column_with_vocabulary_file用法及代碼示例
- Python tf.feature_column.sequence_categorical_column_with_vocabulary_list用法及代碼示例
- Python tf.feature_column.sequence_categorical_column_with_hash_bucket用法及代碼示例
- Python tf.feature_column.shared_embeddings用法及代碼示例
- Python tf.feature_column.indicator_column用法及代碼示例
- Python tf.feature_column.weighted_categorical_column用法及代碼示例
- Python tf.feature_column.numeric_column用法及代碼示例
- Python tf.feature_column.embedding_column用法及代碼示例
- Python tf.feature_column.make_parse_example_spec用法及代碼示例
- Python tf.function用法及代碼示例
- Python tf.fingerprint用法及代碼示例
- Python tf.foldl用法及代碼示例
- Python tf.foldr用法及代碼示例
注:本文由純淨天空篩選整理自tensorflow.org大神的英文原創作品 tf.feature_column.categorical_column_with_vocabulary_file。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
