【學習筆記】Hands-on ML with sklearn&tensorflow [TF] [1]模型的訓練、儲存和載入
阿新 • • 發佈:2018-11-19
本篇內容:一個簡單的預測模型的建立、訓練、儲存和載入。
匯入必要模組:
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')