今天这一节,我们将会展示使用tf.estimator API中的DNNRegressor(深度神经网络回归器)的详细流程,并使用这个回归器来预测波士顿的房价情况。
FEATURES_KEY = "x"
dataset = tf.data.Dataset.from_tensor_slices(
({FEATURES_KEY: tf.log1p(x_train)}, tf.log1p(y_train)))
dataset = dataset.batch(10)
dataset = dataset.repeat()
iterator = dataset.make_one_shot_iterator()
features, labels = iterator.get_next()
return features, labels
# 数据集含有8个属性的数据
feature = tf.feature_column.numeric_column('feature', shape=[8])
创建一个含有三个隐层的网络,每一层的节点数为[256,512,256]
estimator = tf.estimator.DNNRegressor(
feature_columns = [feature],
hidden_units = [256, 512, 256],
model_dir = '模型保存位置')
训练10000次
estimator.train(input_fn= input_training_fn,
max_steps= 10000)
# 输 出:
···
INFO:tensorflow:global_step/sec: 667.019
INFO:tensorflow:loss = 0.0014092014, step = 9601 (0.160 sec)
INFO:tensorflow:global_step/sec: 623.699
INFO:tensorflow:loss = 0.0013482999, step = 9701 (0.150 sec)
INFO:tensorflow:global_step/sec: 625.334
INFO:tensorflow:loss = 0.0013016658, step = 9801 (0.170 sec)
INFO:tensorflow:global_step/sec: 625.337
INFO:tensorflow:loss = 0.001259842, step = 9901 (0.150 sec)
INFO:tensorflow:Saving checkpoints for 10000 into C:\Users\12394\PycharmProjects\ODTEST\test\model.ckpt.
INFO:tensorflow:Loss for final step: 0.001243635.
为了方便起见,我们这还是使用训练数据输入的函数去评估
# 输 出:
estimator.evaluate(input_fn= input_training_fn,
steps= 1000,
checkpoint_path= "模型存储目录\model.ckpt-10000")
···
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Evaluation [100/1000]
INFO:tensorflow:Evaluation [200/1000]
INFO:tensorflow:Evaluation [300/1000]
INFO:tensorflow:Evaluation [400/1000]
INFO:tensorflow:Evaluation [500/1000]
INFO:tensorflow:Evaluation [600/1000]
INFO:tensorflow:Evaluation [700/1000]
INFO:tensorflow:Evaluation [800/1000]
INFO:tensorflow:Evaluation [900/1000]
INFO:tensorflow:Evaluation [1000/1000]
INFO:tensorflow:Finished evaluation at 2018-11-20-07:14:10
INFO:tensorflow:Saving dict for global step 10000: average_loss = 0.00012076629, global_step = 10000, label/mean = 3.1347558, loss = 0.001207663, prediction/mean = 3.1425884
INFO:tensorflow:Saving 'checkpoint_path' summary for global step 10000: C:\Users\12394\PycharmProjects\ODTEST\test\model.ckpt-10000
def input_test_fn():
FEATURES_KEY = "x"
dataset = tf.data.Dataset.from_tensor_slices(
({FEATURES_KEY: tf.log1p(x_test)}))
dataset = dataset.batch(10)
iterator = dataset.make_one_shot_iterator()
with tf.Session() as sess:
for i in range(10):
features = sess.run(iterator.get_next())
return features
pre = estimator.predict(
input_fn=input_test_fn,
checkpoint_path= "模型存储目录\model.ckpt-10000"
)
pre = list(pre)
print(len(pre))
# 输 出:
···
{'predictions': array([3.2248054], dtype=float32)}
{'predictions': array([3.2263634], dtype=float32)}
{'predictions': array([3.297683], dtype=float32)}
{'predictions': array([3.0244052], dtype=float32)}
{'predictions': array([3.0476892], dtype=float32)}
{'predictions': array([2.5239868], dtype=float32)}
{'predictions': array([3.3280423], dtype=float32)}
{'predictions': array([3.3007944], dtype=float32)}
{'predictions': array([3.318545], dtype=float32)}
{'predictions': array([3.0145156], dtype=float32)}
···
在这一节中我们完成了tf.estimator.DNNRegressor的实践操作,有任何的问题请在评论区留言,我会尽快回复,谢谢支持!