TensorFlow学习程序(三):构造一个简单的神经网络并通过Matplotlib可视化
2019-01-27 22:46
591 查看
版权声明:如需转载请标明出处 https://blog.csdn.net/IMUDEGS_YangHan/article/details/86670728
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt #使输出数据可视化 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) #由于biases一般为不为0的数,所以再加上0.1 with tf.name_scope('Wx_plus_b'): Wx_plus_b = tf.matmul(inputs, Weights) + biases #相当于numpy中的np.dot(inputs, Weights) if activation_function is None: #如果没有激活函数,则为线性运算,outputs为Wx_plus_b outputs = Wx_plus_b else: #如果有激活函数,则需对Wx_plus_b进行进一步运算从而得到outputs outputs = activation_function(Wx_plus_b) return outputs #定义期望数据 x_data = np.linspace(-1, 1, 300, dtype=np.float32)[:,np.newaxis] noise = np.random.normal(0, 0.05, x_data.shape).astype(np.float32) y_data = np.square(x_data) - 0.5 + noise with tf.name_scope('inputs'): #创立神经网络的x_input和y_input两个输入(inputs包括这两者) xs = tf.placeholder(tf.float32, [None, 1], name = 'x_input') #因为x_data的属性只有1,None的作用是指传入多少个值都可以 ys = tf.placeholder(tf.float32, [None, 1], name = 'y_input') #因为y_data的属性只有1,None的作用是指传入多少个值都可以 #构建输入层、隐藏层(黑箱部分)、输出层,对于输入层有几个data就有几个神经元,隐藏层神经元个数自定义 l1 = add_layer(xs, 1, 10, activation_function=tf.nn.relu) #relu有默认名字 prediction = add_layer(l1, 10, 1, activation_function=None) #直接输出,没有激活函数 #其中xs是输入层的输入数据,prediction是输出层的输出数据,两个add_layer是隐藏层(黑箱部分) 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) #激活定义的placeholder! if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1: init = tf.initialize_all_variables() else: init = tf.global_variables_initializer() with tf.Session() as sess: writer = tf.summary.FileWriter("logs/",sess.graph) #graph的作用是将前面已经构建好的框架集合起来放入“”中的文件夹 sess.run(init) fig = plt.figure() #生成一个图片框 ax = fig.add_subplot(1, 1, 1) ax.scatter(x_data, y_data) #生成标准图像 plt.ion() #不加的话执行完plt.show()之后会暂停 plt.show() #显示生成的标准图像 lines = None for step in range(1000): sess.run(train_step, feed_dict={xs: x_data, ys: y_data}) #向xs与ys中传值,利用placeholder的优点是可以将x(y)_data中的任意数量的数据传入,故可以实现数据的分批输入从而提高训练效率 if step % 50 == 0: print(sess.run(loss, feed_dict={xs: x_data, ys: y_data})) prediction_picture = sess.run(prediction, feed_dict={xs: x_data}) #因为要生成预测图象,所以运行prediction,并且只向其中传入x_data try: ax.lines.remove(lines[0]) except Exception: pass lines = ax.plot(x_data, prediction_picture, 'r-', lw=5) plt.pause(0.1) #输出的图像之间的间隔是0.1秒 plt.pause(100)
注意,其中的with tf.name_scope()的作用是生成相应代码在数据流图中的名称。
相关文章推荐
- TF之NN:matplotlib动态演示深度学习之tensorflow将神经网络系统自动学习并优化修正并且将输出结果可视化—Jason niu
- 激励函数简介 Tensorflow最简单的三层神经网络及matplotlib可视化 附激励函数常见类型
- tensorflow学习:建立一个最简单的神经网络
- Tensorflow 构造一个简单的神经网络
- python 数据可视化 matplotlib学习一:绘制简单的折线图
- tensorflow学习笔记二——建立一个简单的神经网络拟合二次函数
- tensorflow笔记(二)之构造一个简单的神经网络
- tensorflow基础学习 非线性回归实现,matplotlib可视化结果
- 利用tensorflow构造一个简单的神经网络
- Matplotlib简单入门学习
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试
- 网络编程学习笔记二(实现一个基于简单TCP的用户注册程序)
- Tensorflow新手通过PlayGround可视化初识神经网络
- Python可视化学习(1):Matplotlib的配置
- tensorflow笔记(二)之构造一个简单的神经网络
- tensorflow建立一个简单的神经网络的方法
- SVM实现多分类的程序基础工作(二)——通过一个简单libsvm例子迈入libsvm学习的大门
- 使用RStudio学习一个简单神经网络
- 神经网络与深度学习 使用Python实现基于梯度下降算法的神经网络和自制仿MNIST数据集的手写数字分类可视化程序 web版本
- 通过编写一个简单的漏洞扫描程序学习Python基本语句