带有词汇文件的CategoricalColumn。
用法
tf.compat.v1.feature_column.categorical_column_with_vocabulary_file(
key, vocabulary_file, vocabulary_size=None, num_oov_buckets=0,
default_value=None, dtype=tf.dtypes.string
)参数
-
key标识输入特征的唯一字符串。它用作特征解析配置、特征Tensor对象和特征列的列名和字典键。 -
vocabulary_file词汇文件名。 -
vocabulary_size词汇表中元素的数量。这必须不大于vocabulary_file的长度,如果小于长度,则忽略后面的值。如果为 None,则设置为vocabulary_file的长度。 -
num_oov_buckets非负整数,词汇表外的桶数。所有超出词汇表的输入都将根据输入值的散列分配[vocabulary_size, vocabulary_size+num_oov_buckets)范围内的 ID。不能用default_value指定正的num_oov_buckets。 -
default_value为词汇外特征值返回的整数 ID 值,默认为-1。这不能用正的num_oov_buckets来指定。 -
dtype特征的类型。仅支持字符串和整数类型。
返回
-
带有词汇文件的
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。
import tensorflow as tf
states = tf.feature_column.categorical_column_with_vocabulary_file(
key='states', vocabulary_file='states.txt', vocabulary_size=5,
num_oov_buckets=1)
columns = [states]
features = {'states':tf.constant([['california', 'georgia', 'michigan',
'texas', 'new york'], ['new york', 'georgia', 'california', 'michigan',
'texas']])}
linear_prediction = tf.compat.v1.feature_column.linear_model(features,
columns)
default_value 示例:文件 '/us/states.txt' 包含 51 行 - 第一行是 'XX',其他 50 行各有 2 个字符的美国州缩写。输入中的文字 'XX' 和文件中缺少的其他值都将被分配 ID 0。所有其他值都被分配相应的行号 1-50。
import tensorflow as tf
states = tf.feature_column.categorical_column_with_vocabulary_file(
key='states', vocabulary_file='states.txt', vocabulary_size=6,
default_value=0)
columns = [states]
features = {'states':tf.constant([['california', 'georgia', 'michigan',
'texas', 'new york'], ['new york', 'georgia', 'california', 'michigan',
'texas']])}
linear_prediction = tf.compat.v1.feature_column.linear_model(features,
columns)
并使用以下任一方式进行嵌入:
import tensorflow as tf
states = tf.feature_column.categorical_column_with_vocabulary_file(
key='states', vocabulary_file='states.txt', vocabulary_size=5,
num_oov_buckets=1)
columns = [tf.feature_column.embedding_column(states, 3)]
features = {'states':tf.constant([['california', 'georgia', 'michigan',
'texas', 'new york'], ['new york', 'georgia', 'california', 'michigan',
'texas']])}
input_layer = tf.keras.layers.DenseFeatures(columns)
dense_tensor = input_layer(features)
相关用法
- Python tf.compat.v1.feature_column.make_parse_example_spec用法及代码示例
- Python tf.compat.v1.feature_column.linear_model用法及代码示例
- Python tf.compat.v1.feature_column.shared_embedding_columns用法及代码示例
- Python tf.compat.v1.feature_column.input_layer用法及代码示例
- Python tf.compat.v1.foldr用法及代码示例
- Python tf.compat.v1.fixed_size_partitioner用法及代码示例
- Python tf.compat.v1.foldl用法及代码示例
- Python tf.compat.v1.flags.BaseListParser用法及代码示例
- Python tf.compat.v1.flags.FlagHolder用法及代码示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代码示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代码示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代码示例
- Python tf.compat.v1.distributions.Bernoulli.cross_entropy用法及代码示例
- Python tf.compat.v1.Variable.eval用法及代码示例
- Python tf.compat.v1.train.FtrlOptimizer.compute_gradients用法及代码示例
- Python tf.compat.v1.layers.conv3d用法及代码示例
- Python tf.compat.v1.strings.length用法及代码示例
- Python tf.compat.v1.data.Dataset.snapshot用法及代码示例
- Python tf.compat.v1.data.experimental.SqlDataset.reduce用法及代码示例
- Python tf.compat.v1.data.TextLineDataset.from_tensors用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.feature_column.categorical_column_with_vocabulary_file。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
