吴裕雄 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 ...
随机推荐
- HDU2020——绝对值排序(java实现,使用map)
Question Description Input Output Sample Input Sample Output 解题思路简述: 在接收每一个数组的过程中,将负数及其绝对值以键值对的形式存入m ...
- java内部类概念
一.成员内部类作为外部类的成员存在的类,则称之为成员内部类 public class OuterClass{ public class InnerClass{ } } 成员内部类样例 成员内部类具有如 ...
- Lumen 实现接口 Captcha图片验证码功能
安装 composer require youngyezi/captcha 使用 新版的包已经删除了 session 支持,完全交给业务自由选择存储方式 个人觉得这样更方便来解耦业务,尤其 Lumen ...
- grunt里面将es6转为es5,遇到的坑
今天看了一下grunt的官方文档,就按照教程来自己创建一个demo,发现grunt要把es6转化为es5需要安装babel,然后就按照grunt-babel的文档进行了一系列操作,等我要兴致勃勃的运行 ...
- IntelliJ IDEA 2017.3-----导入jar包
1.选择jar包 2.右键选择 3.点击ok
- IntelliJ IDEA 2017.3尚硅谷-----配置 Tomcat
- sql避免科学计数法并保留两位小数
SELECT trim(to_char(), ), ,), '9999,999,990.00')) AS CAvgPrice FROM POSITION a 结果: 以trim(to_char(ROU ...
- 格式化输出_python
一.直接使用 +a='chen'b='xiao'c='zan'print(a+b+c) 二.利用占位符 %%s:占位符:%d:整数:%x:十六进制:%f:浮点数(默认6位小数)特别注意浮点数: 指定长 ...
- awk从放弃到入门(2):awk分隔符
一.awk分隔符 awk的默认分割符是空格,但是,这样描述并不精确,因为,awk的分隔符还分为两种,"输入分隔符" 和 "输出分隔符" . (1)输入分隔符:英 ...
- LED Decorative Light Manufacturer - Decorative Lighting: Functionality And Aesthetics
Whether it is for general ambient lighting, task lighting or accent lighting, the decorative lightin ...