老司機怎么能不會立FLAG呢·
- 如何利用FLAG設置參數(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