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 模型中有效地使用这些特征列,以提高模型的性能和表达能力。

相关推荐
m0_6090004225 分钟前
向日葵好用吗?4款稳定的远程控制软件推荐。
运维·服务器·网络·人工智能·远程工作
开MINI的工科男1 小时前
深蓝学院-- 量产自动驾驶中的规划控制算法 小鹏
人工智能·机器学习·自动驾驶
AI大模型知识分享2 小时前
Prompt最佳实践|如何用参考文本让ChatGPT答案更精准?
人工智能·深度学习·机器学习·chatgpt·prompt·gpt-3
张人玉4 小时前
人工智能——猴子摘香蕉问题
人工智能
草莓屁屁我不吃4 小时前
Siri因ChatGPT-4o升级:我们的个人信息还安全吗?
人工智能·安全·chatgpt·chatgpt-4o
小言从不摸鱼4 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
AI科研视界5 小时前
ChatGPT+2:修订初始AI安全性和超级智能假设
人工智能·chatgpt
霍格沃兹测试开发学社测试人社区5 小时前
人工智能 | 基于ChatGPT开发人工智能服务平台
软件测试·人工智能·测试开发·chatgpt
小R资源5 小时前
3款免费的GPT类工具
人工智能·gpt·chatgpt·ai作画·ai模型·国内免费
artificiali8 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python