Google TensorFlow课程 编程笔记(2)———使用TensorFlow的基本步骤

使用 TensorFlow 的基本步骤


学习目标:在 TensorFlow 中使用 LinearRegressor 类并基于单个输入特征预测各城市街区的房屋价值中位数,使用均方根误差 (RMSE) 评估模型预测的准确率,通过调整模型的超参数提高模型准确率。

设置

加载必要的库+数据集+进行必要的处理

 

import math
from IPython import display    # display模块可以决定显示的内容以何种格式显示
from matplotlib import cm    # matplotlib为python的2D绘图库# cm为颜色映射表
from matplotlib import gridspec    # 使用 GridSpec 自定义子图位置
from matplotlib import pyplot as plt    # pyplot提供了和matlab类似的绘图API,方便用户快速绘制2D图表
import numpy as np    # numpy为python的科学计算包,提供了许多高级的数值编程工具
import pandas as pd    # pandas是基于numpy的数据分析包,是为了解决数据分析任务而创建的 
from sklearn import metrics    # sklearn(scikit-_learn_)是一个机器学习算法库,包含了许多种机器学习得方式
# * Classification 分类# * Regression 回归
# * Clustering 非监督分类# * Dimensionality reduction 数据降维
# * Model Selection 模型选择# * Preprocessing 数据预处理
# metrics:度量(字面意思),它提供了很多模块可以为第三方库或者应用提供辅助统计信息
import tensorflow as tf    # tensorflow是谷歌的机器学习框架
from tensorflow.python.data import Dataset    # Dataset无比强大得数据集

tf.logging.set_verbosity(tf.logging.ERROR)
pd.options.display.max_rows = 10    # 为了观察数据方便,最多只显示10行数据
pd.options.display.float_format = '{:.1f}'.format

california_housing_dataframe = pd.read_csv("https://storage.googleapis.com/mledu-datasets/california_housing_train.csv", sep=",")   
 #加载数据集

california_housing_dataframe = california_housing_dataframe.reindex(
    np.random.permutation(california_housing_dataframe.index))
california_housing_dataframe["median_house_value"] /= 1000.0
california_housing_dataframe    #对数据进行预处理将median_house_value调整为以千为单位。

california_housing_dataframe.describe()    #检查数据

 

设构建第一个模型

第1步:定义特征并配置特征列

 

my_feature = california_housing_dataframe[["total_rooms"]]# Define the input feature: total_rooms.
# 取数据集中得'total_rooms'这一列作为输入特征

# Configure a numeric feature column for total_rooms.
# 将一个名叫"total_rooms"的特征列定义为**数值数据** ,这样的定义结果存在feature_columns中
# 即上文所说得**特征列**中,这时候特征列其实只是一个存储了分类信息的集合,具体使用的时候需要# 特征集合和特征列结合起来,分类器才能识别的呢
feature_columns = [tf.feature_column.numeric_column("total_rooms")]

 

第2步:定义目标

# Define the label.# 将"median_house_value"列的数据从数据集中取出作为target,定义我们学习的目标
targets = california_housing_dataframe["median_house_value"]

 

第3步:配置LinearRegressor

 

# Use gradient descent as the optimizer for training the model.
my_optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.0000001)    
my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(my_optimizer, 5.0)
# 这里的clip_by_norm是指对梯度进行裁剪,通过控制梯度的最大范式,防止梯度爆炸的问题,是一种
# 比较常用的梯度规约的方式。

linear_regressor = tf.estimator.LinearRegressor(
feature_columns=feature_columns,
optimizer=my_optimizer
)
# Configure the linear regression model with our feature columns and optimizer
# Set a learning rate of 0.0000001 for Gradient Descent.# 线性回归模型,tf.estimator.LinearRegressor是tf.estimator.Estimator的子类
# 传入参数为**特征**列和刚才配置的**优化器**,至此线性回归模型就配置的差不多啦
# 前期需要配置模型,所以是与具体数据(特征值,目标值是无关的)

第4步:定义输入函数

 

def my_input_fn(features, targets, batch_size=1, shuffle=True, num_epochs=None):
# 自定义个输入函数
# 输入的参数分别为
# features:特征值(房间数量)
# targets: 目标值(房屋价格中位数)
# b
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值