tensorflow feature_column 合集

前言

  • feature_column 输入输出类型,用一个数据集给出demo
  • feature_column 接estimator
  • feature_column 接Keras

1. feature_column的输入输出类型

1.1 输入输出类型

feature_column输入可以是原始特征的列名,或者是feature_column。初上手感觉feature_column设计的有点奇怪,不过熟悉了逻辑后用起来还是很方便的。几个需要习惯一下的点:

  • 深度模型的输入必须是Dense类型,所有输出是categorical类型需要经过indicator或者embedding的转换才可以;
  • indicator,embedding,bucketized 的输入不能是原始特征,前两者只能是categorical类型的feature_column,后者只能是numeric_column;
feature_column输入输出输出是否为dense
categorical_column_with_identity数值型离散categoricalN
categorical_column_with_vocabulary_list字符型/数值型离散categoricalN
categorical_column_with_hash_bucket类别太多的离散值categoricalN
crossed_columncategorical/离散值categoricalN
indicator_columncategoricalone/multi-hotY
embedding_columncategoricaldense vectorY
numeric_column数值型连续值numericY
bucketzied_columnnumeric_columnone-hotY

以下给出各种特征工程的demo,原始特征如下:

在这里插入图片描述

1.2 输入-连续值

在这里插入图片描述

1.3 输入-离散值

在这里插入图片描述

1.4 输入-categorical

在这里插入图片描述

2. feature_column接estimator

如果是使用预定义的estimator,feature_column 可以直接作为输入,不需要任何额外操作,只需要注意深度模型只支持Dense类型的feature_column即可。

如果是自定义estimator,则需要多一步用feature_column先创建input_layer。

input_layer = tf.feature_column.input_layer(features, feature_columns)

3. feature_column接keras

为什么要这么搭配呢,好像是没啥必要,只不过进一步证明tf的官方文档确实坑而已。

def model_fn():

    #define Keras input 
    input = {}
    for f in FEATURE_NAME:
        input[f] = Input(shape=(1,), name = f, dtype = DTYPE[f])
    
    #generate feature_columns
    feature_columns = build_features()
    
    #Define transformation from feature_columns to Dense Tensor 
    feature_layer = tf.keras.layers.DenseFeatures( feature_columns )
    
    #Transform input 
    dense_feature = feature_layer(input)
    
    output = Dense(1, activation='sigmoid')(dense_feature)
    
    #feed input placeholder as list 
    model = Model(inputs = [i for i in input.values()], outputs = output)

    return model
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
### TensorFlow 中 `feature_column` 模块导入失败的原因分析 当尝试导入 TensorFlow 的 `feature_column` 模块时出现问题,通常可能由以下几个方面引起: #### 1. **版本兼容性问题** 如果使用的 TensorFlow 版本较新而某些功能尚未完全迁移至 v2.x 或者存在 API 变更,则可能导致无法正常调用旧版中的方法。例如,在 TensorFlow 2.x 中,默认启用了 eager execution 模式,这可能会改变一些原有模块的行为方式[^1]。 ```python import tensorflow.compat.v1 as tf tf.disable_v2_behavior() ``` 上述代码片段展示了如何切换回兼容模式来适配早期依赖于静态图执行环境下的程序逻辑。通过这种方式可以规避因升级带来的不一致现象。 #### 2. **安装错误或路径冲突** 另一个常见原因是本地环境中可能存在多个不同版本的 Python 解释器或者库文件夹之间发生干扰情况。建议先确认当前工作区所关联的具体解释器及其对应已安装软件清单是否匹配预期目标配置需求[^3]。 可以通过运行如下命令检查实际加载到项目的tensorflow实例详情以及其所在磁盘位置: ```bash pip show tensorflow ``` 同时也可以利用以下脚本来验证是否存在重复定义的现象: ```python import pkg_resources print([d for d in pkg_resources.working_set if 'tensorflow' in str(d)]) ``` #### 3. **特定平台支持限制** 对于某些特殊操作系统架构(比如ARM处理器),官方发布的二进制轮子未必能够覆盖全部场景,因此需要自行编译源码才能获得完整的功能性体验[^4]. 如果遇到此类状况则需参照官方文档指南完成定制化构建流程. --- 以下是针对该类问题的一般排查步骤总结: - 确认正在使用的 Tensorflow 是否满足最低要求规格. - 尝试更新 pip 工具链后再重装最新稳定发行版次序. - 当仍然存在问题时候考虑降级回到之前稳定的长期维护分支(LTS). ### 示例修复方案 假设我们已经明确了是因为版本差异引起的异常行为,那么可以直接按照下面的方式调整我们的初始化过程: ```python try: import tensorflow.feature_column as fc except AttributeError: import tensorflow.compat.v1 as tf from tensorflow.compat.v1 import feature_column as fc tf.disable_v2_behavior() # 随后即可正常使用fc对象进行后续操作... columns = [ fc.numeric_column('x'), fc.categorical_column_with_identity(key='id', num_buckets=10), ] ``` 此段示范说明了即使在高阶框架下也能灵活应对底层变动所带来的挑战. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值