- 绘制图纸graph
writer = tf.summary.FileWriter("logs/", sess.graph)
from __future__ import print_function
import tensorflow as tf
def add_layer(inputs, in_size, out_size, activation_function=None):
# add one more layer and return the output of this layer
with tf.name_scope('layer'):
with tf.name_scope('weights'):
Weights = tf.Variable(tf.random_normal([in_size, out_size]), name='W')
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='b')
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.add(tf.matmul(inputs, Weights), biases)
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b, )
return outputs
# define placeholder for inputs to network
with tf.name_scope('inputs'):
xs = tf.placeholder(tf.float32, [None, 1], name='x_input')
ys = tf.placeholder(tf.float32, [None, 1], name='y_input')
# add hidden layer
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 10, 1, activation_function=None)
# the error between prediciton and real data
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),
reduction_indices=[1]))
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
sess = tf.Session()
writer = tf.summary.FileWriter("logs/", sess.graph) ####$######
# tf.initialize_all_variables() no long valid from
# 2017-03-02 if using tensorflow >= 0.12
# init = tf.global_variables_initializer()
# sess.run(init)
# direct to the local dir and run this in terminal:
# $ tensorboard --logdir=logs
- 绘制graph和曲线等
tf.summary.histogram('h_out', prediction)
tf.summary.scalar('loss', loss)
writer = tf.summary.FileWriter('./log', sess.graph)
# write to file
merge_op = tf.summary.merge_all()
# operation to merge all
_, result = sess.run([train_op, merge_op], {tf_x: x, tf_y: y})
#run merge_op
writer.add_summary(result, step)
#add summary
"""
Know more, visit my Python tutorial page: https://morvanzhou.github.io/tutorials/
My Youtube Channel: https://www.youtube.com/user/MorvanZhou
Dependencies:
tensorflow: 1.1.0
numpy
"""
import tensorflow as tf
import numpy as np
tf.set_random_seed(1)
np.random.seed(1)
# fake data
x = np.linspace(-1, 1, 100)[:, np.newaxis] # shape (100, 1)
noise = np.random.normal(0, 0.1, size=x.shape)
y = np.power(x, 2) + noise # shape (100, 1) + some noise
with tf.variable_scope('Inputs'):
tf_x = tf.placeholder(tf.float32, x.shape, name='x')
tf_y = tf.placeholder(tf.float32, y.shape, name='y')
with tf.variable_scope('Net'):
l1 = tf.layers.dense(tf_x, 10, tf.nn.relu, name='hidden_layer')
output = tf.layers.dense(l1, 1, name='output_layer')
# add to histogram summary
tf.summary.histogram('h_out', l1)
tf.summary.histogram('pred', output)
loss = tf.losses.mean_squared_error(tf_y, output, scope='loss')
train_op = tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(loss)
tf.summary.scalar('loss', loss) # add loss to scalar summary
sess = tf.Session()
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter('./logs', sess.graph) # write to file
merge_op = tf.summary.merge_all() # operation to merge all summary
for step in range(100):
# train and net output
_, result = sess.run([train_op, merge_op], {tf_x: x, tf_y: y}) #run merge_op
writer.add_summary(result, step) #add summary
# Lastly, in your terminal or CMD, type this :
# $ tensorboard --logdir path/to/log
# open you google chrome, type the link shown on your terminal or CMD. (something like this: http://localhost:6006)
运行.py文件后,
$ tensorboard --logdir=logs