tf.estimator API技术手册(14)——LinearRegressor实践

tf.estimator API技术手册(14)——LinearRegressor实践

  • (一)前 言
  • (二)构建Estimator
    • (1)定义数据输入函数
    • (2)构建特征列
    • (3)创建estimator
    • (4)开始训练
    • (5)进行评估
    • (6)进行预测
  • (三)总 结

(一)前 言

今天这一节,我们将会展示使用tf.estimator API中的LinearRegressor(线性回归器)的详细流程,并使用这个回归器来预测鲍鱼的年龄。

(二)构建Estimator

(1)定义数据输入函数

def input_training_fn():
    training_data = np.load('npz数据文件')
    features = training_data['feature']
    labels = training_data['label'].reshape(4177, 1)
    features_placeholder = tf.placeholder(features.dtype, features.shape)
    labels_placeholder = tf.placeholder(labels.dtype, labels.shape)
    # 创建dataset
    training_dataset = tf.data.Dataset.from_tensor_slices((features_placeholder,
                                                           labels_placeholder))
    # 设置每批次送入2个数据
    training_dataset = training_dataset.batch(2)
    # 创建可初始化迭代器
    iterator = training_dataset.make_initializable_iterator()
    next_element = iterator.get_next()
    
    with tf.Session() as sess:   
        sess.run(iterator.initializer, feed_dict = {features_placeholder: features,
                                                    labels_placeholder: labels})
        while True:
            try:
                value, label = sess.run(next_element)
                feature_dict = {'x':value}
                # 返回特征和标签
                return feature_dict, label
            except tf.errors.OutOfRangeError :
                break

(2)构建特征列

# 数据集含有8个属性的数据
feature = tf.feature_column.numeric_column('feature', shape=[8])

(3)创建estimator

estimator = tf.estimator.LinearRegressor(feature_columns=[feature],                                        
                                         model_dir="存放模型的位置" 
                                          )

(4)开始训练

# 训练10000次
estimator.train(input_fn = input_training_fn, max_steps=10000)
# 输 出:
···
INFO:tensorflow:global_step/sec: 2088.91
INFO:tensorflow:loss = 0.1643793, step = 9501 (0.049 sec)
INFO:tensorflow:global_step/sec: 2088.86
INFO:tensorflow:loss = 0.1567634, step = 9601 (0.047 sec)
INFO:tensorflow:global_step/sec: 2179.78
INFO:tensorflow:loss = 0.14950174, step = 9701 (0.047 sec)
INFO:tensorflow:global_step/sec: 2133.34
INFO:tensorflow:loss = 0.14257516, step = 9801 (0.046 sec)
INFO:tensorflow:global_step/sec: 2133.28
INFO:tensorflow:loss = 0.13597059, step = 9901 (0.047 sec)
INFO:tensorflow:Saving checkpoints for 10000 into C:/Users/12394/PycharmProjects/ODTEST/test\model.ckpt.
INFO:tensorflow:Loss for final step: 0.1297334.

(5)进行评估

为了方便起见,我们这还是使用训练数据输入的函数去评估

estimator.evaluate(
    input_fn = input_training_fn,
    # 设定评估步数
    steps=1000,
    hooks=None,
    checkpoint_path="模型存储目录,后面跟检查点文件/model.ckpt-10000",
    name=None
      )
# 输 出:
···
INFO:tensorflow:Restoring parameters from C:/Users/12394/PycharmProjects/ODTEST/test/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-05:27:49
INFO:tensorflow:Saving dict for global step 10000: average_loss = 0.06483548, global_step = 10000, label/mean = 11.0, loss = 0.12967096, prediction/mean = 11.022752
INFO:tensorflow:Saving 'checkpoint_path' summary for global step 10000: C:/Users/12394/PycharmProjects/ODTEST/test/model.ckpt-10000

(6)进行预测

# 构建评估数据
data = np.load('npz数据文件')
value = data['feature']
x_dict = {'x': value}
test_set = tf.estimator.inputs.numpy_input_fn(
     x=x_dict,
     y=None,
     batch_size=100,
     num_epochs=1,
     shuffle=False,
     queue_capacity=1000,
     num_threads=1
     )
# 开始预测
pre = estimator.predict(
        input_fn=test_set, 
        checkpoint_path="模型存储目录,后面跟检查点文件/model.ckpt-10000"
        )
for item in pre:
    print(item)
# 输 出:
···
{'predictions': array([11.082258], dtype=float32)}
{'predictions': array([11.025646], dtype=float32)}
{'predictions': array([17.345379], dtype=float32)}
{'predictions': array([17.802246], dtype=float32)}
{'predictions': array([16.360329], dtype=float32)}
{'predictions': array([16.919731], dtype=float32)}
···

(三)总 结

在这一节中我们完成了tf.estimator.LinearRegressor的实践操作,有任何的问题请在评论区留言,我会尽快回复,谢谢支持!

你可能感兴趣的:(tf.estimator,API技术手册)