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手写数据集的更多相关文章

  1. 吴裕雄 python 神经网络TensorFlow实现LeNet模型处理手写数字识别MNIST数据集

    import tensorflow as tf tf.reset_default_graph() # 配置神经网络的参数 INPUT_NODE = 784 OUTPUT_NODE = 10 IMAGE ...

  2. 吴裕雄 python 神经网络——TensorFlow实现AlexNet模型处理手写数字识别MNIST数据集

    import tensorflow as tf # 输入数据 from tensorflow.examples.tutorials.mnist import input_data mnist = in ...

  3. 吴裕雄 PYTHON 神经网络——TENSORFLOW 滑动平均模型

    import tensorflow as tf v1 = tf.Variable(0, dtype=tf.float32) step = tf.Variable(0, trainable=False) ...

  4. 吴裕雄 python 神经网络——TensorFlow 实现LeNet-5模型处理MNIST手写数据集

    import os import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import ...

  5. TensorFlow实战第五课(MNIST手写数据集识别)

    Tensorflow实现softmax regression识别手写数字 MNIST手写数字识别可以形象的描述为机器学习领域中的hello world. MNIST是一个非常简单的机器视觉数据集.它由 ...

  6. 吴裕雄 python 神经网络——TensorFlow 训练过程的可视化 TensorBoard的应用

    #训练过程的可视化 ,TensorBoard的应用 #导入模块并下载数据集 import tensorflow as tf from tensorflow.examples.tutorials.mni ...

  7. 吴裕雄 python 神经网络——TensorFlow 使用卷积神经网络训练和预测MNIST手写数据集

    import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_dat ...

  8. 吴裕雄 PYTHON 神经网络——TENSORFLOW 无监督学习处理MNIST手写数字数据集

    # 导入模块 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 加载数据 from tensor ...

  9. 吴裕雄 python 神经网络——TensorFlow训练神经网络:全模型

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data INPUT_NODE = 784 ...

随机推荐

  1. CF399B Red and Blue Balls

    题目 CF399B 洛谷RemoteJudge 思路 很容易发现,栈中靠上的蓝色球的出栈,对它下方的蓝色球没有影响. 举个例子: 第一步中靠上的蓝色球在第三步出栈了,这一过程对它下面的蓝色球(即第一步 ...

  2. OpenShift 4.3环境中创建基于Go的Operator

    详细步骤可以参考官方文档 https://docs.openshift.com/container-platform/4.3/operators/operator_sdk/osdk-getting-s ...

  3. d2admin框架学习

    <template slot-scope="scope"> <el-button @click="upper_score(scope.$index, s ...

  4. js中进入页面后刷新一次,且只刷新一次

    让页面进行刷新,可以使用location.reload()方法,但是这种方法会让页面一直不断的刷新,这是因为当页面加载完成以后,我们让它刷新一次,那么浏览器就会重新向服务器请求数据,界面会重新加载,然 ...

  5. spring面试合集

    Spring是一个开源的轻量级Java SE / Java EE开发应用框架.在传统应用程序开发中,一个完整的应用是由一组相互协作的对象组成.所以开发一个应用除了要开发业务逻辑之外,最多的是关注如何使 ...

  6. spring(一):思维导图

  7. C#泛型应用及原理

    https://blog.csdn.net/ananlele_/article/details/97623254 https://blog.csdn.net/kebi007/article/detai ...

  8. mysql-sql逻辑查询顺序

    1.sql逻辑执行顺序(物理执行顺序可能会因索引而不同) SELECT  7  DISTINCT 8 FROM  1 JOIN     2 ON      3 WHERE  4 GROUP BY 5 ...

  9. 洛谷P1433 吃奶酪

    #include<iostream> #include<math.h> using namespace std ; ; int n; bool st[N]; double x[ ...

  10. redis架构

    hash槽16384个,0-16383 master1(slave101,slave102)     master2  (slave201,slave202)    master 3 (slave30 ...