如何使用 Estimator 将 TensorFlow 用于特征列转换?
Tensorflow 可以与 Estimator 一起使用来转换特征列,方法是首先将数据集的第一行转换为字典,然后使用独热编码来转换此特征列,即“性别”列。
阅读更多: 什么是 TensorFlow 以及 Keras 如何与 TensorFlow 协作创建神经网络?
我们将使用 Keras Sequential API,它有助于构建一个用于处理简单层堆栈的顺序模型,其中每一层都只有一个输入张量和一个输出张量。
包含至少一层卷积层的神经网络称为卷积神经网络。我们可以使用卷积神经网络来构建学习模型。
我们正在使用 Google Colaboratory 来运行以下代码。Google Colab 或 Colaboratory 帮助在浏览器上运行 Python 代码,无需任何配置,并可免费访问 GPU(图形处理单元)。Colaboratory 是基于 Jupyter Notebook 构建的。
Estimator 是 TensorFlow 对完整模型的高级表示。它旨在简化扩展和异步训练。Estimator 使用特征列来描述模型如何解释原始输入特征。Estimator 期望一个数值输入向量,而特征列将有助于描述模型应该如何转换数据集中每个特征。
选择和使用正确的特征列集对于学习有效的模型至关重要。
示例
print("An example transformation produced by a feature column")
example = dict(dftrain.head(1))
class_fc = tf.feature_column.indicator_column(tf.feature_column.categorical_column_with_vocabulary_list('class', ('First', 'Second', 'Third')))
print('Feature value is: "{}"'.format(example['class'].iloc[0]))
print('One-hot encoded is: ', tf.keras.layers.DenseFeatures([class_fc])(example).numpy())代码来源 -https://tensorflowcn.cn/tutorials/estimator/boosted_trees
输出
An example transformation produced by a feature column Feature value is: "Third" One-hot encoded is: [[0. 0. 1.]]
解释
可以查看特征列产生的转换。
可以在控制台上查看 indicator_column 对单个示例的输出。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP