一、简介
1、环境配置
2、模块化编程(名词作用域与抽象节点)
二、详介
1、环境配置
(1)、Anaconda虚拟环境
conda env list # 显示已存在的虚拟环境
virtualenv --system-site-packages -p python3.6 ./py3(Linux)
conda create -n your_env_name python=3.6 # 创建虚拟环境
source activate your_env_name(Linux)
activate your_env_name(Windows) # 激活虚拟环境
source deactivate(Linux)
deactivate(Windows) # 退出虚拟环境
(2)、设置国内镜像
# 添加Anaconda的TUNA镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
# 设置搜索时显示通道地址
conda config --set show_channel_urls yes
(3)、第三方库安装
pip list installed # 显示已安装的第三方库
pip install tensorflow==1.12 jupyter matplotlib pandas seaborn numpy
2、模块化编程(名词作用域与抽象节点)
(1)、数据处理:获取X和y
import pandas as pd
import numpy as np
def normalize_feature(df):
return df.apply(lambda column: (column - column.mean()) / column.std())
df = normalize_feature(pd.read_csv('data1.csv',
names=['square', 'bedrooms', 'price']))
ones = pd.DataFrame({'ones': np.ones(len(df))})# ones是n行1列的数据框,表示x0恒为1
df = pd.concat([ones, df], axis=1) # 根据列合并数据
df.head()
X_data = np.array(df[df.columns[0:3]])
y_data = np.array(df[df.columns[-1]]).reshape(len(df), 1)
print(X_data.shape, type(X_data))
print(y_data.shape, type(y_data))
(2)、创建线性回归模型(数据流图)
import tensorflow as tf
alpha = 0.01 # 学习率 alpha
epoch = 500 # 训练全量数据集的轮数
with tf.name_scope('input'):
# 输入 X,形状[47, 3]
X = tf.placeholder(tf.float32, X_data.shape, name='X')
# 输出 y,形状[47, 1]
y = tf.placeholder(tf.float32, y_data.shape, name='y')
with tf.name_scope('hypothesis'):
# 权重变量 W,形状[3,1]
W = tf.get_variable("weights",
(X_data.shape[1], 1),
initializer=tf.constant_initializer())
# 假设函数 h(x) = w0*x0+w1*x1+w2*x2, 其中x0恒为1
# 推理值 y_pred 形状[47,1]
y_pred = tf.matmul(X, W, name='y_pred')
with tf.name_scope('loss'):
# 损失函数采用最小二乘法,y_pred - y 是形如[47, 1]的向量。
# tf.matmul(a,b,transpose_a=True) 表示:矩阵a的转置乘矩阵b,即 [1,47] X [47,1]
# 损失函数操作 loss
loss_op = 1 / (2 * len(X_data)) * tf.matmul((y_pred - y), (y_pred - y), transpose_a=True)
with tf.name_scope('train'):
# 随机梯度下降优化器 opt
train_op = tf.train.GradientDescentOptimizer(learning_rate=alpha).minimize(loss_op)
with tf.Session() as sess:
# 初始化全局变量
sess.run(tf.global_variables_initializer())
# 创建FileWriter实例,并传入当前会话加载的数据流图
writer = tf.summary.FileWriter('./summary/linear-regression-1', sess.graph)
# 记录所有损失值
loss_data = []
# 开始训练模型
# 因为训练集小,所以每轮都使用全量数据训练
for e in range(1, epoch + 1):
_, loss, w = sess.run([train_op, loss_op, W], feed_dict={X: X_data, y: y_data})
# 记录每一轮损失值变化情况
loss_data.append(float(loss))
if e % 100 == 0:
log_str = "Epoch %d \t Loss=%.4g \t Model: y=%.4gx1 + %.4gx2 + %.4g"
print(log_str % (e, loss, w[1], w[2], w[0]))
# 关闭FileWriter的输出流
writer.close()
(3)、可视化数据流图
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(context="notebook", style="whitegrid", palette="dark")
ax = sns.lineplot(x='epoch', y='loss', data=pd.DataFrame({'loss': loss_data, 'epoch': np.arange(epoch)}))
ax.set_xlabel('epoch')
ax.set_ylabel('loss')
plt.show()
*、资源
1、代码地址