帶有in-memory 詞匯的CategoricalColumn。
用法
tf.feature_column.categorical_column_with_vocabulary_list(
key, vocabulary_list, dtype=None, default_value=-1, num_oov_buckets=0
)參數
-
key標識輸入特征的唯一字符串。它用作特征解析配置、特征Tensor對象和特征列的列名和字典鍵。 -
vocabulary_list定義詞匯表的有序迭代。每個特征都映射到vocabulary_list中其值的索引(如果存在)。必須可轉換為dtype。 -
dtype特征的類型。僅支持字符串和整數類型。如果None,將從vocabulary_list推斷。 -
default_value為詞匯外特征值返回的整數 ID 值,默認為-1。這不能用正的num_oov_buckets來指定。 -
num_oov_buckets非負整數,詞匯表外的桶數。所有超出詞匯表的輸入都將根據輸入值的散列分配[len(vocabulary_list), len(vocabulary_list)+num_oov_buckets)範圍內的 ID。不能用default_value指定正的num_oov_buckets。
返回
-
帶有in-memory 詞匯的
CategoricalColumn。
拋出
-
ValueError如果vocabulary_list為空,或包含重複鍵。 -
ValueErrornum_oov_buckets是一個負整數。 -
ValueErrornum_oov_buckets和default_value均已指定。 -
ValueError如果dtype不是整數或字符串。
當您的輸入是字符串或整數格式,並且您有一個 in-memory 詞匯表將每個值映射到一個整數 ID 時,請使用此選項。默認情況下,詞匯表外的值被忽略。使用num_oov_buckets 和default_value 中的任何一個(但不能同時使用)來指定如何包含詞匯表外的值。
對於輸入字典 features , features[key] 是 Tensor 或 SparseTensor 。如果 Tensor ,缺失值可以用 -1 表示 int 和 '' 表示 string,這將被此特征列刪除。
num_oov_buckets 示例:在以下示例中,vocabulary_list 中的每個輸入都分配了一個與其索引對應的 ID 0-3(例如,輸入 'B' 產生輸出 2)。所有其他輸入都經過哈希處理並分配了 ID 4-5。
colors = categorical_column_with_vocabulary_list(
key='colors', vocabulary_list=('R', 'G', 'B', 'Y'),
num_oov_buckets=2)
columns = [colors, ...]
features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
linear_prediction, _, _ = linear_model(features, columns)
default_value 示例:在以下示例中,vocabulary_list 中的每個輸入都分配了一個與其索引對應的 ID 0-4(例如,輸入 'B' 產生輸出 3)。所有其他輸入都分配給default_value 0。
colors = categorical_column_with_vocabulary_list(
key='colors', vocabulary_list=('X', 'R', 'G', 'B', 'Y'), default_value=0)
columns = [colors, ...]
features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
linear_prediction, _, _ = linear_model(features, columns)
並使用以下任一方式進行嵌入:
columns = [embedding_column(colors, 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_file用法及代碼示例
- 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_list。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
