码迷,mamicode.com
首页 > 其他好文 > 详细

TensorFlow命令行参数FLAGS使用

时间:2020-01-31 20:35:23      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:step   nal   initial   save   name   reduce   env   histogram   rap   

import os
os.environ[‘TF_CPP_MIN_LOG_LEVEL‘] = ‘2‘

import tensorflow as tf

#tensorboard --logdir="./"

# 命令行参数     python x.py --max_step=500
tf.app.flags.DEFINE_integer("max_step",1000,"train step number")

FLAGS = tf.app.flags.FLAGS

def linearregression():

    with tf.variable_scope("original_data"):
        X = tf.random_normal([100,1],mean=0.0,stddev=1.0)
        y_true = tf.matmul(X,[[0.8]]) + [[0.7]]

    with tf.variable_scope("linear_model"):
        weights = tf.Variable(initial_value=tf.random_normal([1,1]))
        bias = tf.Variable(initial_value=tf.random_normal([1,1]))
        y_predict = tf.matmul(X,weights)+bias

    with tf.variable_scope("loss"):
        loss = tf.reduce_mean(tf.square(y_predict-y_true))

    with tf.variable_scope("optimizer"):
        optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(loss)

    #收集观察张量
    tf.summary.scalar("losses",loss)
    tf.summary.histogram("weight",weights)
    tf.summary.histogram("biases",bias)
    #合并收集的张量
    merge = tf.summary.merge_all()

    init = tf.global_variables_initializer()

    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(init)
        # print(weights.eval(),bias.eval())
        # # 模型加载
        # saver.restore(sess,"./model/linearregression")
        # print(weights.eval(),bias.eval())
        filewriter = tf.summary.FileWriter("./tmp",graph=sess.graph)
        for i in range(FLAGS.max_step):
            sess.run(optimizer)
            print("loss:", sess.run(loss),i)
            print("weight:", sess.run(weights))
            print("bias:", sess.run(bias))
            summary = sess.run(merge)
            filewriter.add_summary(summary,i)

        #checkpoint文件,模型保存
        saver.save(sess,"./model/linearregression")

if __name__ == ‘__main__‘:
    linearregression()

  

TensorFlow命令行参数FLAGS使用

标签:step   nal   initial   save   name   reduce   env   histogram   rap   

原文地址:https://www.cnblogs.com/LiuXinyu12378/p/12246424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!