Tensorflow:深度神经网络DNN预测波士顿房价(boston house price)【二】

在Tensorflow:深度神经网络DNN预测波士顿房价(boston house price)【一】 中训练了网络还保存了模型。如何使用训练好的模型?请往下看。。

import用到的包

# coding: utf-8
import tensorflow as tf
from sklearn.datasets import load_boston
from sklearn.preprocessing import scale
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

获取数据

# get data
boston = load_boston()
X = boston.data
y = boston.target

生成训练集和验证集,这里只用到验证集

# split train and test data
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=0)

# scale data
X_test = scale(X_test)
y_test = scale(y_test).reshape((-1,1))

定义预测方法

def predict(X,y,keep_prob):

    with tf.Session() as sess:

        # restore saver
        saver = tf.train.import_meta_graph(meta_graph_or_file="nn_boston_model/nn_boston.model-10000.meta")
        model_file = tf.train.latest_checkpoint(checkpoint_dir="nn_boston_model")
        saver.restore(sess=sess,save_path=model_file)

        # init graph
        graph = tf.get_default_graph()

        # get placeholder from graph
        xs = graph.get_tensor_by_name("inputs:0")
        ys = graph.get_tensor_by_name("y_true:0")
        keep_prob_s = graph.get_tensor_by_name("keep_prob:0")

        # get operation from graph
        pred = graph.get_tensor_by_name("pred:0")

        # run pred
        feed_dict = {xs: X, ys: y, keep_prob_s: keep_prob}
        y_pred = sess.run(pred,feed_dict=feed_dict)

    return y_pred.reshape(-1)

使用预测方法

y_pred = predict(X=X_test,y=y_test,keep_prob=1)

画图查看预测效果

# show data
plt.plot(range(len(y_test)),y_test,'b')
plt.plot(range(len(y_pred)),y_pred,'r--')
plt.show()

效果麻麻地,拟合还是不够好
Tensorflow:深度神经网络DNN预测波士顿房价(boston house price)【二】_第1张图片

你可能感兴趣的:(tensorflow)