Tensorflow的简单应用和可视化

 
 
 
 

2018/03/28

author:教务科主任

第一次写博文,在网上查了一些资料,根据版本更新和电脑做了一些改进,主要改进有:

1.更改CPU任务优先级

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'#更改CPU任务优先级

2.#将xs, ys, loss在tensorboard中显示,1.6版本tensorflow写法

for value in [xs, ys, loss]:
    tf.summary.scalar(value.op.name, value)
summaries = tf.summary.merge_all()

3.# 将 sess.graph框架文件放到目录下的‘logs1’文件夹中。1.6版本tensorflow写法

writer = tf.summary.FileWriter("logs1/", sess.graph) 

完整代码:

import tensorflow as tf
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'#更改CPU任务优先级
print(tf.__version__)
def add_layer(inputs, in_size, out_size, activation_function=None):
    #定义增加层函数,输入:输入数据,输入层维数,输出层位数,激活函数
    Weights = tf.Variable(tf.random_normal([in_size, out_size]), name='Weights')
    #定义权重变量weight,name属性定义变量在tensorboard中显示的名称
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name='biases')
    #定义偏移值变量
    Wx_plus_b = tf.add(tf.matmul(inputs, Weights), biases,name='model')
    #定义回归函数模型
    for value in [Weights, biases, Wx_plus_b]:
        tf.summary.scalar(value.op.name, value)
    #将Weights, biases, Wx_plus_b在tensorboard中显示
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b, )
    #根据激活函数返回计算结果
    return outputs
#定义输入输出占位符,计算时用feed_dict赋值字典对xs,ys,进行复制
xs = tf.placeholder(tf.float32, [None, 1], name='x_input')
ys = tf.placeholder(tf.float32, [None, 1], name='y_input')
# 增加隐藏层,隐含十个神经单元,激活函数为tf.nn.relu,
# 激活函数tf.nn.relu详情见博文https://blog.csdn.net/zj360202/article/details/70256494
l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu)
# 增加输出层
prediction = add_layer(l1, 10, 1, activation_function=None)
# 定义loss函数,用于衡量结算结果的优劣,并作为参数更正的依据
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]),name='loss')
#定义求解器,使用梯度下降法、求解使loss最小
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
#将xs, ys, loss在tensorboard中显示
for value in [xs, ys, loss]:
    tf.summary.scalar(value.op.name, value)
summaries = tf.summary.merge_all()
#定义运算过程监督器
sess = tf.Session()
# 将 sess.graph框架文件放到目录下的logs1文件夹中。
writer = tf.summary.FileWriter("logs1/", sess.graph) # important step sess.run(tf.initialize_all_variables())
# 进入工作目录,打开命令行工具(shift+右键),运行命令 tensorboard --logdir='logs1/'
# 会返回一个地址,然后用浏览器打开这个地址,在 graph 标签栏下打开


命令行运行结果:

PS C:\Users\Andrew\Desktop\Python\untitled>  tensorboard --logdir='logs1/'
W0328 00:19:04.463888 Reloader tf_logging.py:121] Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events.  Overwriting the graph with the newest event.
W0328 00:19:04.466940 Reloader tf_logging.py:121] Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events.  Overwriting the graph with the newest event.
W0328 00:19:04.468901 Reloader tf_logging.py:121] Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events.  Overwriting the graph with the newest event.
TensorBoard 1.6.0 at http://DESKTOP-H0OSCP6:6006 (Press CTRL+C to quit

用浏览器打开网址:http://DESKTOP-H0OSCP6:6006

得到框架图


因为只是好奇神经网络的运行原理才学的可视化,所以看到这就不再深究了,有兴趣的小伙伴可以继续研究。




猜你喜欢

转载自blog.csdn.net/qq_41644087/article/details/79721437