第二阶段-tensorflow程序图文详解(一)Estimators

来源:互联网 发布:淘宝上的知名甜品店 编辑:程序博客网 时间:2024/06/06 21:06

第二阶段的文档介绍,编写TensorFlow代码的细节。
注意:TensorFlow 1.3版本之后官方文档及API改动比较大。 blog使用1.4版本。

Estimators是一个高层次的tensorflow API,能极大简化tensorflow程序。
注意:TensorFlow还在tf.contrib.learn.Estimator中包含一个弃用的Estimator类。

1, training
2, evaluation
3, prediction
4, export for serving

您可以使用我们提供估算器或编写定制估算器。 所有估算器无论是预制还是定制 - 都是基于tf.estimator.Estimator类的类。

一,Advantages of Estimators(estimator的优势)

1,您可以在本地主机上或在分布式多服务器环境中运行基于估计器的模型,而无需更改模型。 此外,您可以在CPU,GPU或TPU上运行基于估计器的模型,而无需重新编码模型。
2,估算器简化了模型开发人员之间的共享实现。
3,您可以使用高级直观的代码开发最先进的模型。简而言之,使用Estimators创建模型通常比使用低级别的TensorFlow API更容易。
4,估算器本身是建立在tf.layers上,这简化了定制。
5, 估计者为你建立图表。 换句话说,你不必建立图表。
6, 估算人员提供安全的分布式培训循环,控制如何和何时:

     6.1建立图表     6.2初始化变量     6.3开始排队     6.4处理异常     6.5创建检查点文件并从失败中恢复     6.6保存TensorBoard的摘要

二,Pre-made Estimators(预设Estimators)

tensorflow里预设一些Estimators,我们可以直接通过Pre-made Estimators修改一点代码,就能尝试不同的模型。非常方便。
例如:DNNClassifier一个基于密集的前馈神经网络来训练分类模型。

1,Structure of a pre-made Estimators program(预设Estimators程序结构)
通常有4 step组成:
step1,编写一个或多个数据集导入功能
例如,您可以创建一个函数来导入训练集,另一个函数可以导入测试集。 每个数据集导入函数都必须返回两个对象:

  • 一个字典,其中的key是特征的名称,value是张量(或SparseTensors)包含相应的特征数据。

  • 含有一个或多个标签的张量。

例如:以下代码给出了input函数的框架。

def input_fn(dataset):   ...  # manipulate dataset, extracting feature names and the label   return feature_dict, label

step2,定义特征列
每个tf.feature_column标识一个特征名称,类型和任何输入预处理。
例如,下面的代码片断创建了三个保存整数或浮点数据的特征列。 前两个特征列只是标识特征的名称和类型。 第三个特性列还指定了一个lambda程序来调用来缩放原始数据:

# 定义三个数值型的特征列population = tf.feature_column.numeric_column('population')crime_rate = tf.feature_column.numeric_column('crime_rate')median_education = tf.feature_column.numeric_column('median_education',                    normalizer_fn='lambda x: x - global_education_mean')

step3,实例化一个相关的预设估算器

例如,下面是一个名为LinearClassifier的预先估算器的示例实例:

# 实例化一个estimator,导入特征列estimator = tf.estimator.Estimator.LinearClassifier(    feature_columns=[population, crime_rate, median_education],    )

step4,调用train,evaluation等相关的方法

例如:所有的评估器都提供一个train方法用来训练模型。

# my_training_set is the function created in Step 1estimator.train(input_fn=my_training_set, steps=2000)

2,Benefits of pre-made Estimators(预设Estimators的好处)

  • 好处一,确定不同部分计算图应该在哪里运行,还能有策略的在单机,集群上运行。

  • 好处二,能够自动写入一些有用的events 到summary,方便追踪训练过程。

如果您不使用预设估算器,则必须自行实施上述功能。

三,Custom Estimators(定制Estimators)

The heart of every Estimator–whether pre-made or custom–is its model function, which is a method that builds graphs for training, evaluation, and prediction. When you are using a pre-made Estimator, someone else has already implemented the model function. When relying on a custom Estimator, you must write the model function yourself. A companion document explains how to write the model function.
无论是预制还是定制 其模型函数,都是一种为训练,评估和预测构建图形的方法。 当您使用预制的估算器时,其他人已经实施了模型功能。 当依靠自定义的估算器时,您必须自己编写模型函数。 要有开发文档解释模型函数。(后面blog会详细介绍定制Estimators)

四,Recommended workflow(推荐的工作流)
推荐工作流:

  1. 如果有预设评估器符合你的要求,请使用。
  2. 使用预设评估器,能训练测试你的数据,能够保证数据的完整性,可靠性。
  3. 如果有其他合适的Estimators,运行测试选择最佳评估器。
  4. 长远来看,你可以自己定制模型,改善Estimators。

五,Creating Estimators from Keras models(基于Keras模型创建Estimators)

您可以将现有的Keras模型转换为Estimators。 这样做可以发挥Estimator的优势,例如分布式训练。 调用tf.keras.estimator.model_to_estimator,如下例所示:

# Instantiate a Keras inception v3 model.keras_inception_v3 = tf.keras.applications.inception_v3.InceptionV3(weights=None)# Compile model with the optimizer, loss, and metrics you'd like to train with.keras_inception_v3.compile(optimizer=tf.keras.optimizers.SGD(lr=0.0001, momentum=0.9),                          loss='categorical_crossentropy',                          metric='accuracy')# Create an Estimator from the compiled Keras model.est_inception_v3 = tf.keras.estimator.model_to_estimator(keras_model=keras_inception_v3)# Treat the derived Estimator as you would any other Estimator. For example,# the following derived Estimator calls the train method:est_inception_v3.train(input_fn=my_training_set, steps=2000)
原创粉丝点击