from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist = input_data.read_data_sets("MNIST_data/",one_hot = True)
sess = tf.InteractiveSession() def weight_Variable(shape):
initial = tf.truncated_normal(shape,stddev = 0.1)
return tf.Variable(initial) def bias_Variable(shape):
initial = tf.constant(0.1,shape = shape)
return tf.Variable(initial) def conv2d(input,filter):
return tf.nn.conv2d(input,filter,strides = [1,1,1,1],padding = 'SAME') def max_pool_2x2(input):
return tf.nn.max_pool(input,[1,2,2,1],[1,2,2,1],padding = 'SAME') x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
x_image = tf.reshape(x,[-1,28,28,1]) w_conv1 = weight_Variable([5,5,1,32])
b_conv1 = bias_Variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1) w_conv2 = weight_Variable([5,5,32,64])
b_conv2 = bias_Variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)
h_pool2 = max_pool_2x2(h_conv2) w_fc1 = weight_Variable([7*7*64,1024])
b_fc1 = bias_Variable([1024])
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1)+b_fc1) keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob) w_fc2 = weight_Variable([1024,10])
b_fc2 = bias_Variable([10])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop,w_fc2)+b_fc2) cross_entropy = tf.reduce_mean(-tf.reduce_sum(y*tf.log(y_conv),reduction_indices = [1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) tf.global_variables_initializer().run()
for i in range(20000):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict = {x:batch[0],y:batch[1],keep_prob:1.0})
print('step %d,training accuracy %g'%(i,train_accuracy))
train_step.run(feed_dict = {x:batch[0],y:batch[1],keep_prob:0.5}) print('test accuary %g'%accuracy.eval(feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0}))

tensorflow训练代码的更多相关文章

  1. TensorFlow 训练好模型参数的保存和恢复代码

    TensorFlow 训练好模型参数的保存和恢复代码,之前就在想模型不应该每次要个结果都要重新训练一遍吧,应该训练一次就可以一直使用吧. TensorFlow 提供了 Saver 类,可以进行保存和恢 ...

  2. tensorflow训练线性回归模型

    tensorflow安装 tensorflow安装过程不是很顺利,在这里记录一下 环境:Ubuntu 安装 sudo pip install tensorflow 如果出现错误 Could not f ...

  3. 2、TensorFlow训练MNIST

    装载自:http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html TensorFlow训练MNIST 这个教程的目标读者是对机器学习和T ...

  4. tensorflow训练验证码识别模型

    tensorflow训练验证码识别模型的样本可以使用captcha生成,captcha在linux中的安装也很简单: pip install captcha 生成验证码: # -*- coding: ...

  5. TensorFlow训练MNIST报错ResourceExhaustedError

    title: TensorFlow训练MNIST报错ResourceExhaustedError date: 2018-04-01 12:35:44 categories: deep learning ...

  6. TensorFlow.训练_资料(有视频)

    ZC:自己训练 的文章 貌似 能度娘出来很多,得 自己弄过才知道哪些个是坑 哪些个好用...(在CSDN文章的右侧 也有列出很多相关的文章链接)(貌似 度娘的关键字是"TensorFlow ...

  7. 目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练

    将目标检测 的标注数据 .xml 转为 tfrecord 的格式用于 TensorFlow 训练. import xml.etree.ElementTree as ET import numpy as ...

  8. 自己搞了20万张图片100个分类,tensorflow训练23万次后。。。。。。

    自己搞了20万张图片100个分类,tensorflow训练23万次后...... 我自己把训练用的一张图片,弄乱之后做了一个预测 100个汉字,20多万张图片,tensorflow CNN训练23万次 ...

  9. tensorflow训练了10万次,运行完毕,对这个word2vec终于有点感觉了

    tensorflow训练了10万次,运行完毕,对这个word2vec终于有点感觉了 感觉它能找到词与词之间的关系,应该可以用来做推荐系统.自动摘要.相关搜索.联想什么的 tensorflow1.1.0 ...

随机推荐

  1. wxss无法调用本地资源图片

    微信小程序中,wxss中不能调用本地资源图片作为背景图片,奇怪的是在微信开发者工具中可以调用,但是到了真机预览的时候发现并不行,有的电脑上的连微信开发者工具里也不可以调用. 原因在于小程序上传的是代码 ...

  2. Silverlight & Blend动画设计系列二:旋转动画(RotateTransform)

    Silverlight的基础动画包括偏移.旋转.缩放.倾斜和翻转动画,这些基础动画毫无疑问是在Silverlight中使用得最多的动画效果,其使用也是非常简单的.相信看过上一篇<偏移动画(Tra ...

  3. javascript下的arguments,caller,callee,call,apply示例及理解

    (参考:http://justcoding.iteye.com/blog/589111) Arguments  该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments ...

  4. JRebel - 给IDE安装JRebel插件

    JRebel对于很多人来说已经并不陌生了,一搜一大把. 用过JRebel后发现,这对于Java开发简直不可缺少. 尽管其价格有点春节国庆期间的各种交通费用——打劫! 即使如此也出现了有"分享 ...

  5. golang学习之win7下go环境搭建

    以下均采用windows64环境,首先是go的下载,go有msi安装安装和zip解压安装两种安装方式,使用msi安装后go环境会自动配置,zip解压后需手动配置各种环境变量. 首先是下载,网上一搜一大 ...

  6. 生成自签名证书-开启https

    1.生成CA证书 # 生成 CA 私钥 openssl genrsa -out ca.key 2048 # X.509 Certificate Signing Request (CSR) Manage ...

  7. 多个tomcat配置

    在centos7.3下搭建jenkins自动部署环境,需要一个tomcat来启动jenkins,另一个用来自动部署的位置,因此需要两个tomcat同时运行,并且在自动构建后能够启动项目,又不会关闭je ...

  8. Maven+MyBatis 初试

    工作中一直使用的都是Hibernate,总是听见有人拿Mybatis和Hibernate做比较,今天尝试来看看. 一.用Maven建立web项目 此处参见 http://www.cnblogs.com ...

  9. 一台电脑启动多个tomcat

    原文 http://dong-shuai22-126-com.iteye.com/blog/1763666 如果现在一台机器上已经部署了一个tomcat服务,无论这个tomcat是否已经注册为服务了, ...

  10. jQuery数据缓存$.data 的使用以及源码解析

    一.实现原理: 对于DOM元素,通过分配一个唯一的关联id把DOM元素和该DOM元素的数据缓存对象关联起来,关联id被附加到以jQuery.expando的值命名的属性上,数据存储在全局缓存对象jQu ...