Tensorflow手写数字识别(交叉熵)练习
# 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手写数字识别(交叉熵)练习的更多相关文章
- tensorflow手写数字识别(有注释)
import tensorflow as tf import numpy as np # const = tf.constant(2.0, name='const') # b = tf.placeho ...
- 卷积神经网络应用于tensorflow手写数字识别(第三版)
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...
- Tensorflow手写数字识别训练(梯度下降法)
# coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data #p ...
- tensorflow 手写数字识别
https://www.kaggle.com/kakauandme/tensorflow-deep-nn 本人只是负责将这个kernels的代码整理了一遍,具体还是请看原链接 import numpy ...
- Tensorflow手写数字识别---MNIST
MNIST数据集:包含数字0-9的灰度图, 图片size为28x28.训练样本:55000,测试样本:10000,验证集:5000
- 基于tensorflow的MNIST手写数字识别(二)--入门篇
http://www.jianshu.com/p/4195577585e6 基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型 基于tensorflow的MNIST手写数字识 ...
- 手写数字识别 ----卷积神经网络模型官方案例注释(基于Tensorflow,Python)
# 手写数字识别 ----卷积神经网络模型 import os import tensorflow as tf #部分注释来源于 # http://www.cnblogs.com/rgvb178/p/ ...
- 手写数字识别 ----Softmax回归模型官方案例注释(基于Tensorflow,Python)
# 手写数字识别 ----Softmax回归模型 # regression import os import tensorflow as tf from tensorflow.examples.tut ...
- python-积卷神经网络全面理解-tensorflow实现手写数字识别
首先,关于神经网络,其实是一个结合很多知识点的一个算法,关于cnn(积卷神经网络)大家需要了解: 下面给出我之前总结的这两个知识点(基于吴恩达的机器学习) 代价函数: 代价函数 代价函数(Cost F ...
随机推荐
- 《Effective C++》——条款20:宁以pass-by-reference-to-const替换pass-by-value
切割(slicing)问题 请看下面代码: class Window { public: ... std::string name()const; //返回窗口名称 virtual void disp ...
- filter原理
index.jsp: <a href="product-input.action">input</a> <form action="prod ...
- hdoj-1032-The 3n + 1 problem(坑题)
题目链接 //巨坑的一道题,输入的m,n要判断大小,输出还要按照原来的顺序,范围还是i<=n<=j #include <iostream> #include <cstd ...
- 机器学习(六)— logistic回归
最近一直在看机器学习相关的算法,今天学习logistic回归,在对算法进行了简单分析编程实现之后,通过实例进行验证. 一 logistic概述 个人理解的回归就是发现变量之间的关系,也就是求回归系数, ...
- 修改altibase MDB中的复制表的表结构
前提条件:有MDB两个数据库,且知道普通用户和sys用户的密码等基本信息.操作:1:用sys用户登录isqlisql -s 127.0.0.1 -u sys -p manager -port 2030 ...
- UVA - 11107 Life Forms (广义后缀自动机)
题意:给你n个字符串,求出在超过一半的字符串中出现的所有子串中最长的子串,按字典序输出. 对这n个字符串建广义后缀自动机,建完后每个字符串在自动机上跑一遍,沿fail树向上更新所有子串结点的出现次数( ...
- 【LeetCode】003. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- js整数千分化
function numToQfh(num){ var num_str = num.toString(); var strTo = " "; while(num_str.lengt ...
- bzoj 2982 combination——lucas模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2982 明明是lucas定理裸题…… 非常需要注意C( )里 if ( n<m ) r ...
- 转载:trap 的用法 /etc/init.d/rcS trap :1 2 3 24
在有些情况下,我们不希望自己的shell脚本在运行时刻被中断,比如说我们写得shell脚 本设为某一用户的默认shell,使这一用户进入系统后只能作某一项工作,如数据库备份, 我 们可不希望用户使用c ...