Tensorflow 特征列说明

总结以下类型的用法

tf.feature_column.numeric_column、

tf.feature_column.bucketized_column、

tf.feature_column.categorical_column_with_hash_bucket、

tf.feature_column.embedding_column、

tf.feature_column.shared_embedding_columns、

tf.feature_column.indicator_column、

tf.feature_column.crossed_column

以下是对每个特征列的详细说明,包括它们的作用、适用场景、示例,以及在 Wide & Deep 模型中的应用侧。

1. tf.feature_column.numeric_column

  • 作用: 表示数值特征,适用于连续值特征。
  • 应用侧: 通常用于 Deep 侧。
  • 适用场景 :
    • 例子 1: 房价预测中的面积(如平方英尺)。
    • 例子 2: 用户行为分析中的浏览时间(如秒数)。

示例:

python 复制代码
area = tf.feature_column.numeric_column("area")  # 面积
browse_time = tf.feature_column.numeric_column("browse_time")  # 浏览时间

2. tf.feature_column.bucketized_column

  • 作用: 将数值特征分桶(离散化),将连续值映射到离散的区间。
  • 应用侧: 可以用于 Wide 侧和 Deep 侧。
  • 适用场景 :
    • 例子 1: 将用户年龄分段(如18-25、26-35等)。
    • 例子 2: 将收入分段(如低收入、中等收入、高收入)。

示例:

python 复制代码
age = tf.feature_column.numeric_column("age")
age_buckets = tf.feature_column.bucketized_column(age, boundaries=[18, 25, 35, 45, 55, 65])  # 年龄分段

3. tf.feature_column.categorical_column_with_hash_bucket

  • 作用: 表示高基数的分类特征,通过哈希函数将特征值映射到固定数量的桶中。
  • 应用侧: 通常用于 Wide 侧。
  • 适用场景 :
    • 例子 1: 用户ID(如"user_12345")。
    • 例子 2: 产品ID(如"product_67890")。

示例:

python 复制代码
user_id = tf.feature_column.categorical_column_with_hash_bucket("user_id", hash_bucket_size=10000)  # 用户ID
product_id = tf.feature_column.categorical_column_with_hash_bucket("product_id", hash_bucket_size=5000)  # 产品ID

4. tf.feature_column.embedding_column

  • 作用: 将分类特征映射到低维稠密向量(嵌入向量),通常用于深度学习模型。
  • 应用侧: 通常用于 Deep 侧。
  • 适用场景 :
    • 例子 1: 推荐系统中的用户嵌入表示。
    • 例子 2: 自然语言处理中的词嵌入。

示例:

python 复制代码
user_id = tf.feature_column.categorical_column_with_hash_bucket("user_id", hash_bucket_size=10000)
user_id_embedding = tf.feature_column.embedding_column(user_id, dimension=16)  # 用户ID嵌入

5. tf.feature_column.shared_embedding_columns

  • 作用: 将多个分类特征共享同一个嵌入向量空间,适用于多个特征具有相似的语义空间。
  • 应用侧: 通常用于 Deep 侧。
  • 适用场景 :
    • 例子 1: 多语言文本处理中的词嵌入。
    • 例子 2: 推荐系统中用户和物品的共同嵌入。

示例:

python 复制代码
user_id = tf.feature_column.categorical_column_with_hash_bucket("user_id", hash_bucket_size=10000)
product_id = tf.feature_column.categorical_column_with_hash_bucket("product_id", hash_bucket_size=5000)
shared_embedding = tf.feature_column.shared_embedding_columns([user_id, product_id], dimension=16)  # 共享嵌入

6. tf.feature_column.indicator_column

  • 作用: 将分类特征转换为稀疏的独热编码(one-hot encoding),用于表示每个类别的存在与否。
  • 应用侧: 通常用于 Wide 侧。
  • 适用场景 :
    • 例子 1: 处理低基数的分类特征,如性别("male"、"female")。
    • 例子 2: 颜色特征("red"、"green"、"blue")。

示例:

python 复制代码
gender = tf.feature_column.categorical_column_with_vocabulary_list("gender", ["male", "female"])  # 性别
gender_indicator = tf.feature_column.indicator_column(gender)  # 性别独热编码

7. tf.feature_column.crossed_column

  • 作用: 创建交叉特征,用于捕捉多个特征之间的相互关系。
  • 应用侧: 通常用于 Wide 侧。
  • 适用场景 :
    • 例子 1: 捕捉用户特征和产品特征之间的关系(如用户的地理位置和购买的产品)。
    • 例子 2: 组合地理位置和时间段的特征(如"北京-早上")。

示例:

python 复制代码
location = tf.feature_column.categorical_column_with_hash_bucket("location", hash_bucket_size=1000)  # 地理位置
time_of_day = tf.feature_column.categorical_column_with_vocabulary_list("time_of_day", ["morning", "afternoon", "evening", "night"])  # 时间段
location_time_cross = tf.feature_column.crossed_column([location, time_of_day], hash_bucket_size=10000)  # 交叉特征

总结

通过明确每个特征列的作用、适用场景和示例,可以更好地理解如何在 Wide & Deep 模型中有效地使用这些特征列,以提高模型的性能和表达能力。

相关推荐
胡耀超30 分钟前
标签体系设计与管理:从理论基础到智能化实践的综合指南
人工智能·python·深度学习·数据挖掘·大模型·用户画像·语义分析
开-悟34 分钟前
嵌入式编程-使用AI查找BUG的启发
c语言·人工智能·嵌入式硬件·bug
Ailerx35 分钟前
YOLOv13震撼发布:超图增强引领目标检测新纪元
人工智能·yolo·目标检测
大咖分享课1 小时前
开源模型与商用模型协同开发机制设计
人工智能·开源·ai模型
你不知道我是谁?1 小时前
AI 应用于进攻性安全
人工智能·安全
reddingtons1 小时前
Adobe高阶技巧与设计师创意思维的进阶指南
人工智能·adobe·illustrator·设计师·photoshop·创意设计·aftereffects
机器之心2 小时前
刚刚,Grok4跑分曝光:「人类最后考试」拿下45%,是Gemini 2.5两倍,但网友不信
人工智能
蹦蹦跳跳真可爱5892 小时前
Python----大模型(使用api接口调用大模型)
人工智能·python·microsoft·语言模型
小爷毛毛_卓寿杰2 小时前
突破政务文档理解瓶颈:基于多模态大模型的智能解析系统详解
人工智能·llm
Mr.Winter`2 小时前
障碍感知 | 基于3D激光雷达的三维膨胀栅格地图构建(附ROS C++仿真)
人工智能·机器人·自动驾驶·ros·具身智能·环境感知