这篇文章介绍tf.estimator,一个高级TensorFlow API,可以极大简化机器学习编程。Estimators封装了下面几个活动。

  • 训练
  • 评估
  • 预测
  • 出口服务(export for serving)

可以使用tensorflow中自带的Estimators,也可以自定义Estimators。所有的Estimators,都继承自tf.estimator.Estimator类。

1. Estimators的优点

  • 可以在分布式多服务器环境下,无需修改代码运行基于Estimator的模型。可以运行Estimator-based模型在CPUs,GPUs, TPUs,无需重新编码模型。
  • 简化了模型开发者间的共享实现。
  • 开发高级的直观的代码。比使用低级API容易。
  • Estimators建立在tf.keras.layers,简化了自定义。
  • Estimators为你建立图
  • Estimators提供一个安全的分布式训练循环,控制如何和何时 1)建立图  2)初始变量  3)加载数据  4)处理异常  5)生成检查点文件和从错误中恢复   6)保存TensorBoard需要的summaries

当使用Estimators时,必须将数据输入管道和模型分开。这种分离简化了不同数据集上的实验。

2.  Pre-made Estimators

pre-made Estimators生成和管理tf.graph和tf.Session。并且只需要作出很小的代码改动,就能实验各种模型结构。下面以一个基于全链接,前馈神经网络训练分类模型为例。

pre-made Estimators 程序的结构

包含下面四个步骤:

1.     写一个或多个数据集导入函数。你肯呢个会生成一个函数用来导入训练集,另一个函数导入测试集。每个数据集导入函数必须包含两个对象:1)一个字典,keys是特征名,values是Tensors(或 Sparse Tensors)包含对应的特征数据。2)一个Tensor,包含一个或多个标签

代码的基本骨架如下:

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

2.定义特征列。每个tf.feature_column识别一个特征名,它的类型和任何输入预处理。下面的代码段生成三个特征列。前来那个个特征那个列简单地识别特征名和类型。第三个特征那个列指定一个lambda程序,伸缩原始数据。

# Define three numeric feature columns.
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)

3.实例化相关pre-made Estimator.

# Instantiate an estimator, passing the feature columns.
estimator = tf.estimator.LinearClassifier(
feature_columns=[population, crime_rate, median_education],
)

4.调用训练,评估,或预测方法。

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

3. 自定义Estimators

无论是pre-made还是自定义Estimators,核心都是模型函数(建立图,用于训练,评估和预测),pre-made Estimators已经实现了这些。自定义Estimators需要自己实现。

推荐工作流

1.假设一个合适的pre-made Estimator存在,使用它建立你的地一个模型,使用结果建立baseline.

2.建立和测试你的整个管道,包括使用pre-made Estimator的整个代码的完整性和可靠性。

3.如果合适的可供替代的pre-made Estimators存在,运行实验,决定哪个pre-made Estimator产生最好的结果。

4.改善你的代码,建立自己的自定义Estimator.

4.  从Keras模型中生成Estimators

可以将存在的Keras模型转换成Estimators。这样可以使你的Keras模型拥有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. Note the initial model
# state of the keras model is preserved in the created Estimator.
est_inception_v3 = tf.keras.estimator.model_to_estimator(keras_model=keras_inception_v3) # Treat the derived Estimator as you would with any other Estimator.
# First, recover the input name(s) of Keras model, so we can use them as the
# feature column name(s) of the Estimator input function:
keras_inception_v3.input_names # print out: ['input_1']
# Once we have the input name(s), we can create the input function, for example,
# for input(s) in the format of numpy ndarray:
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"input_1": train_data},
y=train_labels,
num_epochs=1,
shuffle=False)
# To train, we call Estimator's train function:
est_inception_v3.train(input_fn=train_input_fn, steps=2000)

最新文章

  1. 利用Python进行数据分析 基础系列随笔汇总
  2. Nova PhoneGap框架 第五章 文件结构
  3. JS重载
  4. RobotFramework自动化测试之脚本编写(一)
  5. thinkphp3.2.3批量执行sql语句(带事务)
  6. SpringAOP之静态代理
  7. 《zw版·Halcon-delphi系列原创教程》 只有2行代码的超市收款单ocr脚本
  8. js不是从上到下执行的吗?
  9. 获取发布版SHA1获取
  10. transactional replication 的immediate_sync属性
  11. NOIP 2014 Day1 T3飞扬的小鸟
  12. HDU 5682/BestCoder Round #83 1003 zxa and leaf 二分+树
  13. 程序员书单_java web编程篇
  14. ArrayList和Iterator的用法
  15. hdu 5067 Harry And Dig Machine (状态压缩dp)
  16. hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
  17. linux中的strings命令简介
  18. MVC上传文件受限制
  19. HDU-1301 Jungle Roads(最小生成树[Prim])
  20. QT 操作 excel 教程

热门文章

  1. Rest API 开发 学习笔记
  2. linux多线程编程基本操作(2)
  3. swiper插件使用技巧
  4. mysql数据去重复distinct、group by
  5. Openresty最佳案例 | 第6篇:OpenResty连接Mysql
  6. c#冒泡排序算法和快速排序算法
  7. vue中的$on,$emit,$once,$off源码实现
  8. 『ACM C++』PTA浙大 | 基础题 - 打印沙漏
  9. C# register global hotkey ,onekey 注册多个全局热键以及单个全局热键
  10. Ansible自动化配置详解