Tensorflow 1.0:老司機立下的Flag

老司機怎么能不會立FLAG呢·

  • 如何利用FLAG設(shè)置參數(shù)(包括超參數(shù)和一些路徑設(shè)置),詳細語法介紹見代碼注釋
  • 利用name_scope管理變量
    ** 1. 方便查看tensorboard,比上一講的清晰多啦
    **
    ** 2.方便。。
    **
# -*- coding: utf-8 -*-
"""
Created on Mon Apr  3 19:15:24 2017

@author: Jhy_BUPT
README:

REF:

"""
import tensorflow as tf
# 載入數(shù)據(jù)
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("C:\\tmp\\data", one_hot=True)

# 老司機立下FLAGS
FLAGS = tf.app.flags.FLAGS
# 根據(jù)參數(shù)類型不同,可以定義字符串DEFINE_string,可以定義整數(shù)DEFINE_integer
#DEFINE_integer('參數(shù)名稱',  參數(shù)值, '參數(shù)的解釋')
tf.app.flags.DEFINE_integer('batch_size', 100, 'batch size')
tf.app.flags.DEFINE_float('learning_rate', 0.1, 'l;earning rate')
tf.app.flags.DEFINE_integer('epoch', 1000, 'how many epoch tf will run')
tf.app.flags.DEFINE_string('ckp_dir', 'C:\\tmp\\d44', 'where to save tensorboard')

with tf.name_scope('input'):
    x = tf.placeholder(tf.float32, [None, 784], name='x_input')
    y_ = tf.placeholder(tf.float32, [None, 10], name='y_input')

with tf.name_scope('nn'):
    with tf.name_scope('weight'):
        W = tf.Variable(tf.zeros([784, 10]))
    with tf.name_scope('bias'):
        b = tf.Variable(tf.zeros([10]))
    y = tf.nn.softmax(tf.matmul(x, W) + b)

with tf.name_scope('loss'):
    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))

with tf.name_scope('trian'):
    train_step = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(cross_entropy)

# 開啟會話
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

for _ in range(FLAGS.epoch):
    batch_xs, batch_ys = mnist.train.next_batch(FLAGS.batch_size)
    fd = {x: batch_xs, y_: batch_ys}
    sess.run(train_step, feed_dict=fd)
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
fd2 = {x: mnist.test.images, y_: mnist.test.labels}
print(sess.run(accuracy, feed_dict=fd2))

merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter(FLAGS.ckp_dir, sess.graph)

tensorboard 變量展示,還可打開哦 開箱有驚喜

Paste_Image.png
Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容