如何使用预制 Estimator 在 Tensorflow 中下载 Iris 数据集?
Tensorflow 可以使用预制 Estimator 通过 Keras 包中的 `get_file` 方法下载 iris 数据集。Google API 存储着 iris 数据集,可以将其作为参数传递给 `get_file` 方法。
阅读更多: 什么是 TensorFlow 以及 Keras 如何与 TensorFlow 协作创建神经网络?
我们将使用 Keras Sequential API,它有助于构建一个顺序模型,该模型用于处理简单的层堆栈,其中每一层都只有一个输入张量和一个输出张量。
包含至少一层卷积层的神经网络称为卷积神经网络。我们可以使用卷积神经网络构建学习模型。
TensorFlow Text 包含一系列与文本相关的类和操作,可用于 TensorFlow 2.0。TensorFlow Text 可用于预处理序列建模。
我们使用 Google Colaboratory 来运行以下代码。Google Colab 或 Colaboratory 帮助在浏览器上运行 Python 代码,无需任何配置,并且可以免费访问 GPU(图形处理单元)。Colaboratory 基于 Jupyter Notebook 构建。
让我们了解如何使用 Estimator。
Estimator 是 TensorFlow 中对完整模型的高级表示。它旨在实现轻松扩展和异步训练。
模型使用 iris 数据集进行训练。它有 4 个特征和一个标签。
- 萼片长度
- 萼片宽度
- 花瓣长度
- 花瓣宽度
基于这些信息,可以定义一些有助于解析数据的常量。
示例
import tensorflow as tf
import pandas as pd
print("Column names defined")
CSV_COLUMN_NAMES = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species']
SPECIES = ['Setosa', 'Versicolor', 'Virginica']
print("Iris training data is being downloaded")
train_path = tf.keras.utils.get_file(
"iris_training.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv")
print("Iris test data is being downloaded")
test_path = tf.keras.utils.get_file(
"iris_test.csv", "https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv")
train = pd.read_csv(train_path, names=CSV_COLUMN_NAMES, header=0)
test = pd.read_csv(test_path, names=CSV_COLUMN_NAMES, header=0)
print("Sample data is being displayed")
train.head()代码来源 -https://tensorflowcn.cn/tutorials/estimator/premade#first_things_first
输出
Column names defined Iris training data is being downloaded Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/iris_training.csv 8192/2194 [================================================================================================================] - 0s 0us/step Iris test data is being downloaded Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/iris_test.csv 8192/573 [============================================================================================================================================================================================================================================================================================================================================================================================================================================] - 0s 0us/step Sample data is being displayed SepalLength SepalWidth PetalLength PetalWidth Species 0 6.4 2.8 5.6 2.2 2 1 5.0 2.3 3.3 1.0 1 2 4.9 2.5 4.5 1.7 2 3 4.9 3.1 1.5 0.1 0 4 5.7 3.8 1.7 0.3 0
解释
- 定义列名。
- 下载数据。
- 在控制台上显示一些示例数据。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP