您的位置:首页 > 其它

TensorBoard-可视化简单例子

2017-04-19 10:03 288 查看
一、代码例子
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 18 18:50:24 2017

@author: YDD
"""
'''
生成一个文件,保存在‘logs/’文件里面(此处我的logs为:G:\Spyder_文件集\logs)
然后在命令行终端(cmd)中运行该文件(tensorboard --logdi='logs/')回车,拷贝网址(http://169.254.61.28:6006),选择GRAPH,即可显示
'''
import tensorflow as tf
#import numpy as np
#import matplotlib.pyplot as plt

sess = tf.Session()

def add_layer(inputs, in_size,out_size,activation_function=None):
    with tf.name_scope('layer'):
        with tf.name_scope('weights'):
            Weights = tf.Variable(tf.random_normal([in_size,out_size]))#随机初始值的一个矩阵。
        with tf.name_scope('biases'):
            biases = tf.Variable(tf.zeros([1, out_size]) + 0.1) #初始值都是0.1的一个列表.
        with tf.name_scope('Wx_plus_b'):
            Wx_plus_b = tf.matmul(inputs, Weights) + biases  #未被激活的值,存在Wx_plus_b中。
        if activation_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activation_function(Wx_plus_b)
        return outputs
#自己定义了所有的数据。
#x_data = np.linspace(-1,1,300)[:,np.newaxis]  #建造输入,-1到1区间300个单位【维度】,一个特性有300个例子。
#noise = np.random.normal(0,0.05,x_data.shape)  #噪声,方差是0.05,格式和x_data一样。
#y_data = np.square(x_data)-0.5 + noise   #输出y=x^2 -0.5+noise.

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')

#建立一个隐藏层hidden1(10个神经元),一个输出层prediction(1个神经元)
hidden1 = add_layer(xs,1,10,activation_function=tf.nn.relu) #激活函数选择tf.nn.relu。(可尝试其他激活函数,选择误差结果较小最好)
prediction = add_layer(hidden1,10,1,activation_function=None)
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-prediction),
                         reduction_indices=[1]))  #求一个预测值的平方误差,然后将所有的平方误差求和,最后求和的一个平均值。
with tf.name_scope('train_step'):
    train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)   #学习的速率为0.1.

# 这里注意和教程有点差别!!#选定可视化存储目录
writer = tf.summary.FileWriter("logs/",sess.graph)

init = tf.global_variables_initializer()
sess.run(init)

二、运行代码,生成文件




三、命令行终端,找到该文件路径下,运行文件



四、复制网址,并网页打开查看可视化结果



(参考  http://v.youku.com/v_show/id_XMTYxMTYwMjEwMA==.html?f=27327189&o=0&spm=a2h1n.8251843.playList.5!17~5~A

8cf5
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: