TensorFlow 深度学习 | 特征工程与 tf.feature_column

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在优快云上与你们相遇~💖

在这里插入图片描述

本博客的精华专栏:
【自动化测试】 【测试经验】 【人工智能】 【Python】


在这里插入图片描述

TensorFlow 深度学习:特征工程与 tf.feature_column

在深度学习任务中,特征工程(Feature Engineering)是决定模型效果的关键步骤。虽然 TensorFlow 2.x 已经推荐使用 Keras 的 tf.keras.layers 来处理特征,但 tf.feature_column 依然是一个常见且强大的工具,尤其适合构建结构化数据(tabular data)的输入管道。

本文将详细介绍 tf.feature_column 的常见用法,并结合代码示例进行说明。


🔹 一、为什么需要特征工程?

在原始数据中,特征可能是数值型、分类型、文本型甚至是稀疏特征。深度学习模型通常只能直接处理数值向量,因此我们需要通过 特征工程 将原始数据转换为适合神经网络输入的张量。

例如:

  • 将类别特征(如“性别”)转化为 独热编码(One-Hot Encoding)
  • 将连续数值特征(如“年龄”)进行 分桶(Bucketization)
  • 将文本特征转化为 嵌入向量(Embedding)
  • 将多个特征 组合交叉(Crossed Feature) 来捕捉非线性关系。

tf.feature_column 提供了一系列 API 来实现这些操作。


🔹 二、数值型特征

最基础的情况就是 数值特征,它们可以直接输入到模型。

import tensorflow as tf

# 定义数值型特征
age = tf.feature_column.numeric_column("age")

# 构建输入层
feature_columns = [age]
feature_layer = tf.keras.layers.DenseFeatures(feature_columns)

# 模拟输入
inputs = {
   
   "age": tf.constant([[23.], [45.], [31.]])}
print(feature_layer(inputs))

🔹 三、类别特征

1. 分类 ID(离散类别)

对于类别型特征,可以使用 categorical_column_with_vocabulary_list 将类别转化为 one-hot 编码。

gender = tf.feature_column.categorical_column_with_vocabulary_list(
    key="gender",
    vocabulary_list=["male", "female"]
)

gender_one_hot = tf.featur
C:\Users\ggbond\AppData\Local\anaconda3\python.exe C:/Users/ggbond/Desktop/基于深度学习的中文语音识别系统(完整代码+报告+毕业设计)/deepspeechrecognition/test.py 2025-03-17 00:57:49.368113: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. 2025-03-17 00:57:50.202278: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`. Traceback (most recent call last): File "C:\Users\ggbond\Desktop\基于深度学习的中文语音识别系统(完整代码+报告+毕业设计)\deepspeechrecognition\test.py", line 4, in <module> import tensorflow as tf File "C:\Users\ggbond\AppData\Local\anaconda3\Lib\site-packages\tensorflow\__init__.py", line 49, in <module> from tensorflow._api.v2 import __internal__ File "C:\Users\ggbond\AppData\Local\anaconda3\Lib\site-packages\tensorflow\_api\v2\__internal__\__init__.py", line 13, in <module> from tensorflow._api.v2.__internal__ import feature_column File "C:\Users\ggbond\AppData\Local\anaconda3\Lib\site-packages\tensorflow\_api\v2\__internal__\feature_column\__init__.py", line 8, in <module> from tensorflow.python.feature_column.feature_column_v2 import DenseColumn # line: 1777 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ggbond\AppData\Local\anaconda3\Lib\site-packages\tensorflow\python\feature_column\feature_column_v2.py", line 38, in <module> from tensorflow.python.feature_column import feature_column as fc_old File "C:\Users\ggbond\AppData\Local\anaconda3\Lib\site-packages\tensorflow\python\feature_column\feature_column.py", line 41, in <module> from tensorflow.python.layers import base File "C:\Users\ggbond
03-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Thomas Kant

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值