TensorFlow学习笔记----TensorBoard_1
2017-02-22 11:29
513 查看
一个曲线拟合的小例子说明要使用TensorBoard,需要对程序添加那些额外的东西。程序:
import tensorflow as tf
import numpy as np
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(1000,1).astype(np.float32)
y_data = tf.sin(x_data)*tf.cos(x_data)+tf.random_uniform([1000,1], -0.1, 0.1)
#graph
X = tf.placeholder(tf.float32,[None,1],name = 'X-input')
Y = tf.placeholder(tf.float32,[None,1],name = 'Y-input')
W1 = tf.Variable(tf.random_uniform([1,5], -1.0, 1.0),name = 'weight1')
W2 = tf.Variable(tf.random_uniform([5,2], -1.0, 1.0),name = 'weight2')
W3 = tf.Variable(tf.random_uniform([2,1], -1.0, 1.0),name = 'weight3')
b1 = tf.Variable(tf.zeros([5]), name = 'bias1')
b2 = tf.Variable(tf.zeros([2]), name = 'bias2')
b3 = tf.Variable(tf.zeros([1]), name = 'bias3')
with tf.name_scope('layer2') as scope:
L2 = tf.sigmoid(tf.matmul(X,W1)+b1)
with tf.name_scope('layer3') as scope:
L3 = tf.sigmoid(tf.matmul(L2,W2)+b2)
with tf.name_scope('layer4') as scope:
hypothesis = tf.sigmoid(tf.matmul(L3,W3)+b3)
with tf.name_scope('cost') as scope:
cost = -tf.reduce_mean(Y*tf.log(hypothesis))
cost_summery = tf.scalar_summary("cost",cost)
with tf.name_scope('train') as scope:
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(cost)
#the summery
w1_hist = tf.histogram_summary("weight1",W1)
w2_hist = tf.histogram_summary("weight2",W2)
b1_hist = tf.histogram_summary("bisa1",b1)
b2_hist = tf.histogram_summary("bisa2",b2)
y_hist = tf.histogram_summary("y",Y)
init = tf.initialize_all_variables()
#run
with tf.Session() as sess:
sess.run(init)
#the workers who translate data to TensorBoard
merged = tf.merge_all_summaries() #collect the tf.xxxxx_summary
writer = tf.train.SummaryWriter('keep',sess.graph)
# maybe many writers to show different curvs in the same figure
for step in range(20000):
summary, _ = sess.run([merged, train], feed_dict={X:x_data,Y:y_data.eval()})
writer.add_summary(summary, step)
if step%10 ==0:
print('step %s' % (step))
import tensorflow as tf
import numpy as np
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(1000,1).astype(np.float32)
y_data = tf.sin(x_data)*tf.cos(x_data)+tf.random_uniform([1000,1], -0.1, 0.1)
#graph
X = tf.placeholder(tf.float32,[None,1],name = 'X-input')
Y = tf.placeholder(tf.float32,[None,1],name = 'Y-input')
W1 = tf.Variable(tf.random_uniform([1,5], -1.0, 1.0),name = 'weight1')
W2 = tf.Variable(tf.random_uniform([5,2], -1.0, 1.0),name = 'weight2')
W3 = tf.Variable(tf.random_uniform([2,1], -1.0, 1.0),name = 'weight3')
b1 = tf.Variable(tf.zeros([5]), name = 'bias1')
b2 = tf.Variable(tf.zeros([2]), name = 'bias2')
b3 = tf.Variable(tf.zeros([1]), name = 'bias3')
with tf.name_scope('layer2') as scope:
L2 = tf.sigmoid(tf.matmul(X,W1)+b1)
with tf.name_scope('layer3') as scope:
L3 = tf.sigmoid(tf.matmul(L2,W2)+b2)
with tf.name_scope('layer4') as scope:
hypothesis = tf.sigmoid(tf.matmul(L3,W3)+b3)
with tf.name_scope('cost') as scope:
cost = -tf.reduce_mean(Y*tf.log(hypothesis))
cost_summery = tf.scalar_summary("cost",cost)
with tf.name_scope('train') as scope:
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(cost)
#the summery
w1_hist = tf.histogram_summary("weight1",W1)
w2_hist = tf.histogram_summary("weight2",W2)
b1_hist = tf.histogram_summary("bisa1",b1)
b2_hist = tf.histogram_summary("bisa2",b2)
y_hist = tf.histogram_summary("y",Y)
init = tf.initialize_all_variables()
#run
with tf.Session() as sess:
sess.run(init)
#the workers who translate data to TensorBoard
merged = tf.merge_all_summaries() #collect the tf.xxxxx_summary
writer = tf.train.SummaryWriter('keep',sess.graph)
# maybe many writers to show different curvs in the same figure
for step in range(20000):
summary, _ = sess.run([merged, train], feed_dict={X:x_data,Y:y_data.eval()})
writer.add_summary(summary, step)
if step%10 ==0:
print('step %s' % (step))
相关文章推荐
- tensorflow学习笔记:运行tensorboard遇到的错误
- [TensorFlow]入门学习笔记(6)-Tensorboard简易教程和模型保存
- TensorFlow学习笔记----TensorBoard_2
- Tensorflow学习笔记:基础篇(7)——Mnist手写集改进版(Tensorboard可视化)
- TensorFlow学习笔记(6):TensorBoard之Embeddings
- Tensorflow学习笔记(二)——Tensorboard和数据类型
- Tensorflow学习笔记:基础篇(6)——Mnist手写集改进版(Optimizer与Tensorboard)
- TensorFlow学习笔记3——windows下采用Anaconda时使用tensorboard的方法
- TensorFlow学习笔记(十二)TensorFLow tensorBoard 总结
- [翻译]斯坦福CS 20SI:基于Tensorflow的深度学习研究课程笔记,Lecture note 4: How to structure your model in TensorFlow
- TensorFlow学习笔记7----Large-scale Linear Models with TensorFlow
- #TensorFlow 学习笔记# 01 Start 一个完整的TensorFlow网络结构
- Tensorflow学习笔记一:getting started with tensorflow
- TensorFlow学习笔记13----TensorFlow Serving
- TensorFlow学习笔记8----TensorFlow Linear Model Tutorial
- Tensorflow学习之TensorBoard
- 学习笔记TF039:TensorBoard
- 学习TensorFlow,TensorBoard可视化网络结构和参数
- [tensorflow学习笔记]tensor.eval
- TensorFlow-6-TensorBoard 可视化学习