如何使用Python实例化Tensorflow的估计器?
通过使用Tensorflow库中‘estimator’类中存在的‘DNNClassifier’方法,可以使用Tensorflow实例化估计器。
更多Python相关文章,请阅读:Python 教程
我们将使用Keras Sequential API,该API有助于构建用于处理纯层堆叠的顺序模型,其中每个层都具有一个输入张量和一个输出张量。
至少包含一层的神经网络称为卷积层。我们可以使用卷积神经网络来构建学习模型。
TensorFlow Text包含了与TensorFlow 2.0一起使用的文本相关类和操作的集合。可以使用TensorFlow Text进行序列建模前处理。
我们正在使用Google Colaboratory来运行以下代码。Google Colab或Colaboratory在浏览器上运行Python代码,不需要任何配置,并免费使用GPU(图形处理单元)。Colaboratory是基于Jupyter Notebook构建的。
估计器是TensorFlow的完整模型的高级表示形式。它旨在实现易于扩展和异步培训。
该模型是使用鸢尾花数据集进行训练的。
示例
print("Build a DNN that has 2 hidden layers with 30 and 10 hidden nodes each")
classifier = tf.estimator.DNNClassifier(
feature_columns=my_feature_columns,
hidden_units=[30, 10],
n_classes=3)
代码来源 – https://www.tensorflow.org/tutorials/estimator/premade#first_things_first
输出结果
Build a DNN that has 2 hidden layers with 30 and 10 hidden nodes each
INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpdh8866zb
INFO:tensorflow:Using config: {'_model_dir': '/tmp/tmpdh8866zb', '_tf_random_seed': None, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_save_checkpoints_secs': 600, '_session_config': allow_soft_placement: true
graph_options {
rewrite_options {
meta_optimizer_iterations: ONE
}
}
, '_keep_checkpoint_max': 5, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_train_distribute': None, '_device_fn': None, '_protocol': None, '_eval_distribute': None, '_experimental_distribute': None, '_experimental_max_worker_delay_secs': None, '_session_creation_timeout_secs': 7200, '_checkpoint_save_graph_def': True, '_service': None, '_cluster_spec': ClusterSpec({}), '_task_type': 'worker', '_task_id': 0, '_global_id_in_cluster': 0, '_master': '', '_evaluation_master': '', '_is_chief': True, '_num_ps_replicas': 0, '_num_worker_replicas': 1}
解释
- Iris问题被视为分类问题。
- Tensorflow带有许多预制的分类器Estimators,包括 −
- tf.estimator.DNNClassifier用于执行多类分类的深度模型。
- tf.estimator.DNNLinearCombinedClassifier用于宽深度模型。
- tf.estimator.LinearClassifier用于基于线性模型的分类器。
- 对于Iris问题,我们使用tf.estimator。
极客教程