类似caffe,在TensorFlow中也存在可以绘制网络结构图的工具,只不过相对麻烦一些,小编也在这过程中出现了很多错误,比如端口被占用,TensorFlow版本无法编译使用等等。
首先是在一些必要节点给出name=' ',程序如下:
登录后复制
# -*- coding: utf-8 -*-
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
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'):#######注意tab缩进
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("/home/ren_dong/PycharmProjects/tensorflow", sess.graph)###加载到制定的文件夹中
# important step
sess.run(tf.global_variables_initializer())
###查看方式
###在终端中输入下面一行指令,在给出的网址打开,graph中会显示出结构图
###tensorboard --logdir=/home/ren_dong/PycharmProjects/tensorflow
运行程序后,会根据最后一行命令给定的地址,将网络结构图文件存放在文件夹中在,比如小编存在了
登录后复制
/home/ren_dong/PycharmProjects/tensorflow
event.out...就是结构图文件,那么如何打开呢,首先打开终端,在终端中输入
登录后复制
tensorboard --logdir=/home/ren_dong/PycharmProjects/tensorflow
如果提示
表示6006端口被占用,这时候输入
可以很明显看到是10353占用了6006端口,kill掉,重新输入tensorboard..命令,结果如下:
会给出一个网址,复制到浏览器访问进入tensorboard网站,可以看到网络结构图
可以双击放大观看
总的来讲,在TensorFlow中进行网络结构可视化还是相当繁琐的,相比caffe来讲,个人更喜欢caffe框架,简单粗暴。
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删