澳门网络娱乐游戏平台-澳门电子游戏娱乐网址-官方直营

澳门平台网址大全:tf.estimator 火速入门

未经同意,不得转发,感谢~~

Tuesday, September 12, 2017

自家是叁个很懒的人,作者想试试

盼望自身能一心一德到最后,把tensorflow的官方教程全部翻译出来

提升协和,也赞助别人

自己的博客:平生学习者

TensorFlow作为深度学习的主流框架之一,还是那个有不可能贫乏学习一下的.

Posted by The TensorFlow Team

tf.estimator Quickstart

TensorFlow 的高档次机器学习 API(tf.estimator卡塔尔国使得配置,练习,和评估五颜六色的机械学习模型变得越来越的轻松。在本教程中,你将利用 tf.estimator 布局叁个神经互连网分类器,在 Iris 数据集 上演习,并经过花的萼片和花瓣的几何样子预测花的门类。你将编制代码来促成以下七个步骤:

  1. 澳门平台网址大全:tf.estimator 火速入门。读取包涵 Iris 练习/测量检验数据的 CSVs 数据格式,到TensorFlow Dataset
  2. 布局贰个 神经互连网分类器
  3. 运用练习多少演练模型
  4. 评估模型的准头
  5. 分拣新的范本

小心:请在起头本课程前, 设置 TensorFlow 到你的机器上

本篇小说的代码与图片均来自官方网站文书档案Getting Started with TensorFLow

TensorFlow 1.3 introduces two important features that you should try out:

Complete Neural Network Source Code

以下是神经网络分类器的共同体代码:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
from six.moves.urllib.request import urlopen

import numpy as np
import tensorflow as tf

# Data sets
IRIS_TRAINING = "iris_training.csv"
IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv"

IRIS_TEST = "iris_test.csv"
IRIS_TEST_URL = "http://download.tensorflow.org/data/iris_test.csv"

def main():
  # If the training and test sets aren't stored locally, download them.
  if not os.path.exists(IRIS_TRAINING):
    raw = urlopen(IRIS_TRAINING_URL).read()
    with open(IRIS_TRAINING, "wb") as f:
      f.write(raw)

  if not os.path.exists(IRIS_TEST):
    raw = urlopen(IRIS_TEST_URL).read()
    with open(IRIS_TEST, "wb") as f:
      f.write(raw)

  # Load datasets.
  training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
      filename=IRIS_TRAINING,
      target_dtype=np.int,
      features_dtype=np.float32)
  test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
      filename=IRIS_TEST,
      target_dtype=np.int,
      features_dtype=np.float32)

  # Specify that all features have real-value data
  feature_columns = [tf.feature_column.numeric_column("x", shape=[4])]

  # Build 3 layer DNN with 10, 20, 10 units respectively.
  classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
                                          hidden_units=[10, 20, 10],
                                          n_classes=3,
                                          model_dir="/tmp/iris_model")
  # Define the training inputs
  train_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": np.array(training_set.data)},
      y=np.array(training_set.target),
      num_epochs=None,
      shuffle=True)

  # Train model.
  classifier.train(input_fn=train_input_fn, steps=2000)

  # Define the test inputs
  test_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": np.array(test_set.data)},
      y=np.array(test_set.target),
      num_epochs=1,
      shuffle=False)

  # Evaluate accuracy.
  accuracy_score = classifier.evaluate(input_fn=test_input_fn)["accuracy"]

  print("nTest Accuracy: {0:f}n".format(accuracy_score))

  # Classify two new flower samples.
  new_samples = np.array(
      [[6.4, 3.2, 4.5, 1.5],
       [5.8, 3.1, 5.0, 1.7]], dtype=np.float32)
  predict_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": new_samples},
      num_epochs=1,
      shuffle=False)

  predictions = list(classifier.predict(input_fn=predict_input_fn))
  predicted_classes = [p["classes"] for p in predictions]

  print(
      "New Samples, Class Predictions:    {}n"
      .format(predicted_classes))

if __name__ == "__main__":
    main()

以下部分将详细介绍代码细节。

预先善其事,必先利其器.

  • Datasets: A completely new way of creating input pipelines (that is, reading data into your program).

  • Estimators: A high-level way to create TensorFlow models. Estimators include pre-made models for common machine learning tasks, but you can also use them to create your own custom models.

Load the Iris CSV data to TensorFlow

Iris 数据集 包蕴了 150 行数据,两种有关的 Iris 品种,每一个 Iris 品种有 50 个样板: Iris setosaIris virginica,和Iris versicolor

澳门平台网址大全 1

Petal geometry compared for three iris species: Iris setosa, Iris virginica, and Iris versicolor

从左到右, Iris setosa (by Radomil, CC BY-SA 3.0State of Qatar,Iris versicolor (by Dlanglois, CC BY-SA 3.0卡塔尔,和 Iris virginica(by Frank May田野同志, CC BY-SA 2.0卡塔尔国.

对于各种花朵样品,每一行都含有了以下数据:萼片长度,萼片宽度, 花瓣长度,花瓣宽度和花的档期的顺序。花的等级次序用整数型数字代表,0代表Iris setosa,1表示Iris versicolor

Sepal Length Sepal Width Petal Length Petal Width Species
5.1 3.5 1.4 0.2 0
4.9 3.0 1.4 0.2 0
4.7 3.2 1.3 0.2 0
7.0 3.2 4.7 1.4 1
6.4 3.2 4.5 1.5 1
6.9 3.1 4.9 1.5 1
6.5 3.0 5.2 2.0 2
6.2 3.4 5.4 2.3 2
5.9 3.0 5.1 1.8 2

本学科中,Iris 数据随机打乱并私分成五个单身的 CSV 数据集:

  • 含有 120 个样品的锻炼集 (iris_training.csv)
  • 包蕴 30 个样品的测量试验集 (iris_test.csv)

千帆竞发前,首先 import 进具备的要求的模块,并定义何地下载数据和仓库储存数据集:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
from six.moves.urllib.request import urlopen

import tensorflow as tf
import numpy as np

IRIS_TRAINING = "iris_training.csv"
IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv"

IRIS_TEST = "iris_test.csv"
IRIS_TEST_URL = "http://download.tensorflow.org/data/iris_test.csv"

接下来,要是锻炼和测量试验数据集不总是存在于本地,那么下载它们。

if not os.path.exists(IRIS_TRAINING):
  raw = urlopen(IRIS_TRAINING_URL).read()
  with open(IRIS_TRAINING,'wb') as f:
    f.write(raw)

if not os.path.exists(IRIS_TEST):
  raw = urlopen(IRIS_TEST_URL).read()
  with open(IRIS_TEST,'wb') as f:
    f.write(raw)

下一步,使用 learn.datasets.base 中的load_csv_with_header() 方法读取练习和测量检验数据集,加载到 Dataset 中。load_csv_with_header() 方法富含几个必备的参数:

  • filename, 它从 CSV 文件获得文件路线
  • target_dtype, 它应用数据集目的值的 numpy 数据类型
  • features_dtype, 它使用数据集特征值的 numpy 数据类型

在那处,指标(值是你操练的模子的估计卡塔尔是花的品类,它是三个从 0 到 2 的板寸,所以适当的numpy数据类型是np.int

# Load datasets.
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
    filename=IRIS_TRAINING,
    target_dtype=np.int,
    features_dtype=np.float32)
test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
    filename=IRIS_TEST,
    target_dtype=np.int,
    features_dtype=np.float32)

Dataset 在 tf.contrib.learn 中名叫元组;你能够经过 datatarget字段访谈特征数据和指标值。这里, training_set.datatraining_set.target 分别包蕴了操练集的特色数据和天性值,而 test_set.datatest_set.target 分别包罗了测量试验集的特征数据和对象值。

稍后,在 "Fit the DNNClassifier to the Iris Training Data," 你将利用training_set.datatraining_set.target演习你的模型,在澳门在线赌网站注册送 ,"Evaluate Model Accuracy," 你将选用 test_set.datatest_set.target。但首先,你将要下一节中布局你的模子。

上学TensorFLow在此之前必需在微Computer上下载并设置配备好TensorFlow。

Below you can see how they fit in the TensorFlow architecture. Combined, they offer an easy way to create TensorFlow models and to feed data to them:

Construct a Deep Neural Network Classifier

tf.estimator 提供了各样预约义的模子,称为Estimator,你能够动用"开箱即用"对你的数据运维锻炼和评估操作。在那间,你将布置二个深度神经互连网分类器模型来适应 Iris 数据。使用 tf.estimator,你能够经过两行代码来实例化你的 tf.estimator.DNNClassifier

# Specify that all features have real-value data
feature_columns = [tf.feature_column.numeric_column("x", shape=[4])]

# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
                                        hidden_units=[10, 20, 10],
                                        n_classes=3,
                                        model_dir="/tmp/iris_model")

地方的代码首先定义了模型的特征列,钦赐了数量汇总的特点的数据类型。全部的特点数据都是连连的,所以tf.feature_column.numeric_column 是用于组织特征列的确切函数。这里有八个特点在数码汇总(萼片宽度,萼片长度,花瓣宽度和花瓣长度State of Qatar,于是shape 必得安装为[4]以适应全部的数据。

下一场,代码应用以下参数创设了几个 DNNClassifier 模型:

  • feature_columns=feature_columns。上边定义的一组特征列。
  • hidden_units=[10, 20, 10]。三个 隐藏层,分别包括10,20 和 10 神经元。
  • n_classes=3。多个指标分类,代表三种 Iris 品种。
  • model_dir=/tmp/iris_model。TensorFlow 将要模型练习时期保留检查测验数据和 TensorBoard 摘要的目录。

有关TensorFlow的设置互连网能够找到超多的科目,多找多少个试试看就能够了。

Describe the training input pipeline

tf.estimator API 使用输入函数,创立了为了模型生成数据的 TensorFlow 操作。大家能够选拔tf.estimator.inputs.numpy_input_fn来发生输入管道:

# Define the training inputs
train_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": np.array(training_set.data)},
    y=np.array(training_set.target),
    num_epochs=None,
    shuffle=True)

澳门平台网址大全 ,官方网站也提供了对应的安装指南:Installing TensorFlow

澳门平台网址大全 2

Fit the DNNClassifier to the Iris Training Data

以往,你早就配备了你的 DNN classifier 模型,你能够行使 train 方法将模型拟合 Iris 锻练多少。将 train_input_fn 传递给input_fn,并安装练习的次数(这里是 二〇〇三卡塔尔:

# Train model.
classifier.train(input_fn=train_input_fn, steps=2000)

模型的景色是保存在classifier,那象征一旦您赏识您可以迭代替练习练模型。举个例子,以上代码等同于以下代码:

classifier.train(input_fn=train_input_fn, steps=1000)
classifier.train(input_fn=train_input_fn, steps=1000)

而是,假设你希望在练习的历程中追踪模型,则你或者更要求接受TensorFlow 的 SessionRunHook 来实行日志操作记录。

TensorFlow提供了重重的样例代码,供大家学习.

Our Example Model

To explore these features we're going to build a model and show you relevant code snippets. The complete code is available here, including instructions for getting the training and test files. Note that the code was written to demonstrate how Datasets and Estimators work functionally, and was not optimized for maximum performance.

The trained model categorizes Iris flowers based on four botanical features (sepal length, sepal width, petal length, and petal width). So, during inference, you can provide values for those four features and the model will predict that the flower is one of the following three beautiful variants:

澳门平台网址大全 3

From left to right: Iris setosa (by Radomil, CC BY-SA 3.0), Iris versicolor (by Dlanglois, CC BY-SA 3.0), and Iris virginica(by Frank Mayfield, CC BY-SA 2.0).

We're going to train a Deep Neural Network Classifier with the below structure. All input and output values will be float32, and the sum of the output values will be 1 (as we are predicting the probability for each individual Iris type):

澳门平台网址大全 4

For example, an output result might be 0.05 for Iris Setosa, 0.9 for Iris Versicolor, and 0.05 for Iris Virginica, which indicates a 90% probability that this is an Iris Versicolor.

Alright! Now that we have defined the model, let's look at how we can use Datasets and Estimators to train it and make predictions.

Evaluate Model Accuracy

您曾在 Iris 演习多少上练习了你的DNNClassifier模型。以后您能够在 Iris测量试验数据上行使 evaluate 方法来检验模型的正确性。像train那样,evaluate 使用输入函数营造它的输入管道。evaluate回去一个饱含评估结果的dict。以下代码传递 Iris 测量检验数据——test_set.datatest_set.targetevaluate 并从结果中打字与印刷 accuracy

# Define the test inputs
test_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": np.array(test_set.data)},
    y=np.array(test_set.target),
    num_epochs=1,
    shuffle=False)

# Evaluate accuracy.
accuracy_score = classifier.evaluate(input_fn=test_input_fn)["accuracy"]

print("nTest Accuracy: {0:f}n".format(accuracy_score))

注意:在这里 numpy_input_fn 中参数num_epochs=1 是很首要的。test_input_fn 将迭代数据三遍,然后触发OutOfRangeError。那几个错误表示分类器甘休评估,所以它将对输入只评估三次。

当您运转总体脚本,它将打字与印刷相似下边包车型地铁数字:

Test Accuracy: 0.966667

您的准头结果恐怕会有某个例外,不过应该超越十分之八。对于多少个对峙很小的数据集来说那并不算太差!

  1. 选料三个你想要用来放置那一个代码的目录,然后运营以下命令就会获取到(当然前提是你的微计算机上装好了git,越多关于git的操作戳这里哦~git clone https://github.com/tensorflow/models

  2. 下一场步入到那篇小说里面用到的代码目录:cd models/samples/core/get_started/能够观望该目录下有4份代码文件。那篇教程用到了内部的多个代码文件:

    • premade_estimator.py用来模型创立以至锻炼测验
    • iris_data.py用来对数码集举行拍卖。
  3. 试跳看运维程序python premade_estimator.py

    自己的Computer上运转的时候现身了不当。

    • Bug: AttributeError: 'module' object has no attribute 'keras'
    • 难点所在:AttributeError: module 'tensorflow' has no attribute 'keras'
    • 由来: 笔者今天的tensorflow版本是1.3的,而官方的tensorflow已经升任到了1.4.新添了tf.keras tf.data等API
    • 化解措施: pip install --upgrade tensorflow履新tensorflow运转结果如下所示:

Introducing The Datasets

Datasets is a new way to create input pipelines to TensorFlow models. This API is much more performant than using feed_dict or the queue-based pipelines, and it's cleaner and easier to use. Although Datasets still resides in tf.contrib.data at 1.3, we expect to move this API to core at 1.4, so it's high time to take it for a test drive.

At a high-level, the Datasets consists of the following classes:

澳门平台网址大全 5

Where:

  • Dataset: Base class containing methods to create and transform datasets. Also allows you initialize a dataset from data in memory, or from a Python generator.
  • TextLineDataset: Reads lines from text files.
  • TFRecordDataset: Reads records from TFRecord files.
  • FixedLengthRecordDataset: Reads fixed size records from binary files.
  • Iterator: Provides a way to access one dataset element at a time.

Classify New Samples

使用 estimator 的 predict() 方法来分类新的样板。举例,假让你有那五个新的花的范本:

Sepal Length Sepal Width Petal Length Petal Width
6.4 3.2 4.5 1.5
5.8 3.1 5.0 1.7

您能够接收predict() 方法预测它们的档期的顺序。 predict回到叁个dict,你能够轻巧的将其转为 list 。以下代码检索并打字与印刷预测的类:

# Classify two new flower samples.
new_samples = np.array(
    [[6.4, 3.2, 4.5, 1.5],
     [5.8, 3.1, 5.0, 1.7]], dtype=np.float32)
predict_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"x": new_samples},
    num_epochs=1,
    shuffle=False)

predictions = list(classifier.predict(input_fn=predict_input_fn))
predicted_classes = [p["classes"] for p in predictions]

print(
    "New Samples, Class Predictions:    {}n"
    .format(predicted_classes))

你的结果看起来如下:

New Samples, Class Predictions:    [1 2]

就此你的模子预测了第一个样板是Iris versicolor,而第一个样品是 Iris virginica

澳门平台网址大全 6运转结果部分截图

Our dataset

To get started, let's first look at the dataset we will use to feed our model. We'll read data from a CSV file, where each row will contain five values-the four input values, plus the label:

澳门平台网址大全 7

The label will be:

  • 0 for Iris Setosa
  • 1 for Versicolor
  • 2 for Virginica.

Additional Resources

  • 欲领悟越来越多关于使用 tf.estimator 创立线性模型,请查阅 Large-scale Linear Models with TensorFlow。
  • 要利用 tf.estimator APIs 创设你子集的 Estimator,请查阅Creating Estimators in tf.estimator 。
  • 为了在浏览器中落到实处神经网络建模并可视化,请查阅Deep Playground。
  • 有关神经互联网更加尖端的教程,请查阅 Convolutional Neural Networks 和Recurrent Neural Networks。

本文由澳门网络娱乐游戏平台发布于编程,转载请注明出处:澳门平台网址大全:tf.estimator 火速入门

相关阅读