Tensorflow.Estimators笔记 - 介绍

网友投稿 716 2022-05-30

一、简介

estimators是tensorFlow的机器学习高阶API:

包括四个部分:

1.1、训练

1.2、评估

1.3、预测

1.4、服务导出

该api基于类:tf.estimator.Estimator.

二、优势

estimator有以下优势:

2.1、基于estimator的模型既可以在本地运行,也可以在分布式环境中运行,可以不修改代码同时在CPU、TPU、GPU服务器上运行

2.2、轻便的实现模型开发者之间的共享

2.3、更简单的时间模型

2.4、estimator是简化订制在tf.layers

2.5、自动图像化

2.6、提供安全的训练循环:·图像化、·内置变量、·启动队列、·异常管理、·从错误中创建和删除测点文件

三、pre-made(预制)

预制能够让开发者从更高层面工作。预制estimator为开发者创造和管理图和会话对象。

3.1、预制estimators程序结构

3.1.1 写一个或多个数据集导入函数(返回一个特征数据字典、一个包含标签的tensor)

def input_fn(dataset):

...  # manipulate dataset, extracting the feature dict and the label

return feature_dict, label

3.1.2 定义特征列

每一个tf.feature_column定义特征名、类型和任何输入预处理,举三个例子:

Tensorflow.Estimators笔记 - 介绍

# 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.1.3 实例化预制estimator

举 LinearClassifier的例子:

# Instantiate an estimator, passing the feature columns.

estimator = tf.estimator.LinearClassifier(

feature_columns=[population, crime_rate, median_education],

)

3.1.4调用训练、评价、生成方法:

举train方法:

# my_training_set is the function created in Step 1

estimator.train(input_fn=my_training_set, steps=2000)

3.2、预制评估器的优势

指明计算图在计算机和计算集群中何处运行的最佳实践

普遍有用的事件总结最佳实践

四、订制评估器

订制评估器和预制评估器的核心,都是模型函数。

五、工作流推荐

5.1 假设合适的预制评估器存在,用它来构造第一个模型并建立基准线

5.2 构造和测试全局管道,包括对这个预制评估器的数据完整性和可靠性

5.3 对预制评估器做适当的修改,使他能够产生最佳结果

5.4 如果可以的话,建立自己的模型

六、从Keras models建立评估器

举例调用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)

TensorFlow

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:从零认识云计算(一)——什么是云计算
下一篇:人工智能与大数据、云计算、物联网的关系
相关文章