吴裕雄 python 神经网络——TensorFlow实现回归模型训练预测MNIST手写数据集
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("E:\\MNIST_data\\", one_hot=True) #构建回归模型,输入原始真实值(group truth),采用sotfmax函数拟合,并定义损失函数和优化器
#定义回归模型
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
# 预测值=矩阵运算(输入,权值) + 偏置
y = tf.matmul(x, W) + b
# 定义损失函数和优化器
#输入真实占位符
y_ = tf.placeholder(tf.float32, [None, 10])
#使用tf.nn.softmax_cross_entropy_with_logits计算预测值与真实值之间的差距
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
#使用梯度下降优化
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) #训练模型
#使用InteractiveSession()创建交互式上下文tf会话,这里的会话是默认
#在tf.Tensor.eval 和tf.Operation.run中都可以使用该会话来运行操作(OP)
sess = tf.InteractiveSession()
#注意:之前的版本中使用的是 tf.initialize_all_variables 作为初始化全局变量,已被弃用,更新后的采用一下命令
tf.global_variables_initializer().run() for _ in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) #模型评估
# 计算预测模型和真实值
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
# 布尔型转化为浮点数,并取平均值 ,得出准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#计算模型在测试集上的准确率 并打印结果
print(sess.run(accuracy, feed_dict={x: mnist.test.images,y_: mnist.test.labels}))

吴裕雄 python 神经网络——TensorFlow实现回归模型训练预测MNIST手写数据集的更多相关文章
- 吴裕雄 python 神经网络TensorFlow实现LeNet模型处理手写数字识别MNIST数据集
import tensorflow as tf tf.reset_default_graph() # 配置神经网络的参数 INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE ...
- 吴裕雄 python 神经网络——TensorFlow实现AlexNet模型处理手写数字识别MNIST数据集
import tensorflow as tf # 输入数据 from tensorflow.examples.tutorials.mnist import input_data mnist = in ...
- 吴裕雄 PYTHON 神经网络——TENSORFLOW 滑动平均模型
import tensorflow as tf v1 = tf.Variable(0, dtype=tf.float32) step = tf.Variable(0, trainable=False) ...
- 吴裕雄 python 神经网络——TensorFlow 实现LeNet-5模型处理MNIST手写数据集
import os import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import ...
- TensorFlow实战第五课(MNIST手写数据集识别)
Tensorflow实现softmax regression识别手写数字 MNIST手写数字识别可以形象的描述为机器学习领域中的hello world. MNIST是一个非常简单的机器视觉数据集.它由 ...
- 吴裕雄 python 神经网络——TensorFlow 训练过程的可视化 TensorBoard的应用
#训练过程的可视化 ,TensorBoard的应用 #导入模块并下载数据集 import tensorflow as tf from tensorflow.examples.tutorials.mni ...
- 吴裕雄 python 神经网络——TensorFlow 使用卷积神经网络训练和预测MNIST手写数据集
import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_dat ...
- 吴裕雄 PYTHON 神经网络——TENSORFLOW 无监督学习处理MNIST手写数字数据集
# 导入模块 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 加载数据 from tensor ...
- 吴裕雄 python 神经网络——TensorFlow训练神经网络:全模型
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...
随机推荐
- ArcGIS10:ArcGIS version not specified. You must call RuntimeManager.Bind before creating any ArcGIS
[网络转载] 今天将ArcGIS系列的软件从ArcGIS9.3.1升级到ArcGIS10,然后就使用VS创建一个简单的AE应用程序,然后拖放一个toolbar.LicenseControl以及MapC ...
- django--关于部署scrapyd项目报错问题
首先在同步两篇之前写过的博客,可能能用得到 1.https://www.cnblogs.com/lutt/p/10893192.html2.https://www.cnblogs.com/lutt/p ...
- flume--exec源
创建a2.conf文件并且编写 Sources 类型是exec 配置文件如下 启动集群 启动flume 产生临时文件 ...tmp 关闭临时文件 再次写入文本,会再次产生一个临时文件.截图左边是一个内 ...
- IntelliJ IDEA 2017.3-----导入jar包
1.选择jar包 2.右键选择 3.点击ok
- opencv3.3 基础:Mat类里setTo函数
Mat& setTo(InputArray value, InputArray mask=noArray()); 说明: 1.功能:把矩阵mask中元素不为0的点全部变为value值: 2.当 ...
- TOYS(计算几何-入门)
题目 ‘^’代表叉乘 ‘•’代表点乘 点积:a•b=ax*bx+ay*by 叉积:a^b=ax*by-bx*ay
- Junit +cucumber 运行报错 initiallizationError
step1: 访问 https://search.maven.org/ 搜索下载相关jar包 step2: 访问 http://maven.outofmemory.cn/info.cukes/cuc ...
- win7系统Java 开发环境配置
我的天啊,博客园写了两年九个月,终于有六个粉丝啦,哈哈哈哈哈哈哈哈哈,谢谢大家的关注 进入正题,java环境配置,我要学java了,人生在于瞎折腾. 久闻java大名,但是没接触过,但java环境配置 ...
- 素问 - IC跨期套利
摘自<小韭的学习圈> Q 我现在是长持ic.我观察到IC1907和IC1909的贴水差会有波动.有时候,IC 1907涨的多,有时候IC1909涨的多.而在某一天这个趋势相对是稳定的. 那 ...
- 每天进步一点点------Allegro 手工布线时控制面板各选项说明
在进行手工布线过程中,最重要的就是对控制面板中的各个选项进行设置,因此首先介绍控制面板中各个选项的含义. 手工布线的命令为Route->connect,执行命令后,右侧控制面板如图8.14所示. ...