# coding: utf-8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#print("hello")

#载入数据集
mnist = input_data.read_data_sets("F:\\TensorflowProject\\MNIST_data",one_hot=True)

#每个批次的大小,训练时一次100张放入神经网络中训练
batch_size = 100

#计算一共有多少个批次
n_batch = mnist.train.num_examples//batch_size

#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
#0-9十个数字
y = tf.placeholder(tf.float32,[None,10])

#创建一个神经网络
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b)

#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))
#交叉熵
#loss = tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()

#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

#
with tf.Session() as sess:
  sess.run(init)
  for epoch in range(100):
    for batch in range(n_batch):
      batch_xs,batch_ys = mnist.train.next_batch(batch_size)
      sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})

    #测试准确率
    acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
    print("Iter: "+str(epoch)+" ,Testing Accuracy "+str(acc))

Tensorflow手写数字识别(交叉熵)练习的更多相关文章

  1. tensorflow手写数字识别(有注释)

    import tensorflow as tf import numpy as np # const = tf.constant(2.0, name='const') # b = tf.placeho ...

  2. 卷积神经网络应用于tensorflow手写数字识别(第三版)

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...

  3. Tensorflow手写数字识别训练(梯度下降法)

    # coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data #p ...

  4. tensorflow 手写数字识别

    https://www.kaggle.com/kakauandme/tensorflow-deep-nn 本人只是负责将这个kernels的代码整理了一遍,具体还是请看原链接 import numpy ...

  5. Tensorflow手写数字识别---MNIST

    MNIST数据集:包含数字0-9的灰度图, 图片size为28x28.训练样本:55000,测试样本:10000,验证集:5000

  6. 基于tensorflow的MNIST手写数字识别(二)--入门篇

    http://www.jianshu.com/p/4195577585e6 基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型 基于tensorflow的MNIST手写数字识 ...

  7. 手写数字识别 ----卷积神经网络模型官方案例注释(基于Tensorflow,Python)

    # 手写数字识别 ----卷积神经网络模型 import os import tensorflow as tf #部分注释来源于 # http://www.cnblogs.com/rgvb178/p/ ...

  8. 手写数字识别 ----Softmax回归模型官方案例注释(基于Tensorflow,Python)

    # 手写数字识别 ----Softmax回归模型 # regression import os import tensorflow as tf from tensorflow.examples.tut ...

  9. python-积卷神经网络全面理解-tensorflow实现手写数字识别

    首先,关于神经网络,其实是一个结合很多知识点的一个算法,关于cnn(积卷神经网络)大家需要了解: 下面给出我之前总结的这两个知识点(基于吴恩达的机器学习) 代价函数: 代价函数 代价函数(Cost F ...

随机推荐

  1. hdoj-1046-Gridland(规律题)

    题目链接 #include <algorithm> #include <iostream> using namespace std; int main() { int t; c ...

  2. git教程4-创建分支与删除分支

    一.分支的理解 分支,即branch,用于从主分支(master)中新开辟出一个分支,用于对文件进行修改.这部分修改的内容在新的分支未融合到主分支的情况下,主分支是看不见的.新的分支相当于开辟了新的修 ...

  3. grep搜索子目录中包含某字符串的特定文件

    grep -n "str" -r ./ 这是查找当前目录下以及下辖子目录下所有包含str字符串的文件,会列出文件名.以及该行的内容.以及行号 比如,我想搜源码目录src下,包含“n ...

  4. 怪盗基德的滑翔翼(还是最长x序列)

    //怪盗基德的滑翔翼 #include<iostream> #include<cstdio> #include<cstdlib> #include<cstri ...

  5. spark gateway引发:跟踪Cloudera安装服务异常日志跟踪

    spark gateway是用于接收cloudera管理的应用:可以上报数据,不影响正常使用.启动gateway失败,我觉得可能是因为配置问题? 这个问题可能比较深,因为我通过查看日志(clouder ...

  6. hdu 4336 Card Collector —— Min-Max 容斥

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4336 bzoj 4036 的简单版,Min-Max 容斥即可. 代码如下: #include<cst ...

  7. 学习Linux相关书籍

    要推荐的书,我在<那两年炼就的Android内功修养>这篇文章中有提到,这里再列一下出来: 语言类: <深度探索C++对象模型>,对应的英文版是<Inside C+++  ...

  8. sass和less、stylus语法(2)

    6.运算符(Operations)CSS预处理器语言还具有运算的特性,其简单的讲,就是对数值型的Value(如:数字.颜色.变量等)进行加减乘除四则运算.这样的特性在CSS样式中是想都不敢想的,但在C ...

  9. 原生的ado.net(访问sql server数据库)

    本文介绍原生的ado.net(访问sql server数据库) 写在前面 数据库连接字符串 过时的写法 string str = "server=localhost;database=my_ ...

  10. 如何同时打开两个UltraEdit

    高级—配置—应用程序布局—其它,勾上资源管理器选的文件将以独立的UltraEdit打开和允许多个UltraEdit