带有词汇文件的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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
