TensorFlow the model and load the model to make predictions (no need to redefine the network structure)

Source: Internet
Author: User
below, an offline regression model is used to record the saving model and load model to make predictions . Reference Articles:http://blog.csdn.net/thriving_fcl/article/details/71423039

train an offline regression model and save a look at the code:
Import TensorFlow as TF import numpy as NP import Matplotlib.pyplot as Plt Money=np.array ([[[109],[82],[99], [72], [87], [ [To], [to], [Astype], [+]]. Np.float32 Click=np.array ([[One], [8], [8], [6],[7], [7], [7], [8], [9], [5]]). Astyp E (Np.float32) x_test=money[0:5].reshape ( -1,1) y_test=click[0:5] X_train=money[5:].reshape ( -1,1) y_train=click[5:] X =tf.placeholder (tf.float32,[none,1],name= ' x ') #保存要输入的格式 W=TF. Variable (Tf.zeros ([1,1])) B=TF. Variable (Tf.zeros ([1])) Y=tf.matmul (x,w) +b tf.add_to_collection (' pred_network ', y) #用于加载模型获取要预测的网络结构 y_= Tf.placeholder (tf.float32,[none,1]) cost=tf.reduce_sum (Tf.pow ((Y-y_), 2)) train_step= Tf.train.GradientDescentOptimizer (0.000001). Minimize (cost) Init=tf.global_variables_initializer () sess=tf. Session () Sess.run (init) cost_history=[] saver = Tf.train.Saver () to I in range (MB): Feed={x:x_train,y_:y_train} SESS.R Un (train_step,feed_dict=feed) cost_history.append (Sess.run (cost,feed_dict=feed)) # outputs the final w,b and cost value of print ("109 of the predicted value is: ", Sess.run (Y, Feed_dICT={X: [[[109]]}) Print ("W_value:%f"% Sess.run (W), "B_value:%f"% Sess.run (b), "Cost_value:%f"% sess.run (cost, Feed_ dict=feed)) Saver_path = Saver.save (Sess, "/users/shuubiasahi/desktop/tensorflow/mod

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.