TensorFlow notes - saving model, recovery and achieve linear regression

Save Model

tf.train.Saver(var_list=None,max_to_keep=5)

• var_list: Specifies that you want to save and restore the variables. It can be used as a

dict or a list of transfer.

• max_to_keep: indicates the maximum number of the most recent checkpoint files you want to keep.

When you create a new file, it deletes older files. If there is no or 0, leave all

The checkpoint file. The default is 5 (that is, keep the most recent five checkpoint file.)

saver = tf.train.Saver()
saver.save(sess, "")

Recovery Model

The method of recovery model is restore (sess, save_path), save_path is the path to the previously saved parameters, we can use tf.train.latest_checkpoint to get the latest checkpoint file (also write directly to a malicious file directory)

if os.path.exists("tmp/ckpt/checkpoint"):
            saver.restore(sess,"")
            print("恢复模型")

Custom command-line arguments

import tensorflow as tf

FLAGS = tf.app.flags.FLAGS

tf.app.flags.DEFINE_string('data_dir', '/tmp/tensorflow/mnist/input_data',
                           """数据集目录""")
tf.app.flags.DEFINE_integer('max_steps', 2000,
                            """训练次数""")
tf.app.flags.DEFINE_string('summary_dir', '/tmp/summary/mnist/convtrain',
                           """事件文件目录""")

def main(argv):
    print(FLAGS.data_dir)
    print(FLAGS.max_steps)
    print(FLAGS.summary_dir)
    print(argv)


if __name__=="__main__":
    tf.app.run()

Linear Regression

Prepare data

tf.variable_scope with ( " Data " ):
     # . 1, data preparation, x eigenvalue [100, 1] y target [100] 
    X = tf.random_normal ([100,. 1], Mean = 1.75, STDDEV = 0.5, = name " x_data " )
     # matrix multiplication must be two-dimensional 
     y_true = tf.matmul (x, [[ 0.7]]) + 0.8

Construction of model

tf.variable_scope with ( " Model " ):
     # 2, a linear regression model features, a weight, a bias B + XW = Y 
    # randomized to a weight value and offset, to calculate the loss of him, and then optimizing the current state 
    # with the definition of variables to optimize 
    weight = tf.Variable (tf.random_normal ([. 1,. 1], Mean = 0.0, STDDEV = 1.0), name = " W " ) 
    BIAS = tf.Variable (0.0 , name = " B " ) 
    y_predict = tf.matmul (X, weight) + BIAS

Constructor function loss

tf.variable_scope with ( " Loss " ):
    # . 3, the establishment of a loss function, the mean square error 
   loss = tf.reduce_mean (tf.square (y_true - y_predict))

Gradient descent

tf.variable_scope with ( " Optimizer " ):
        # . 4, the gradient descent optimization loss leaning_rate: ~ 0. 1, 2, 3,5,. 7, 10 
       train_op = tf.train.GradientDescentOptimizer (0.1) .minimize (Loss)

 

Source

 

Import tensorflow TF AS
 Import OS 

os.environ [ ' TF_CPP_MIN_LOG_LEVEL ' ] = ' 2 ' 
# where Li In Flag 
tf.app.flags.DEFINE_integer ( " MAX_STEP " , 100, " model training step count " ) 
tf.app.flags .DEFINE_string ( " model_dir " , " tmp / the Summary / the Test " , " load path of the model file " ) 

the FLAGS = tf.app.flags.FLAGS
 DEF myregression (): 
    with tf.variable_scope ( "Data " ): 
        X = tf.random_normal ([100,. 1], Mean = 1.75, STDDEV = 0.5 ) 
        y_true = tf.matmul (X, [[0.7]]) + 0.8 
    with tf.variable_scope ( " Model " ):
         # weight trainable weights specified session whether the weights change with 
        weight = tf.Variable (tf.random_normal ([int (x.shape [. 1]),. 1], Mean = 0, = STDDEV. 1), name = " W " )
         # bias term 
        bIAS = tf.Variable (0.0, name = ' B ' )
         # constructor function y 
        y_predict tf.matmul = (X, weight) + bIAS 
    with tf.variable_scope (" Loss " ):
         # define loss function 
        Loss = tf.reduce_mean (tf.square (y_true - y_predict)) 
    with tf.variable_scope ( " Optimizer " ):
         # gradient descent solved 
        train_op = tf.train.GradientDescentOptimizer (0.1 ) .minimize ((Loss))
     # 1. collected tensor 
    tf.summary.scalar ( " LOSSES " , Loss) 
    tf.summary.histogram ( " weights " , weight)
     # 2. tensor combined definitions of OP 
    merged = tf.summary. merge_all ()
     # define a model to save op
    = Saver tf.train.Saver () 
    with tf.Session () AS sess: 
        . tf.global_variables_initializer () RUN () 
        # Import matplotlib.pyplot AS plt 
        # plt.scatter (x.eval (), y_true.eval () ) 
        # plt.show () 
        Print ( " the right to re-initialize:% f, offset term:% f " % (weight.eval (), bias.eval ()))
         # create an event file 
        filewriter = tf.summary.FileWriter ( ' ./tmp/summary/test/ ' , Graph = sess.graph)
         # loading model 
        IF os.path.exists ( " tmp / CKPT / the checkpoint " ):
            saver.restore(sess,FLAGS.model_dir)
            print("加载")
        n = 0
        while loss.eval() > 1e-6:
            n += 1
            if(n==FLAGS.max_step):
                break
            sess.run(train_op)
            summary = sess.run(merged)
            filewriter.add_summary(summary, n)
            print("第%d次权重:%f,偏置项:%f" % (n, weight.eval(), bias.eval()))
        saver.save(sess, FLAGS.model_dir)
    return weight, bias


myregression()
# x_min,x_max = np.min(x.eval()),np.max(x.eval())
# tx = np.arange(x_min,x_max,100)

 

 

Guess you like

Origin www.cnblogs.com/TimVerion/p/11224498.html