admin管理员组文章数量:1650829
1.作用
对hash映射之后的特征进行交叉。
优势:特征交叉,在有些情况下,特征独自编码与多维特征交叉后的特征特性会有不一样的结果。
使用方式:
tf.feature_column.crossed_column(
keys, hash_bucket_size, hash_key=None
)
输入:
keys | 可迭代识别功能交叉。每个元素可以是:
|
hash_bucket_size | 一个int> 1.桶的数量。ont-hot的长度。 |
hash_key | 指定将被使用的HASH_KEY FingerprintCat64 功能做交叉,与SparseCrossOp(可选)相结合。 |
输出:
交叉后的Tensor。输出为cross的one-hot结果,hash_bucket_size代表输出的交叉向量的one-hot维度。
2.例子
官方文档中给了一个执行交叉的过程:从例子中可看出,按照index进行交叉的,也就是说同一个样本,不同特征进行交叉。对于缺失的特征,使用前一个index的特征做交叉。
SparseTensor1:
shape = [2, 2]
{
[0, 0]: "a"
[1, 0]: "b"
[1, 1]: "c"
}
SparseTensor2:
shape = [2, 1]
{
[0, 0]: "d"
[1, 0]: "e"
}
交叉的过程:
shape = [2, 2]
{
[0, 0]: Hash64("d", Hash64("a")) % hash_bucket_size
[1, 0]: Hash64("e", Hash64("b")) % hash_bucket_size
[1, 1]: Hash64("e", Hash64("c")) % hash_bucket_size
}
具体例子:
import tensorflow as tf
sess=tf.Session()
#特征数据
features = {
'sex': [1, 2, 1, 1, 2],
'department': ['sport', 'sport', 'drawing', 'gardening', 'travelling'],
}
#特征列
department = tf.feature_column.categorical_column_with_vocabulary_list('department', ['sport','drawing','gardening','travelling'], dtype=tf.string)
sex = tf.feature_column.categorical_column_with_identity('sex', num_buckets=2, default_value=0)
sex_department = tf.feature_column.crossed_column([department,sex], 10)
# sex_department = tf.feature_column.crossed_column([features['department'],features['sex']], 16)
sex_department = tf.feature_column.indicator_column(sex_department)
#组合特征列
columns = [sex_department]
#输入层(数据,特征列)
inputs = tf.feature_column.input_layer(features, columns)
#初始化并运行
init = tf.global_variables_initializer()
sess.run(tf.tables_initializer())
sess.run(init)
v=sess.run(inputs)
print(v)
输出:
[[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
参考
1.官方文档:https://www.tensorflow/versions/r1.15/api_docs/python/tf/feature_column/crossed_column
2.知乎:https://zhuanlan.zhihu/p/73701872
本文标签: 函数作用TFtensorflowcrossedcolumn
版权声明:本文标题:【TensorFlow】(七)tf.feature_column.crossed_column()函数的作用及用法 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dongtai/1729533616a1205127.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论