根据给定的 feature_columns 返回线性预测 Tensor 。
用法
tf.compat.v1.feature_column.linear_model(
features, feature_columns, units=1, sparse_combiner='sum',
weight_collections=None, trainable=True, cols_to_vars=None
)参数
-
features从键到张量的映射。_FeatureColumn通过这些键查找。例如numeric_column('price')将查看此字典中的 'price' 键。值是Tensor或SparseTensor取决于相应的_FeatureColumn。 -
feature_columns一个包含要用作模型输入的 FeatureColumns 的迭代。所有项目都应该是派生自_FeatureColumn的类的实例。 -
units一个整数,输出空间的维度。默认值为 1。 -
sparse_combiner如果分类列是多价的,则指定如何减少的字符串。除了numeric_column, 几乎所有的列都传递给linear_model被视为分类列。它独立地组合每个分类列。目前支持"mean"、"sqrtn"和"sum",其中"sum"是线性模型的默认值。 "sqrtn" 通常可以达到很好的准确性,特别是对于 bag-of-words 列。- "sum":不对列中的特征进行归一化
- "mean":对列中的特征做 l1 归一化
- "sqrtn":对列中的特征做l2归一化
-
weight_collections将添加变量的集合名称列表。请注意,变量也将添加到集合tf.GraphKeys.GLOBAL_VARIABLES和ops.GraphKeys.MODEL_VARIABLES中。 -
trainable如果True还将变量添加到图形集合GraphKeys.TRAINABLE_VARIABLES(请参阅tf.Variable)。 -
cols_to_vars如果不None, 必须是一个字典,将填充来自的映射_FeatureColumn到关联列表Variables。例如,在调用之后,我们可能有 cols_to_vars = { _NumericColumn( key='numeric_feature1', shape=(1,): [],'bias':[ ], _NumericColumn(key='numeric_feature2', shape=(2,)): [ ]} 如果一列没有创建变量,它的值将是一个空列表。请注意,cols_to_vars 还将包含一个映射到变量列表的字符串键 'bias'。
返回
-
Tensor表示线性模型的预测/logits。它的形状是 (batch_size, units),它的 dtype 是float32。
抛出
-
ValueError如果feature_columns中的项目既不是_DenseColumn也不是_CategoricalColumn。
此函数根据输出维度 units 生成加权和。加权和是指分类问题中的对数。它指的是线性回归问题的预测本身。
关于支持的列的注意事项:linear_model 将分类列视为 indicator_column s。具体来说,假设输入为SparseTensor,如下所示:
shape = [2, 2]
{
[0, 0]:"a"
[1, 0]:"b"
[1, 1]:"c"
}
linear_model 为 "a"、"b"、"c' 的存在分配权重,就像 indicator_column 一样,而 input_layer 明确要求用 embedding_column 或 indicator_column 包装每个分类列。
使用示例:
price = numeric_column('price')
price_buckets = bucketized_column(price, boundaries=[0., 10., 100., 1000.])
keywords = categorical_column_with_hash_bucket("keywords", 10K)
keywords_price = crossed_column('keywords', price_buckets, ...)
columns = [price_buckets, keywords, keywords_price ...]
features = tf.io.parse_example(..., features=make_parse_example_spec(columns))
prediction = linear_model(features, columns)
sparse_combiner 参数的工作方式如下例如,对于表示为分类列的两个特征:
# Feature 1
shape = [2, 2]
{
[0, 0]:"a"
[0, 1]:"b"
[1, 0]:"c"
}
# Feature 2
shape = [2, 3]
{
[0, 0]:"d"
[1, 0]:"e"
[1, 1]:"f"
[1, 2]:"f"
}
使用sparse_combiner 作为"mean",线性模型输出结果为:
y_0 = 1.0 / 2.0 * ( w_a + w_b ) + w_d + b
y_1 = w_c + 1.0 / 3.0 * ( w_e + 2.0 * w_f ) + b
其中y_i是输出,b是偏差,w_x是分配给输入特征中存在x的权重。
相关用法
- Python tf.compat.v1.feature_column.categorical_column_with_vocabulary_file用法及代码示例
- Python tf.compat.v1.feature_column.make_parse_example_spec用法及代码示例
- 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.linear_model。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
