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. Sql 语句常语法

    以前感觉在这个方面很欠缺,于是就找了些这方面的材料,自己也做了些总结,汇总到了一块.便于以后的查阅. --1.获取表的主键字段SELECT name FROM SysColumns WHERE id= ...

  2. EF CodeFirst简介、默认约定、数据库初始化策略

    CodeFirst 工作流程 创建或修改领域类-->使用数据注解或者Fluent API来配置领域类-->使用自动数据库迁移技术或者基于代码的数据库迁移技术来创建数据库. CodeFirs ...

  3. Allegro 反射仿真--拓扑结构的提取提取及波形分析

    在SPECCTRAQuest下,选择Analyze->SI/EMI sim->Probe,进入如下图所示界面: 注:BRD文件命名不用使用中文字符及一些不常用的字符,如".&qu ...

  4. Android SDK安装环境变量配置

    安卓tool: http://tools.android-studio.org/ SDK下载地址:http://dl.google.com/android/android-sdk_r24.4.1-wi ...

  5. ssh: connect to git@gitlab.xxxxx.com:xxxxx.git port 22: Connection refused

    公司服务器上的gitlab项目添加了ssh密钥,但是操作时却报错ssh: connect to git@gitlab.xxxxx.com:xxxxx.git port 22: Connection r ...

  6. mysq,oraclel复杂SQL操作汇总

    一.对数据库原有字段默认值的设置 1.删除原有字段默认值 alter table 表名 alter column 字段 drop default;2..重写原有字段默认值alter table 表名 ...

  7. 你是否听说过 HashMap 在多线程环境下操作可能会导致程序死循环?

    作者:炸鸡可乐 原文出处:www.pzblog.cn 一.问题描述 经常有些面试官会问,是否了解过 HashMap 在多线程环境下使用时可能会发生死循环,导致服务器 cpu 100% 的线上故障? 关 ...

  8. AcWing 154. 滑动窗口

    https://www.acwing.com/problem/content/156/ #include <iostream> using namespace std; ; int a[N ...

  9. Docker - 构建一个简单的应用镜像

    概述 做个简单的可用镜像 背景 之前的镜像, 都是 命令教程 类的 这次我想构建一个 可以用的 简单镜像镜像 1. 环境 os centos7 docker 18.09 docker image ja ...

  10. MySQL导入含有中文字段(内容)CSV文件乱码解决方法

    特别的注意:一般的CSV文件并不是UTF-8编码,而是10008(MAC-Simplified Chinese GB 2312),所以再通过Navicat导入数据的时候需要指定的编码格式是10008( ...