本篇内容:一个简单的预测模型的建立、训练、保存和载入。
导入必要模块:
import numpy as np
import pandas as pd
import tensorflow as tf
import ssl #解决数据源网站签名认证失败的问题
from sklearn.datasets import fetch_california_housing
解决数据源网站签名认证失败的问题:
ssl._create_default_https_context = ssl._create_unverified_context
获取数据并加入bias项:
housing = fetch_california_housing()
housing.drop('ocean_proximity', axis = 1, inplace = True)
m,n = housing.data.shape
housing_with_bias = np.c_[np.ones((m, 1)), housing.data]
数据标准化:
housing_with_bias = sklearn.preprocessing.scale(housing_with_bias)
housing_with_bias = pd.DataFrame(housing_with_bias)
#注意使用sklearn.preprocessing.scale输出的结果为ndarray,可使用pd.DataFrame()转换回DF
设置迭代周期数、学习速率:
n_epochs = 1000
learning_rate = 0.01
添加数据、标签、权重、预测值节点,设置损失函数、优化操作:
X = tf.constant(housing_with_bias, dtype=tf.float32, name = 'X')
y = tf.constant(housing.target.reshape(-1, 1), dtype=tf.float32, name = 'y')
theta = tf.Variable(tf.random_uniform([n+1, 1], -1.0, 1.0), name = 'theta')
y_pred = tf.matmul(X, theta, name = 'predictions')
error = y_pred - y
mse = tf.reduce_mean(tf.square(error), name = 'mse')
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate)
添加初始化节点:
init = tf.global_variables_initializer()
训练:
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
if epoch % 100 == 0:
print('Epoch: ', epoch, 'MSE = ', mse.eval())
sess.run(training_op)
best_theta = theta.eval()
保存模型:
[...]
theta = ...
[...]
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init)
for epoch in range(n_epochs):
if epoch % 100 == 0:
save_path = saver.save(sess, '/.../my_model.ckpt')
sess.run(training_op)
best_theta = theta.eval()
save_path = saver.save(sess, '/.../my_model_final.ckpt')
读取模型:
with tf.Session as sess:
saver.restore(sess, '/.../my_model_final.ckpt')