zoukankan      html  css  js  c++  java
  • Tensorboard 学习笔记

    Tensorboard 学习笔记

    搭建图纸

    首先从 Input 开始:

    # define placeholder for inputs to network
    xs = tf.placeholder(tf.float32, [None, 1])
    ys = tf.placeholder(tf.float32, [None, 1])
    

    对于input我们进行如下修改: 首先,可以为xs指定名称为x_in:

    xs= tf.placeholder(tf.float32, [None, 1],name='x_in')
    

    然后再次对ys指定名称y_in:

    ys= tf.placeholder(tf.loat32, [None, 1],name='y_in')
    

    这里指定的名称将来会在可视化的图层inputs中显示出来

    使用with tf.name_scope('inputs')可以将xsys包含进来,形成一个大的图层,图层的名字就是with tf.name_scope()方法里的参数。

    with tf.name_scope('inputs'):
        # define placeholder for inputs to network
        xs = tf.placeholder(tf.float32, [None, 1])
        ys = tf.placeholder(tf.float32, [None, 1])
    

    接下来开始编辑layer , 请看编辑前的程序片段 :

    def add_layer(inputs, in_size, out_size, activation_function=None):
        # add one more layer and return the output of this layer
        Weights = tf.Variable(tf.random_normal([in_size, out_size]))
        biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
        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
    

    这里的名字应该叫layer, 下面是编辑后的:

    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'):
            Weights= tf.Variable(tf.random_normal([in_size, out_size]))
            # and so on...
    

    在定义完大的框架layer之后,同时也需要定义每一个’框架‘里面的小部件:(Weights biases 和 activation function): 现在现对 Weights 定义: 定义的方法同上,可以使用tf.name.scope()方法,同时也可以在Weights中指定名称W。 即为:

        def add_layer(inputs, in_size, out_size, activation_function=None):
    	#define layer name
        with tf.name_scope('layer'):
            #define weights name 
            with tf.name_scope('weights'):
                Weights= tf.Variable(tf.random_normal([in_size, out_size]),name='W')
            #and so on......
    

    接着继续定义biases , 定义方式同上。

    def add_layer(inputs, in_size, out_size, activation_function=None):
        #define layer name
        with tf.name_scope('layer'):
            #define weights name 
            with tf.name_scope('weights')
                Weights= tf.Variable(tf.random_normal([in_size, out_size]),name='W')
            # define biase
            with tf.name_scope('Wx_plus_b'):
                Wx_plus_b = tf.add(tf.matmul(inputs, Weights), biases)
            # and so on....
    

    activation_function 的话,可以暂时忽略。因为当你自己选择用 tensorflow 中的激励函数(activation function)的时候,tensorflow会默认添加名称。 最终,layer形式如下:

    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
    
    

    最后编辑loss部分:将with tf.name_scope()添加在loss上方,并为它起名为loss

    # the error between prediciton and real data
    with tf.name_scope('loss'):
        loss = tf.reduce_mean(
        tf.reduce_sum(
        tf.square(ys - prediction),
        eduction_indices=[1]
        ))
    
    

    使用with tf.name_scope()再次对train_step部分进行编辑,如下:

    with tf.name_scope('train'):
        train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
    
    

    我们需要使用 tf.summary.FileWriter() (tf.train.SummaryWriter() 这种方式已经在 tf >= 0.12 版本中摒弃) 将上面‘绘画’出的图保存到一个目录中,以方便后期在浏览器中可以浏览。 这个方法中的第二个参数需要使用sess.graph , 因此我们需要把这句话放在获取session的后面。 这里的graph是将前面定义的框架信息收集起来,然后放在logs/目录下面。

    sess = tf.Session() # get session
    # tf.train.SummaryWriter soon be deprecated, use following
    writer = tf.summary.FileWriter("logs/", sess.graph)
    
    

    最后在你的terminal(终端)中 ,使用以下命令

    tensorboard --logdir logs
    
    

    同时将终端中输出的网址复制到浏览器中,便可以看到之前定义的视图框架了。

  • 相关阅读:
    oracle常用命令(比较常见好用)
    vim编辑器详解
    对话框和打印控件
    winform弹出唯一的窗体
    ListView
    菜单栏和布局
    窗体属性和公共控件
    ASPCMS和WPF
    MVC
    正则表达式
  • 原文地址:https://www.cnblogs.com/Archger/p/12774665.html
Copyright © 2011-2022 走看看