# -*- coding:utf-8 -*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os
import argparse
import sys DATA_DIR = os.path.join('.', 'mnist_link') # =======================================
# COMMON OPERATIONS
# =======================================
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') def init_weight(shape):
init = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(init) def init_bias(shape):
init = tf.constant(0.1, shape=shape)
return tf.Variable(init) # =======================================
# BUILD CNN
# =======================================
def build_cnn(x):
'''
build the cnn model
'''
x_image = tf.reshape(x, [-1,28,28,1]) w1 = init_weight([5,5,1,32])
b1=init_bias([32])
conv1 = tf.nn.relu(conv2d(x_image, w1) + b1)
pool1 = max_pool_2x2(conv1) w2 = init_weight([5,5,32,64])
b2 = init_bias([64])
conv2 = tf.nn.relu(conv2d(pool1, w2) + b2)
pool2 = max_pool_2x2(conv2) # fc
w_fc1 = init_weight([7*7*64, 1024])
b_fc1 = init_bias([1024])
pool2_flat = tf.reshape(pool2, [-1, 7*7*64])
fc1 = tf.nn.relu(tf.matmul(pool2_flat, w_fc1) + b_fc1) # dropout
keep_prob = tf.placeholder(tf.float32)
fc1_dropout = tf.nn.dropout(fc1, keep_prob) # fc2
w_fc2 = init_weight([1024, 10])
b_fc2 = init_bias([10])
y_conv = tf.matmul(fc1_dropout, w_fc2) + b_fc2
return y_conv, keep_prob # =======================================
# train and test
# =======================================
def main():
'''
feed data into cnn model, and train and test the model
'''
# import data
print('import data...')
mnist = input_data.read_data_sets(DATA_DIR, one_hot=True) # create graph for cnn
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
y_conv, keep_prob = build_cnn(x) cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_, logits = y_conv))
optimizer = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_predictions = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))
init = tf.global_variables_initializer() print('start training...')
with tf.Session() as sess:
sess.run(init)
for i in range(2000):
batch = mnist.train.next_batch(128)
optimizer.run(feed_dict={x:batch[0], y_:batch[1], keep_prob:0.5})
if i%100 == 0:
train_acc = accuracy.eval(
feed_dict = {x:batch[0], y_:batch[1], keep_prob:1.0})
print('step {}, accuracy is {}'.format(i, train_acc)) test_acc = accuracy.eval(feed_dict={x:mnist.test.images, y_:mnist.test.labels, keep_prob:1.0})
print('test accuracy is {}'.format(test_acc)) if __name__ == '__main__':
print('run main')
main()

Tensorflow学习笔记3:卷积神经网络实现手写字符识别的更多相关文章

  1. tensorflow学习笔记七----------卷积神经网络

    卷积神经网络比神经网络稍微复杂一些,因为其多了一个卷积层(convolutional layer)和池化层(pooling layer). 使用mnist数据集,n个数据,每个数据的像素为28*28* ...

  2. TensorFlow卷积神经网络实现手写数字识别以及可视化

    边学习边笔记 https://www.cnblogs.com/felixwang2/p/9190602.html # https://www.cnblogs.com/felixwang2/p/9190 ...

  3. CNN学习笔记:卷积神经网络

    CNN学习笔记:卷积神经网络 卷积神经网络 基本结构 卷积神经网络是一种层次模型,其输入是原始数据,如RGB图像.音频等.卷积神经网络通过卷积(convolution)操作.汇合(pooling)操作 ...

  4. 【学习笔记】卷积神经网络 (CNN )

    前言 对于卷积神经网络(cnn)这一章不打算做数学方面深入了解,所以只是大致熟悉了一下原理和流程,了解了一些基本概念,所以只是做出了一些总结性的笔记. 感谢B站的视频 https://www.bili ...

  5. 学习笔记TF027:卷积神经网络

    卷积神经网络(Convolutional Neural Network,CNN),可以解决图像识别.时间序列信息问题.深度学习之前,借助SIFT.HoG等算法提取特征,集合SVM等机器学习算法识别图像 ...

  6. Tensorflow搭建卷积神经网络识别手写英语字母

    更新记录: 2018年2月5日 初始文章版本 近几天需要进行英语手写体识别,查阅了很多资料,但是大多数资料都是针对MNIST数据集的,并且主要识别手写数字.为了满足实际的英文手写识别需求,需要从训练集 ...

  7. 深度学习笔记 (一) 卷积神经网络基础 (Foundation of Convolutional Neural Networks)

    一.卷积 卷积神经网络(Convolutional Neural Networks)是一种在空间上共享参数的神经网络.使用数层卷积,而不是数层的矩阵相乘.在图像的处理过程中,每一张图片都可以看成一张“ ...

  8. Tensorflow学习教程------利用卷积神经网络对mnist数据集进行分类_利用训练好的模型进行分类

    #coding:utf-8 import tensorflow as tf from PIL import Image,ImageFilter from tensorflow.examples.tut ...

  9. 使用TensorFlow的卷积神经网络识别手写数字(3)-识别篇

    from PIL import Image import numpy as np import tensorflow as tf import time bShowAccuracy = True # ...

随机推荐

  1. 舞蹈课(dancingLessons)

    有n个人参加一个舞蹈课.每个人的舞蹈技术由整数ai来决定.在舞蹈课的开始,他们从左到右站成一排.当这一排中至少有一对相邻的异性时,舞蹈技术相差最小的那一对会出列并开始跳舞.如果相差最小的不止一对,那么 ...

  2. [ZJU 1004] Anagrams by Stack

    ZOJ Problem Set - 1004 Anagrams by Stack Time Limit: 2 Seconds      Memory Limit: 65536 KB How can a ...

  3. 封装类和非封装类比较相同不int和Integer

    A.所有和int(非封装类比较的,只要数值相同就行) B.io3由valueof弄出来的,所以和io1相同 C.io4是new出来的,所以地址不一样,就不相同 D.和A相同

  4. mysq访问方式

    mysql -h10.81.32.196 -P5152 -Dns_map_data_new -uwangyuchuan_r -p3DLg15rhSsm0O7Nsselect uid,name from ...

  5. Menu Items are not showing on Action Bar

    http://stackoverflow.com/questions/18010072/menu-items-are-not-showing-on-action-bar 版权声明:本文为博主原创文章, ...

  6. 大数据笔记(十三)——常见的NoSQL数据库之HBase数据库(A)

    一.HBase的表结构和体系结构 1.HBase的表结构 把所有的数据存到一张表中.通过牺牲表空间,换取良好的性能. HBase的列以列族的形式存在.每一个列族包括若干列 2.HBase的体系结构 主 ...

  7. slider组件

    slider组件,是一个强大的滑动选择器组件: 属性: min:类型 数字 滑动选择器的(范围)最小值 max:类型 数字 滑动选择器的(范围)最大值 step:类型 数字 步长(滑一次走的距离)  ...

  8. 解决 ffmpeg 在avformat_find_stream_info执行时间太长

    用ffmpeg做demux,网上很多参考文章.对于网络流,avformt_find_stream_info()函数默认需要花费较长的时间进行流格式探测,那么,如何减少探测时间内? 可以通过设置AVFo ...

  9. 网络处理器(Network Processor)

    网络处理器(Network Processor,简称NP),又可以称为交换芯片,专用于实现核心交换机高速转发功能. 根据网络处理器会议(Network Processors Conference)的定 ...

  10. qbzt day2 上午

    内容提要 贪心 分治 分块 搜索 接着昨天的讲 过河问题 考虑AB是最快的人,CD是最慢的人,要把CD两个人送过河,只有两种方案,牵扯到四个人,并且n个规模的原问题化成了n-2个规模的子问题 那么最后 ...