tensorflow的基本用法(六)——神经网络可视化
来源:互联网 发布:太师鞭 知乎 编辑:程序博客网 时间:2024/05/19 13:24
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
本文主要是对tensorflow的神经网络训练过程以及神经网络结构进行可视化工作。
#!/usr/bin/env python# _*_ coding: utf-8 _*_import tensorflow as tfimport numpy as np# 创建一个神经网络层def add_layer(input, in_size, out_size, activation_function = None): """ :param input: 神经网络层的输入 :param in_zize: 输入数据的大小 :param out_size: 输出数据的大小 :param activation_function: 神经网络激活函数,默认没有 """ 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) with tf.name_scope('W_mul_x_plus_b'): # 计算w*x+b W_mul_x_plus_b = tf.matmul(input, Weights) + biases # 根据是否有激活函数进行处理 if activation_function is None: output = W_mul_x_plus_b else: output = activation_function(W_mul_x_plus_b) return output# 创建一个具有输入层、隐藏层、输出层的三层神经网络,神经元个数分别为1,10,1# 创建只有一个特征的输入数据,数据数目为300,输入层x_data = np.linspace(-1, 1, 300)[:, np.newaxis]# 创建数据中的噪声noise = np.random.normal(0, 0.05, x_data.shape)# 创建输入数据对应的输出y_data = np.square(x_data) + 1 + noisewith tf.name_scope('input'): # 定义输入数据,None是样本数目,表示多少输入数据都行,1是输入数据的特征数目 xs = tf.placeholder(tf.float32, [None, 1], name = 'x_input') # 定义输出数据,与xs同理 ys = tf.placeholder(tf.float32, [None, 1], name = 'y_input')# 定义一个隐藏层hidden_layer = add_layer(xs, 1, 10, activation_function = tf.nn.relu)# 定义输出层prediction = add_layer(hidden_layer, 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'): train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)# 变量初始化init = tf.global_variables_initializer()# 定义Sessionsess = tf.Session()# 将网络结构图写到文件中writer = tf.summary.FileWriter('logs/', sess.graph)# 执行初始化工作sess.run(init)# 绘制求解的曲线fig = plt.figure()ax = fig.add_subplot(1, 1, 1)ax.scatter(x_data, y_data)plt.ion()plt.show()# 进行训练for i in range(1000): # 执行训练,并传入数据 sess.run(train_step, feed_dict = {xs: x_data, ys: y_data}) if i % 100 == 0: try: ax.lines.remove(lines[0]) except Exception: pass # print sess.run(loss, feed_dict = {xs: x_data, ys: y_data}) # 计算预测值 prediction_value = sess.run(prediction, feed_dict = {xs: x_data}) 绘制预测值 lines = ax.plot(x_data, prediction_value, 'r-', lw = 5) plt.pause(0.1)# 关闭Sessionsess.close()
执行结果如下:
在网络结果代码中添加tf.name_scope('name')
并将网络结构图写入文件后,可用tensorboard命令查看神经网络的结果图:
tensorboard --logdir=log/
结果如图:
0 0
- tensorflow的基本用法(六)——神经网络可视化
- tensorflow的基本用法(十)——保存神经网络参数和加载神经网络参数
- tensorflow的基本用法(五)——创建神经网络并训练
- tensorflow的基本用法(七)——使用MNIST训练神经网络
- tensorflow的基本用法(九)——定义卷积神经网络训练MNIST
- tensorflow的基本用法
- TensorFlow的基本用法
- TensorFlow的基本用法
- Tensorflow的基本用法
- tensorflow的基本用法
- tensorflow构建神经网络及可视化
- keras的基本用法(三)——创建神经网络
- keras的基本用法(四)——Fine Tuning神经网络
- tensorflow的基本用法(八)——dropout的作用
- 深度学习--tensorflow playground-神经网络的可视化
- tensorflow的基本用法(四)——placeholder
- Tensorflow系列——基本用法
- tensorflow的基本用法(二)
- JavaSE进阶-Java设计模式
- java.lang.RuntimeException: java.sql.SQLException: Too many parameters: expected 0, was given 1 Quer
- 4.14
- 线性结构——用栈实现波兰表达式求值。
- 对象内置方法,继承,instanceof添加上(安全使用)
- tensorflow的基本用法(六)——神经网络可视化
- 1088. Rational Arithmetic (20)[逻辑题]
- Spring学习之缓存机制EhCache
- react-native新导航组件react-navigation详解
- 无向图的割顶和桥,无向图的双连通分量入门详解及模板
- 碰撞器和触发器
- 创业公司小团队为什么要使用Docker
- hive-1.x在hadoop-2.x上的安装
- 论配置weblogic服务器