如何使用Estimator和Tensorflow从训练模型中进行预测?

如何使用Estimator和Tensorflow从训练模型中进行预测?

Tensorflow可以与Estimator一起使用,使用“分类器”方法中存在的“predict”方法来预测新数据上的输出。

更多Python相关文章,请阅读:Python 教程

我们将使用Keras Sequential API,它有助于构建顺序模型,用于处理仅具有一个输入张量和一个输出张量的纯层堆栈。

至少包含一个层的神经网络称为卷积层。我们可以使用卷积神经网络构建学习模型。

TensorFlow Text 包含了可与 TensorFlow 2.0 一起使用的一组文本相关类和操作。TensorFlow文本可用于预处理序列建模。

我们使用Google Colaboratory来运行下面的代码。Google Colab或Colaboratory可在浏览器上运行Python代码,无需任何配置,并且可以免费访问GPU(图形处理单元)。Colaboratory已构建在Jupyter Notebook之上。

Estimator是TensorFlow完整模型的高级表示。它旨在实现易于扩展且异步训练。

使用鸢尾花数据集训练模型。有4个特征和一个标签。

  • 萼片长度
  • 萼片宽度
  • 花瓣长度
  • 花瓣宽度

例子

print(“Generating predictions from model”)
expected = ['Setosa', 'Versicolor', 'Virginica']
predict_x = {
   'SepalLength': [5.1, 5.9, 6.9],
   'SepalWidth': [3.3, 3.0, 3.1],
   'PetalLength': [1.7, 4.2, 5.4],
   'PetalWidth': [0.5, 1.5, 2.1],
}
print(“Defining input function for prediction”)
print(“It converts inputs to dataset without labels”)
def input_fn(features, batch_size=256):
return tf.data.Dataset.from_tensor_slices(dict(features)).batch(batch_size)
predictions = classifier.predict(
   input_fn=lambda: input_fn(predict_x))

代码来源: https://www.tensorflow.org/tutorials/estimator/premade#first_things_first

输出

Generating predictions from model
Defining input function for prediction
It converts inputs to dataset without labels

说明

  • 训练模型将产生良好的结果。
  • 这可以用于根据某些未标记的测量预测鸢尾花的物种。
  • 使用单个函数调用进行预测。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程