estimator同keras是tensorflow的高级API。在tensorflow1.13以上,estimator已经作为一个单独的package从tensorflow分离出来了。
estimator抽象了tensorflow底层的api, 同keras一样,他分离了model和data, 不同于keras这个不得不认养的儿子,estimator作为tensorflow的亲儿子,天生具有分布式的基因,更容易在生产环境里面使用

tensorflow官方文档提供了比较详细的estimator程序的构建过程:
https://www.tensorflow.org/guide#estimators

tensorflow model提供了estimator构建的mnist程序:
https://github.com/tensorflow/models/blob/master/official/mnist/mnist.py

estimator模型由model_fn决定:
官方文档:

其中features, labels是必需的。model, params, config参数是可选的
如下是estiamtor定义的一个模型:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def (features, labels, mode, params):
"""DNN with three hidden layers and learning_rate=0.1.""" net = tf.feature_column.input_layer(features, params['feature_columns'])
for units in params['hidden_units']:
net = t 大专栏  tf.estimatorf.layers.dense(net, units=units, activation=tf.nn.relu) # Compute logits (1 per class).
logits = tf.layers.dense(net, params['n_classes'], activation=None) # Compute predictions.
predicted_classes = tf.argmax(logits, 1)
if mode == tf.estimator.ModeKeys.PREDICT:
predictions = {
'class_ids': predicted_classes[:, tf.newaxis],
'probabilities': tf.nn.softmax(logits),
'logits': logits,
}
return tf.estimator.EstimatorSpec(mode, predictions=predictions) # Compute loss.
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits) # Compute evaluation metrics.
accuracy = tf.metrics.accuracy(labels=labels,
predictions=predicted_classes,
name='acc_op')
metrics = {'accuracy': accuracy}
tf.summary.scalar('accuracy', accuracy[1]) if mode == tf.estimator.ModeKeys.EVAL:
return tf.estimator.EstimatorSpec(
mode, loss=loss, eval_metric_ops=metrics) # Create training op.
assert mode == tf.estimator.ModeKeys.TRAIN optimizer = tf.train.AdagradOptimizer(learning_rate=0.1)
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)

可以看见features, labels分别是模型的input,output。所以是必须的。但是在有的模型里面。比如bert预训练的模型里面,我们不需要训练,所以只用到features。

tf.estimator的更多相关文章

  1. 机器学习笔记5-Tensorflow高级API之tf.estimator

    前言 本文接着上一篇继续来聊Tensorflow的接口,上一篇中用较低层的接口实现了线性模型,本篇中将用更高级的API--tf.estimator来改写线性模型. 还记得之前的文章<机器学习笔记 ...

  2. import tensorflow 报错: tf.estimator package not installed.

    import tensorflow 报错: tf.estimator package not installed. 解决方案1: 安装 pip install tensorflow-estimator ...

  3. tf.estimator.Estimator类的用法

    官网链接:https://www.tensorflow.org/api_docs/python/tf/estimator/Estimator Estimator - 一种可极大地简化机器学习编程的高阶 ...

  4. tf.estimator.Estimator

    1.定义 tf.estimator.Estimator(model_fn=model_fn) #model_fn是一个方法 2.定义model_fn: def model_fn_builder(sel ...

  5. 启动Tensorboard时发生错误:class BeholderHook(tf.estimator.SessionRunHook): AttributeError: module 'tensorflow.python.estimator.estimator_lib' has no attribute 'SessionRunHook'

    报错:class BeholderHook(tf.estimator.SessionRunHook):AttributeError: module 'tensorflow.python.estimat ...

  6. tensorflow estimator API小栗子

    TensorFlow的高级机器学习API(tf.estimator)可以轻松配置,训练和评估各种机器学习模型. 在本教程中,您将使用tf.estimator构建一个神经网络分类器,并在Iris数据集上 ...

  7. tensorflow创建自定义 Estimator

    https://www.tensorflow.org/guide/custom_estimators?hl=zh-cn 创建自定义 Estimator 本文档介绍了自定义 Estimator.具体而言 ...

  8. Tensorflow1.4 高级接口使用(estimator, data, keras, layers)

    TensorFlow 高级接口使用简介(estimator, keras, data, experiment) TensorFlow 1.4正式添加了keras和data作为其核心代码(从contri ...

  9. TensorFlow 1.4利用Keras+Estimator API进行训练和预测

    Tensorflow 1.4中,Keras作为作为核心模块可以直接通过tf.keas进行调用,但是考虑到keras对tfrecords文件进行操作比较麻烦,而将keras模型转成tensorflow中 ...

随机推荐

  1. php错误和异常的重定向

    通过重定向错误或异常,我们可以更安全的显示错误信息,一般也用来记录错误和异常日志. 参数可以是全局函数名,也可以是类中的方法,非静态方法通过数组传递类名和方法名进去, 静态方法直接带命名空间和类名,看 ...

  2. aws ec2 安装Elastic search 7.2.0 kibana 并配置 hanlp 分词插件

    文章大纲 Elastic search & kibana & 分词器 安装 版本控制 下载地址 Elastic search安装 kibana 安装 分词器配置 Elastic sea ...

  3. python语法基础-并发编程-进程-进程锁和进程间通信

    ###############   守护进程  ############## """ 守护进程 父进程中将一个子进程设置为守护进程,那么这个子进程会随着主进程的结束而结束 ...

  4. 面向对象 part2 属性的特性

    6.1.1理解对象 创建自定义对象最简单的方式就是创建一个object实例.然后添加方法和实例 var person = new Object() person.name = "hi&quo ...

  5. mysql引擎与物理文件

    SELECT VERSION();show GLOBAL VARIABLES like '%PARTITION%';-- 查看分区情况 show GLOBAL VARIABLES like '%dat ...

  6. jedis异常Broken pipe (Write failed)

    异常:java.net.SocketException: Broken pipe (Write failed); nested exception is redis.clients.jedis.exc ...

  7. sqlite如何避免重复建表(获取已经存在的表)

    找到已经存在的所有表,手动判断是否需要建表 SELECT name FROM SQLITE_MASTER WHERE type='table'ORDER BY name" 建表时sqlite ...

  8. Flink(二) —— 部署与任务提交

    一.下载&启动 官网上下载安装包,执行下列命令即启动完成. ./bin/start-cluster.sh 效果图 Flink部署模式 Standalone模式 Yarn模式 k8s部署 二.配 ...

  9. (转)Navicat Premium 12.1.8.0安装与激活

    http://www.mamicode.com/info-detail-2493067.html

  10. Java操作redis客户端Jedis连接集群(Cluster)

    创建JedisCluster类连接redis集群. @Test public void testJedisCluster() throws Exception { //创建一连接,JedisClust ...