TensorFlow 全连接网络实现
来源:互联网 发布:上海雨人软件 编辑:程序博客网 时间:2024/05/29 02:12
1**神经网络**是一种数学模型,大量的神经元相连接并进行计算,用来对输入和输出间复杂的关系进行建模。
神经网络训练,通过大量数据样本,对比正确答案和模型输出之间的区别(梯度),然后把这个区别(梯度)反向的传递回去,对每个相应的神经元进行一点点的改变。那么下一次在训练的时候就可以用已经改进一点点的神经元去得到稍微准确一点的结果。
基于TensorFlow实现一个简单的神经网络。
结构图
搭建神经网络图
1. 准备训练数据
导入相应的包:
import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt
准备训练数据:
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 = 2 * np.power(x_data, 3) + np.power(x_data, 2) + noise
2. 定义网络结构
定义占位符:
xs = tf.placeholder(tf.float32, [None, 1])ys = tf.placeholder(tf.float32, [None, 1])
定义神经层:隐藏层和预测层
# 隐层1Weights1 = tf.Variable(tf.random_normal([1, 5]))biases1 = tf.Variable(tf.zeros([1, 5]) + 0.1)Wx_plus_b1 = tf.matmul(xs, Weights1) + biases1l1 = tf.nn.relu(Wx_plus_b1)# 隐层2Weights2 = tf.Variable(tf.random_normal([5, 10]))biases2 = tf.Variable(tf.zeros([1, 10]) + 0.1)Wx_plus_b2 = tf.matmul(l1, Weights2) + biases2l2 = tf.nn.relu(Wx_plus_b2)# 输出层Weights3 = tf.Variable(tf.random_normal([10, 1]))biases3 = tf.Variable(tf.zeros([1, 1]) + 0.1)prediction = tf.matmul(l2, Weights3) + biases3
3. 定义 loss 表达式
这里采用均方差(mean squared error):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
4. optimizer
即训练的优化策略,一般有梯度下降(GradientDescentOptimizer)、AdamOptimizer等。.minimize(loss)是让 loss 达到最小。
train_step = tf.train.AdamOptimizer(0.1).minimize(loss)
训练
# 初始化所有变量init = tf.global_variables_initializer()# 激活会话with tf.Session() as sess: sess.run(init) # 绘制原始x-y散点图。 fig = plt.figure() ax = fig.add_subplot(1, 1, 1) ax.scatter(x_data, y_data) plt.ion() plt.show() # 迭代次数 = 10000 for i in range(10000): # 训练 sess.run(train_step, feed_dict={xs: x_data, ys: y_data}) # 每50步绘图并打印输出。 if i % 50 == 0: # 可视化模型输出的结果。 try: ax.lines.remove(lines[0]) except Exception: pass prediction_value = sess.run(prediction, feed_dict={xs: x_data}) # 绘制模型预测值。 lines = ax.plot(x_data, prediction_value, 'r-', lw=5) plt.pause(1) # 打印损失 print(sess.run(loss, feed_dict={xs: x_data, ys: y_data}))
最终结果
最终损失:0.0026713(不同的初始化可能会有不同)
GitHub完整代码:FullConnectedNetwork.py
Reference
youtube 上的课程
阅读全文
2 0
- TensorFlow 全连接网络实现
- Tensorflow <一> 一层全连接网络实现XOR
- tensorflow 多元全连接
- TensorFlow学习笔记(4)----完整的工程示例:全连接前馈网络识别MNIST
- 汐月教育之理解TensorFlow(三.2)构建全连接网络进行MNIST识别
- Tensorflow实现CNN网络
- Tensorflow实现全链接神经网络
- 使用tensorflow实现全连接神经网络的简单示例,含源码
- 4. tensorflow之全连接层(dense)
- 开源|如何利用Tensorflow实现语义分割全卷积网络(附源码)
- TensorFlow学习记录--3.MNIST从低级到高级(从全连接网络到卷积神经网络的解释)
- mysql 实现全连接
- tensorflow 实现游乐场的网络
- TensorFlow实现简单卷积网络
- 【Tensorflow网络架构简单实现】 用Tensorflow实现VGG模型
- Tensorflow CNN(两层卷积+全连接+softmax)
- tensorflow 全连接神经网络 MNIST手写体数字识别
- 基于Tensorflow的机器学习(5) -- 全连接神经网络
- yii2.0报的js冲突的错
- C语言:(新)四则计算器(支持括号和次方运算)
- ACM训练半周总结—10月26
- 内存泄漏
- [刷题]HDU3157
- TensorFlow 全连接网络实现
- CentOS 7搭建VPN虚拟局域网服务器
- Priority Queues
- 中介者模式学习和思考
- HTML插入图片存储路径问题
- C++中友元及继承
- Docker的安装
- golang基础-结构体、结构体链表前后插入、节点添加删除
- codeforces 702A Maximum Increase