combiner
combiner的行为
tf.contrib.layers.sparse_column_with_hash_bucket 的参数中有一个combiner. 他的含义是说,当产生稠密向量的时候,使用combiner与权重重重新计算权重,也就是相当于对权重的正则化。举个例子来说,feature A中,包含两个稀疏向量,分别是[0, 1, 0]和[0,0,1] 他们分别对应一个权重0.4和0.1。那么对于不同的combiner处理方式是这样的:
- sum:
A = ([0, 1, 0] * 0.4 + [0, 0, 1] * 0.1)
- mean:
A = ([0, 1, 0] * 0.4 + [0, 0, 1] * 0.1) / (0.4 + 0.1)
- sqrtn:
A = ([0, 1, 0] * 0.4 + [0, 0, 1] * 0.1) / sqrt(0.4 * 0.4 + 0.1 * 0.1)
combiner的使用
查看 tf.contrib.learn.LinearClassifier, 调用关系如下:
LinearClassifier->BaseEstimator.fit->LinearClassifier._linear_model_fn->joint_weighted_sum_from_feature_columns->_create_joint_embedding_lookup->_safe_embedding_lookup_sparse->embedding_ops.embedding_lookup_sparse
所以对于featrue的处理最终就落到了 embedding_lookup_sparse 这个函数
embedding_lookup_sparse
文档中关于这个函数的说明非常清楚,也就是前面我列的combiner的行为哪里的阐述. 在这里最重要的是 sp_weights 这是一个参数。sp_weights是从_safe_embedding_lookup_sparse
的参数中获取的, 而 _safe_embedding_lookup_sparse
是在_create_joint_embedding_lookup
中赋值的参数。代码如下:
def _create_joint_embedding_lookup(columns_to_tensors,
embedding_lookup_arguments,
num_outputs,
trainable,
weight_collections):
"""Creates an embedding lookup for all columns sharing a single weight."""
for arg in embedding_lookup_arguments:
assert arg.weight_tensor is None, (
'Joint sums for weighted sparse columns are not supported. '
'Please use weighted_sum_from_feature_columns instead.')
assert arg.combiner == 'sum', (
'Combiners other than sum are not supported for joint sums. '
'Please use weighted_sum_from_feature_columns instead.')
assert len(embedding_lookup_arguments) >= 1, (
'At least one column must be in the model.')
prev_size = 0
sparse_tensors = []
for a in embedding_lookup_arguments:
t = a.input_tensor
values = t.values + prev_size
prev_size += a.vocab_size
sparse_tensors.append(
sparse_tensor_py.SparseTensor(t.indices,
values,
t.dense_shape))
sparse_tensor = sparse_ops.sparse_concat(1, sparse_tensors)
with variable_scope.variable_scope(
None, default_name='linear_weights', values=columns_to_tensors.values()):
variable = contrib_variables.model_variable(
name='weights',
shape=[prev_size, num_outputs],
dtype=dtypes.float32,
initializer=init_ops.zeros_initializer(),
trainable=trainable,
collections=weight_collections)
if fc._is_variable(variable): # pylint: disable=protected-access
variable = [variable]
else:
variable = variable._get_variable_list() # pylint: disable=protected-access
predictions = embedding_ops.safe_embedding_lookup_sparse(
variable,
sparse_tensor,
sparse_weights=None,
combiner='sum',
name='_weights')
return variable, predictions
看到在 safe_embedding_lookup_sparse 调用的时候,将sparse_weights设置成了None, 同时combiner为’sum’. 所以实际在featrue_column中设置的combiner根本就没有起到任何作用,都是sum,同时因为 ‘sparser_weights=None’ 所以weight是没有数据的。其实即使这里不设置为none在 _SparserColumn
中也是获取的None. 所以这个参数在实际的线性分类器中就没有起作用。