Tensorflow学习笔记3:卷积神经网络实现手写字符识别
# -*- 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:卷积神经网络实现手写字符识别的更多相关文章
- tensorflow学习笔记七----------卷积神经网络
卷积神经网络比神经网络稍微复杂一些,因为其多了一个卷积层(convolutional layer)和池化层(pooling layer). 使用mnist数据集,n个数据,每个数据的像素为28*28* ...
- TensorFlow卷积神经网络实现手写数字识别以及可视化
边学习边笔记 https://www.cnblogs.com/felixwang2/p/9190602.html # https://www.cnblogs.com/felixwang2/p/9190 ...
- CNN学习笔记:卷积神经网络
CNN学习笔记:卷积神经网络 卷积神经网络 基本结构 卷积神经网络是一种层次模型,其输入是原始数据,如RGB图像.音频等.卷积神经网络通过卷积(convolution)操作.汇合(pooling)操作 ...
- 【学习笔记】卷积神经网络 (CNN )
前言 对于卷积神经网络(cnn)这一章不打算做数学方面深入了解,所以只是大致熟悉了一下原理和流程,了解了一些基本概念,所以只是做出了一些总结性的笔记. 感谢B站的视频 https://www.bili ...
- 学习笔记TF027:卷积神经网络
卷积神经网络(Convolutional Neural Network,CNN),可以解决图像识别.时间序列信息问题.深度学习之前,借助SIFT.HoG等算法提取特征,集合SVM等机器学习算法识别图像 ...
- Tensorflow搭建卷积神经网络识别手写英语字母
更新记录: 2018年2月5日 初始文章版本 近几天需要进行英语手写体识别,查阅了很多资料,但是大多数资料都是针对MNIST数据集的,并且主要识别手写数字.为了满足实际的英文手写识别需求,需要从训练集 ...
- 深度学习笔记 (一) 卷积神经网络基础 (Foundation of Convolutional Neural Networks)
一.卷积 卷积神经网络(Convolutional Neural Networks)是一种在空间上共享参数的神经网络.使用数层卷积,而不是数层的矩阵相乘.在图像的处理过程中,每一张图片都可以看成一张“ ...
- Tensorflow学习教程------利用卷积神经网络对mnist数据集进行分类_利用训练好的模型进行分类
#coding:utf-8 import tensorflow as tf from PIL import Image,ImageFilter from tensorflow.examples.tut ...
- 使用TensorFlow的卷积神经网络识别手写数字(3)-识别篇
from PIL import Image import numpy as np import tensorflow as tf import time bShowAccuracy = True # ...
随机推荐
- Oracle--创建TRIGGER实现跟踪用户登录信息
---创建日志表记录用户登录信息create table user_log( user_id VARCHAR2(30), session_id NUMBER(10), ...
- 配置maven方法
配置maven方法 1.官网下载maven, http://maven.apache.org/download.cgi Only the binaries are required, so look ...
- 在 mac 系统上安装 python 的 MySQLdb 模块
在 mac 系统上安装 python 的 MySQLdb 模块 特别说明:本文主要参考了Mac系统怎么安装MySQLdb(MySQL-Python) 第 1 步:下载 MySQL-python-1.2 ...
- 500 cannot be cast to javax.xml.registry.infomodel
在使用mybatis的时候每次一调用一个返回User类型的sql时,总是会报错如下: com.xx.all.domain.User cannot be cast to javax.xml.regist ...
- BBED ORA-00600: internal error code, arguments: [16703], [1403], [20], [], [], [], [], [], [], [], [], []
BBED模拟并修复 删除:$ORACLE_HOME/rdbms/admin/prvtsupp.plb SQL> alter database open;alter database open*E ...
- qbzt day3 晚上 平衡树的一些思想
pks大佬的blog 二叉查找树 任何一个节点左子树的所有元素都小于这个节点,右子树的所有元素都大于这个节点 查找一个节点:从根节点开始,比他小就向左走,比他大就向右走 平衡树:解决二叉查找树的一些痛 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_06 Properties集合_1_使用Properties集合存储数据,遍历取出集合中的数据
map下面的实现类叫做Hashtable Properties是唯一和IO流相结合的 讲解 代码
- 阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第4节 多态_15_多态的概述
不是多种状态,而是有多种形态 继承是多态的前提 子类就是一个父类.学生就是一个人 多态性说的是这个对象.
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_02 泛型_3_定义和使用含有泛型的类
创建一个类,添加一个name的属性,然后生成get和set 使用上面创建的类 使用泛型 所以我们取出来也是一个Object的类型 定义的时候规定的类型是Integer,所以这里setName设置的时候 ...
- KETTLE——(一)资源库
对KETTLE有了大概的了解,pdi-ce-6.0.1.0-386也下载完成了. 1.解压pdi-ce-6.0.1.0-386.zip文件,双击运行Spoon.bat(KETTLE是Java开发的,运 ...