LinearRegressor的基本格式

初始化

# 定义线性回归器
linear_regressor = tf.estimator.LinearRegressor(feature_columns=feature_columns,  # 自定义的特征列optimizer=my_optimizer  # 选择合适的优化函数
)

训练

linear_regressor.train(input_fn=lambda: my_input_fn(my_feature, targets),  # 输入函数喂取数据steps=100  # 训练的步数
)

预测

# 预测数据
predictions = linear_regressor.predict(input_fn=lambda: my_input_fn(my_feature, targets, num_epochs=1, shuffle=False)
)

预测的格式:

{
'predictions': array([0.34],dtype=float32)
}

是一个字典类型的数据,与之前的DNNClassifier不同,这个仅仅是输出了每一标签对应的输出值,每一项是一个标量。而神经网络的更加复杂一些。。。

配合Dataset读取数据

之前的神经网络分类器DNNClassifier使用了随机乱序的输入,这里的方式类似,不过使用了数据迭代的方式进行:

def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):'''输入函数,用于向回归器喂取数据:param features: 特征列:param labels: 标签:param targets: 目标值:param batch_size: batch size:param shuffle: 是否乱序:param num_epochs: epochs的数量:return: 数据和标签'''# 把pandas数据转换成dict的array类型,每一个# features = {key: np.array(value) for key, value in dict(features).items()}ds = Dataset.from_tensor_slices((dict(features), targets))ds = ds.batch(batch_size).repeat(num_epochs)if shuffle:ds.shuffle(10000)# 迭代方式,每次返回一个批次的数据features, labels = ds.make_one_shot_iterator().get_next()return features, labels

使用make_one_shot_iterator().get_next()方法,每次yeild数据,直到训练步数完成。

完整版代码:

说明:这里的线性回归只是一个测试的例子,如果观察绘制的图像或者最小二乘损失的话,误差会很大的。不过,这仅仅是为了说明一般的使用方法。。忽略掉误差吧。。。。。。。

import math
import os
from IPython import display
from matplotlib import cm
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
import tensorflow as tf
from tensorflow.python.data import Datasettf.logging.set_verbosity(tf.logging.INFO)
pd.options.display.max_rows = 10
# pd.options.display.float_format = '{:.1f}'.format()# 读取数据
if not os.path.exists('data.csv'):california_housing_dataframe = pd.read_csv("https://storage.googleapis.com/mledu-datasets/california_housing_train.csv", sep=",")
else:california_housing_dataframe = pd.read_csv('data.csv')# 数据乱序操作
california_housing_dataframe = california_housing_dataframe.reindex(np.random.permutation(california_housing_dataframe.index))# 对median_house_value数据列进行标量化处理
california_housing_dataframe['median_house_value'] /= 1000.0
print(california_housing_dataframe.describe())# 确定标签、标签列、目标结果
my_feature = california_housing_dataframe[['total_rooms']]
feature_columns = [tf.feature_column.numeric_column(key='total_rooms')]
targets = california_housing_dataframe['median_house_value']# 设置梯度下降函数,同时设置最大下降范围,防止梯度爆炸
my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.0000001)
my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)# 定义线性回归器
linear_regressor = tf.estimator.LinearRegressor(feature_columns=feature_columns,optimizer=my_optimizer
)def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):'''输入函数,用于向回归器喂取数据:param features: 特征列:param labels: 标签:param targets: 目标值:param batch_size: batch size:param shuffle: 是否乱序:param num_epochs: epochs的数量:return: 数据和标签'''# 把pandas数据转换成dict的array类型,每一个# features = {key: np.array(value) for key, value in dict(features).items()}ds = Dataset.from_tensor_slices((dict(features), targets))ds = ds.batch(batch_size).repeat(num_epochs)if shuffle:ds.shuffle(10000)# 迭代方式,每次返回一个批次的数据features, labels = ds.make_one_shot_iterator().get_next()return features, labels# 开始训练
linear_regressor.train(input_fn=lambda: my_input_fn(my_feature, targets),steps=100
)# 预测数据
predictions = linear_regressor.predict(input_fn=lambda: my_input_fn(my_feature, targets, num_epochs=1, shuffle=False)
)predictions = np.array([item['predictions'][0] for item in predictions])MSE = metrics.mean_squared_error(predictions, targets)
RMSE = math.sqrt(MSE)
print("Mean Squared Error (on training data): %0.3f" % MSE)
print("Root Mean Squared Error (on training data): %0.3f" % RMSE)min_house_value = california_housing_dataframe["median_house_value"].min()
max_house_value = california_housing_dataframe["median_house_value"].max()
min_max_difference = max_house_value - min_house_valueprint("Min. Median House Value: %0.3f" % min_house_value)
print("Max. Median House Value: %0.3f" % max_house_value)
print("Difference between Min. and Max.: %0.3f" % min_max_difference)
print("Root Mean Squared Error: %0.3f" % RMSE)calibration_data = pd.DataFrame()
calibration_data["predictions"] = pd.Series(predictions)
calibration_data["targets"] = pd.Series(targets)
print(calibration_data.describe())sample = california_housing_dataframe.sample(n=300)# 获取最大最小值
x_0 = sample["total_rooms"].min()
x_1 = sample["total_rooms"].max()# 获取权重和偏置项
weight = linear_regressor.get_variable_value('linear/linear_model/total_rooms/weights')[0]
bias = linear_regressor.get_variable_value('linear/linear_model/bias_weights')# 获取有关的数据.
y_0 = weight * x_0 + bias
y_1 = weight * x_1 + bias# 追至直线
plt.plot([x_0, x_1], [y_0, y_1], c='r')# 坐标的名称
plt.ylabel("median_house_value")
plt.xlabel("total_rooms")# 画出图的分散点
plt.scatter(sample["total_rooms"], sample["median_house_value"])# Display graph.
plt.show()

Tensorflow Estimator之LinearRegressor相关推荐

  1. TensorFlow estimator训练时 invalid continuation byte

    ** TensorFlow estimator训练时 报错:invalid continuation byte ** 报错:UnicodeDecodeError: 'utf-8' codec can' ...

  2. Tensorflow Estimator学习

    前沿 Tensorflow Estimator 最近在公司接触到, 也算是只有到工业界才会频繁接触的一种框架语言吧. 因为在工业界需要大数据和分布式处理,而本身它就是一种支持分布式训练和部署tenso ...

  3. TensorFlow Estimator 官方文档之----Feature column

    Feature column 本文档详细介绍了特征列(feature columns).您可以将特征列视为原始数据和 Estimator 之间的媒介.特征列非常丰富,使您可以将各种原始数据转换为 Es ...

  4. Tensorflow Estimator之DNNClassifier

    DNNClassifier的基本格式 初始化: feature_columns作为特征列,但是这里不添加数据,仅仅是使用tf.feature_column添加数据特征:数据特征相当于一个字典的键值,这 ...

  5. TensorFlow Estimator 官方文档之----内置Estimator

    Premade Estimators 本文档介绍了 TensorFlow 编程环境,并展示了怎么用 Premade Estimators 来解决 Iris 分类问题. 文章目录 1. TensorFl ...

  6. TensorFlow Estimator 教程之----快速入门

    TensorFlow 版本:1.10.0 > Guide > Introduction to Estimators Estimator 概述 本篇将介绍 TensorFlow 中的 Est ...

  7. tensorflow estimator详细介绍,实现模型的高效训练

    estimator是tensorflow高度封装的一个类,里面有一些可以直接使用的分类和回归模型,例如tf.estimator.DNNClassifier,但这不是这篇博客的主题,而是怎么使用esti ...

  8. tensorflow estimator 实践

    本文以mnist数据集为例.estimator通常是和tf的dataset一起使用,故先制作tfrecord文件,在使用estimator进行测试. 文章结构: 1.文件目录 2. 制作tfrecor ...

  9. TensorFlow estimator详解

    1.框架 Estimator是属于High level的API Mid-level API分别是 -- Layers:用来构建网络结构.Datasets: 用来构建数据读取pipeline.Metri ...

最新文章

  1. pythontcp服务器如何关闭阻塞_python实现单线程多任务非阻塞TCP服务端
  2. shell脚本判断进程是否运行
  3. 研发手Q推广遇到的一系列问题
  4. idea怎么打包有依赖关系的项目_项目需求不明确,项目各模块逻辑关系不清晰,怎么排计划?...
  5. .NET Core MVC扩展实践
  6. php post修改字段,单个{customposttype}.php中的函数wp_insert_post()清除自定义字段
  7. 埋点是什么意思_(一百二十二)埋点方案举例,如何做埋点方案
  8. nodejs生成UID(唯一标识符)——node-uuid模块
  9. top结合jstack处理线上cpu飙升问题
  10. android自定义pickerview,一个非常好用的Android PickerView库
  11. 尚硅谷vue基础笔记
  12. mysql怎么安装安全补丁_讲解SQL Server安装sp4补丁报错的解决方法_MySQL
  13. 电子信息产业发展研究院副主任杨春立:基于数字孪生的智慧城市顶层设计探索与实践...
  14. 你养狗的方法够科学吗?
  15. audio接线图解_图文:主板跳线(排线)连接技巧HD AUDIO连线接法
  16. contents属性
  17. mytrader-开源股票期货金融软件+支持C/C++/Python/Excel/VBA/麦语言的量化分析交易平台
  18. 在c语言求30角的正弦值,正弦及30度角的正弦值.doc
  19. python对numpy数组求导_NumPy数组计算——python
  20. ImageJ 用户手册——第三部分(ImageJ扩展)

热门文章

  1. TensorFlow:tensorflow之CIFAR10与ResNet18实战
  2. 深度学习入门笔记(五):神经网络的学习
  3. CAD(计算机辅助设计)
  4. 编程实现 有符号乘法溢出判断
  5. 线性筛法求质数分解、欧拉函数
  6. 在eclipse中使用第三方库总结
  7. IO流 (一) ----- 基本概念和FIle类
  8. CSDN markdown编辑器 页面内跳转目录
  9. Programming in the Mid-Future(转)
  10. 安装Jaspersoft Studio