tensorflow使用DNN、CNN、RNN(lstm)分别实现识别mnist手写数字图片

本文主要是介绍tensorflow使用DNN、CNN、RNN(lstm)分别实现识别mnist手写数字图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、DNN结构实现mnist手写数字图片
import os
import struct
import numpy as np
import tensorflow as tf#数据加载函数
def load_mnist(path, kind='train'):"""load mnist dateArgs:path: date pathkind: train or testReturns:images and labels"""labels_path = os.path.join(path,'%s-labels.idx1-ubyte'% kind)   #标签数据images_path = os.path.join(path,'%s-images.idx3-ubyte'% kind)   #图像数据with open(labels_path, 'rb') as lbpath:magic, n = struct.unpack('>II',lbpath.read(8))labels = np.fromfile(lbpath,dtype=np.uint8)with open(images_path, 'rb') as imgpath:magic, num, rows, cols = struct.unpack('>IIII',imgpath.read(16))images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(labels), 784)return images, labels#将label进行one-hot处理
def y_onehot(y):"""one-hot optionArgs:y: labelsReturns:one-hot labeleg:1->[0,1,0,0,0,0,0,0,0]"""n_class = 10y_labels = np.eye(n_class)[y]return y_labels#超参
Epoch=100
batch_size=256
learning_rate=0.05x=tf.placeholder(tf.float32, [None, 784])
y=tf.placeholder(tf.float32, [None, 10])
is_train = tf.placeholder(tf.bool)  #BatchNorm 参数
keep_prob = tf.placeholder(tf.float32)   #dropout参数W_fc1 = tf.Variable(tf.truncated_normal(shape=[784, 1024], stddev=0.1), name="W_fc1")
b_fc1 = tf.Variable(tf.constant(0.01, shape=[1024]), name="b_fc1")W_fc2 = tf.Variable(tf.truncated_normal(shape=[1024, 512], stddev=0.1), name="W_fc2")
b_fc2 = tf.Variable(tf.constant(0.01, shape=[512]), name="b_fc2")W_fc3 = tf.Variable(tf.truncated_normal(shape=[512, 10], stddev=0.1), name="W_fc3")
b_fc3 = tf.Variable(tf.constant(0.01, shape=[10]), name="b_fc3")def minist_dnn(x, is_train, keep_prob, W_fc1, b_fc1, W_fc2, b_fc2):layer1 = tf.add(tf.matmul(x, W_fc1), b_fc1)layer1_bn = tf.layers.batch_normalization(layer1, training=is_train)  #BN层layer1_relu = tf.nn.relu(layer1_bn)#一般添加了BN层就不添加dropout,添加了dropout就不添加BN,这一层只使用dropoutlayer2 = tf.add(tf.matmul(layer1_relu, W_fc2), b_fc2)layer2_relu = tf.nn.relu(layer2)layer2_drop = tf.nn.dropout(layer2_relu, keep_prob)   #dropout层layer3 = tf.add(tf.matmul(layer2_drop, W_fc3), b_fc3)pred = tf.nn.softmax(layer3)return predpred = minist_dnn(x, is_train, keep_prob, W_fc1, b_fc1, W_fc2, b_fc2)
loss = -tf.reduce_mean(y*tf.log(tf.clip_by_value(pred, 1e-8, 1)))
correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(pred,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
train_op=optimizer.minimize(loss)init = tf.global_variables_initializer()
saver = tf.train.Saver(tf.global_variables())path = 'D:/data/mnist/'
X_train, y_train = load_mnist(path, kind='train')    #训练集
y_train_labels = y_onehot(y_train)with tf.Session() as sess:sess.run(init)total_batch = int(len(X_train)/batch_size)for step in range(Epoch):for i in range(1,total_batch):batch_x = X_train[(i-1)*batch_size: i*batch_size]batch_y = y_train_labels[(i-1)*batch_size: i*batch_size]sess.run(train_op,feed_dict={x:batch_x, y:batch_y, is_train:True, keep_prob:0.5})entropy ,acc = sess.run([loss, accuracy], feed_dict={x:X_train[0:1000], y:y_train_labels[0:1000], is_train:False, keep_prob:1})print('step{} loss=============>:{:.4f},   auc===========> {:.4f}'.format(step, entropy, acc) )print ("Optimization Finished!")

在这里插入图片描述

二、CNN结构实现mnist手写数字图片
import os
import struct
import numpy as np
import tensorflow as tftf.reset_default_graph()  #清空计算图#数据加载函数
def load_mnist(path, kind='train'):"""load mnist dateArgs:path: date pathkind: train or testReturns:images and labels"""labels_path = os.path.join(path,'%s-labels.idx1-ubyte'% kind)   #标签数据images_path = os.path.join(path,'%s-images.idx3-ubyte'% kind)   #图像数据with open(labels_path, 'rb') as lbpath:magic, n = struct.unpack('>II',lbpath.read(8))labels = np.fromfile(lbpath,dtype=np.uint8)with open(images_path, 'rb') as imgpath:magic, num, rows, cols = struct.unpack('>IIII',imgpath.read(16))images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(labels), 784)return images, labels#将label进行one-hot处理
def y_onehot(y):"""one-hot optionArgs:y: labelsReturns:one-hot labeleg:1->[0,1,0,0,0,0,0,0,0]"""n_class = 10y_labels = np.eye(n_class)[y]return y_labels#超参
Epoch=100
batch_size=256
learning_rate=0.001x= tf.placeholder(tf.float32, [None, 784])  #x
y=tf.placeholder(tf.float32, [None, 10])  #y
keep_prob = tf.placeholder(tf.float32)   #用于空值dropout概率def mnist_cnn(x, keep_prob):x_image=tf.reshape(x, [-1,28,28,1])  #将数据变为28*28形状#第一层卷积with tf.variable_scope("conv_pool1"):W_conv1 = tf.get_variable("weights",[5,5,1,32], initializer = tf.truncated_normal_initializer(stddev=0.1))   #第一层卷积参数,fileter尺寸为5*5,输入通道为1,输出通道为32b_conv1 = tf.get_variable("bias",[32], initializer = tf.constant_initializer(0.01))   #第一层偏置项,输出的每一个通道通会加一个bias,总共加32个h_conv1=tf.nn.conv2d(x_image, filter= W_conv1, strides=[1,1,1,1], padding="SAME")   #第一层卷积,stride为移动步长h_conv1_relu = tf.nn.relu(h_conv1+b_conv1)  #激活函数,注意tf.nn.conv2d不带激活函数,tf.layers.conv2d参数可以指定激活函数h_pooling1 = tf.nn.max_pool(h_conv1_relu, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')  #ksize为pooling核大小,pooling后变为(?, 14, 14, 32)#第二层卷积with tf.variable_scope("conv_pool2"):W_conv2 = tf.get_variable("weights",[5,5,32,64], initializer = tf.truncated_normal_initializer(stddev=0.1))   #第一层卷积参数,fileter尺寸为5*5,输入通道为1,输出通道为32b_conv2 = tf.get_variable("bias",[64], initializer = tf.constant_initializer(0.01))   #第一层偏置项,输出的每一个通道通会加一个bias,总共加32个h_conv2=tf.nn.conv2d(h_pooling1, filter= W_conv2, strides=[1,1,1,1], padding="SAME")   #第一层卷积,stride为移动步长h_conv2_relu = tf.nn.relu(h_conv2+b_conv2)  #激活函数,注意tf.nn.conv2d不带激活函数,tf.layers.conv2d参数可以指定激活函数h_pooling2 = tf.nn.max_pool(h_conv2_relu, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')  #ksize为pooling核大小,pooling后变为(?, 14, 14, 32)pool_shape = h_pooling2.get_shape().as_list()   #获得h_pooling2的维度,为[batch_size, wide, height, channel]h_pooling2_flat = tf.reshape(h_pooling2, [-1, pool_shape[1]*pool_shape[2]*pool_shape[3]])  #对pooling2一维展开#全连接with tf.variable_scope("fc1"):W_fc1 = tf.get_variable("weights",[pool_shape[1]*pool_shape[2]*pool_shape[3], 1024], initializer = tf.truncated_normal_initializer(stddev=0.1))   #全连接层权重,因为经过两层pooling,图片由28*28变为14*14,再变为7*7, 所以输入神经元为7*7*64b_fc1 = tf.get_variable("bias",[1024], initializer = tf.constant_initializer(0.01))   #h_pooling2 = tf.layers.flatten(h_pooling2)  #对pooling2一维展开fc1 = tf.add(tf.matmul(h_pooling2_flat, W_fc1), b_fc1) #第一层全连接fc1_relu = tf.nn.relu(fc1)fc1_drop = tf.nn.dropout(fc1_relu, keep_prob)   #dropout层#输出层with tf.variable_scope("output"):W_fc2 = tf.get_variable("weights",[1024, 10], initializer = tf.truncated_normal_initializer(stddev=0.1))    #全连接层权重,因为最终判断为10维,所以最终神经元为10个b_fc2 = tf.get_variable("bias",[10], initializer = tf.constant_initializer(0.01)) output = tf.add(tf.matmul(fc1_drop, W_fc2), b_fc2)   #第二层全连接pred = tf.nn.softmax(output)return predpred = mnist_cnn(x, keep_prob)  #带入函数
loss = -tf.reduce_mean(y*tf.log(tf.clip_by_value(pred,1e-11,1.0)))
correct_prediction = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))   #判断预测准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))     #准确率#可以加正则化损失
#loss = loss+ 0.001*tf.nn.l2_loss(W_fc1)
#optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)  #用梯度下降求解
train_op=optimizer.minimize(loss)init = tf.global_variables_initializer()
saver = tf.train.Saver(tf.global_variables())
#saver = tf.compat.v1.train.Saver(tf.compat.v1.global_variables())#with tf.Session() as sess:
#    print(accuracy)path = 'D:/data/mnist/'
X_train, y_train = load_mnist(path, kind='train')    #训练集
y_train_labels = y_onehot(y_train)with tf.Session() as sess:sess.run(init)total_batch = int(len(X_train)/batch_size)for step in range(Epoch):for i in range(1,total_batch):batch_x = X_train[(i-1)*batch_size: i*batch_size]batch_y = y_train_labels[(i-1)*batch_size: i*batch_size]sess.run(train_op,feed_dict={x:batch_x, y:batch_y, keep_prob:0.5})#saver.save(sess, 'D:/data/mnist/cnn_mnist.module', global_step=step)  #保存模型entropy ,acc = sess.run([loss, accuracy], feed_dict={x:X_train[0:1000], y:y_train_labels[0:1000], keep_prob:1})print('step{} loss=============>:{:.4f},   auc===========> {:.4f}'.format(step, entropy, acc) )print ("Optimization Finished!")

在这里插入图片描述

三、LSTM结构实现mnist手写数字图片
import os
import struct
import numpy as np
import tensorflow as tftf.reset_default_graph()  #清空计算图#数据加载函数
def load_mnist(path, kind='train'):"""load mnist dateArgs:path: date pathkind: train or testReturns:images and labels"""labels_path = os.path.join(path,'%s-labels.idx1-ubyte'% kind)   #标签数据images_path = os.path.join(path,'%s-images.idx3-ubyte'% kind)   #图像数据with open(labels_path, 'rb') as lbpath:magic, n = struct.unpack('>II',lbpath.read(8))labels = np.fromfile(lbpath,dtype=np.uint8)with open(images_path, 'rb') as imgpath:magic, num, rows, cols = struct.unpack('>IIII',imgpath.read(16))images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(labels), 784)return images, labels#将label进行one-hot处理
def y_onehot(y):"""one-hot optionArgs:y: labelsReturns:one-hot labeleg:1->[0,1,0,0,0,0,0,0,0]"""n_class = 10y_labels = np.eye(n_class)[y]return y_labels#超参
Epoch=100
learning_rate=0.05
timestep=28   #特征序列长度,对应隐藏层ht个数
num_input=28   #特征维度,共28维。图像每一行可以看做一个维度特征,共28维
lstm_hidden_size=64  #lstm隐藏层神经元个数
num_of_layers = 2  #lstm的层数x= tf.placeholder(tf.float32, [None, 784])  #x
y=tf.placeholder(tf.float32, [None, 10])  #y
lstm_keep_prob = tf.placeholder(tf.float32)
keep_prob = tf.placeholder(tf.float32)   #用于空值dropout概率
batch_size = tf.placeholder(tf.int32, [])   #一个批次数据,训练集是使用256,预测时使用1000,[]表示是一个Scalardef mnist_lstm(x, lstm_keep_prob, keep_prob, batch_size):x_image = tf.reshape(x, [-1,timestep, num_input])  #将数据序列长度*特征维度with tf.variable_scope("lstmlayer"):#创建lstm结构,ht神经元个数为64,,可推测出,一个门的参数个数为64*(64+28)+64, (64+28)表示ht-1与xt拼接的维度#为lstm创建dropout,有两个参数,训练时,input_keep_prob,一般设置为1,out_keep_prob一般设置为0.5#创建num_of_layers层lstm,不能用[lstm]*N,否则每层lstm参数会共享#注意lstm,dropout和多层MultiRNNCell必须放在一起调用,否则会出现维度错误stacked_lstm = tf.nn.rnn_cell.MultiRNNCell([tf.nn.rnn_cell.DropoutWrapper(tf.nn.rnn_cell.BasicLSTMCell(lstm_hidden_size), input_keep_prob=1, output_keep_prob =lstm_keep_prob ) for _ in range(num_of_layers)]) init_state = stacked_lstm.zero_state(batch_size, dtype=tf.float32)#计算前项lstm层的输出, 两个输出,一个记录ht,一个记录ctlstm_outputs, _ = tf.nn.dynamic_rnn(stacked_lstm, x_image, initial_state=init_state, dtype=tf.float32)#我们支取最后一个输出状态ht进行下一步的预测lstm_output = lstm_outputs[:,-1,:]with tf.variable_scope("fc1"):W_fc1 = tf.get_variable('weight', shape=[lstm_hidden_size, 10], initializer = tf.truncated_normal_initializer(stddev=0.1), dtype=tf.float32)b_fc1 = tf.get_variable('bias', shape=[10], initializer = tf.constant_initializer(0.1), dtype=tf.float32)output = tf.add(tf.matmul(lstm_output, W_fc1), b_fc1) #第一层全连接pred = tf.nn.softmax(output)return predpred = mnist_lstm(x, lstm_keep_prob, keep_prob, batch_size)  #带入函数
loss = -tf.reduce_mean(y*tf.log(tf.clip_by_value(pred,1e-11,1.0)))
correct_prediction = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))   #判断预测准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))     #准确率#可以加正则化损失
#loss = loss+ 0.001*tf.nn.l2_loss(W_fc1)
#optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)  #用梯度下降求解
train_op=optimizer.minimize(loss)init = tf.global_variables_initializer()
#saver = tf.train.Saver(tf.global_variables())path = 'D:/data/mnist/'
X_train, y_train = load_mnist(path, kind='train')    #训练集
y_train_labels = y_onehot(y_train)with tf.Session() as sess:sess.run(init)total_batch = int(len(X_train)/256)for step in range(Epoch):for i in range(1,total_batch):batch_x = X_train[(i-1)*256: i*256]batch_y = y_train_labels[(i-1)*256: i*256]sess.run(train_op,feed_dict={x:batch_x, y:batch_y, lstm_keep_prob:0.5, keep_prob:0.5, batch_size:256})#saver.save(sess, 'D:/data/mnist/cnn_mnist.module', global_step=step)  #保存模型entropy ,acc = sess.run([loss, accuracy], feed_dict={x:X_train[0:1000], y:y_train_labels[0:1000], lstm_keep_prob:1, keep_prob:1, batch_size:1000})print('step{} loss=============>:{:.4f},   auc===========> {:.4f}'.format(step, entropy, acc) )print ("Optimization Finished!")

在这里插入图片描述

这篇关于tensorflow使用DNN、CNN、RNN(lstm)分别实现识别mnist手写数字图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048741

相关文章

C语言中联合体union的使用

本文编辑整理自: http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=179471 一、前言 “联合体”(union)与“结构体”(struct)有一些相似之处。但两者有本质上的不同。在结构体中,各成员有各自的内存空间, 一个结构变量的总长度是各成员长度之和。而在“联合”中,各成员共享一段内存空间, 一个联合变量

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

捷瑞数字业绩波动性明显:关联交易不低,募资必要性遭质疑

《港湾商业观察》施子夫 5月22日,山东捷瑞数字科技股份有限公司(以下简称,捷瑞数字)及保荐机构国新证券披露第三轮问询的回复,继续推进北交所上市进程。 从2023年6月递表开始,监管层已下发三轮审核问询函,关注到捷瑞数字存在同业竞争、关联交易、募资合理性、期后业绩波动等焦点问题。公司的上市之路多少被阴影笼罩。​ 业绩波动遭问询 捷瑞数字成立于2000年,公司是一家以数字孪生驱动的工

Tolua使用笔记(上)

目录   1.准备工作 2.运行例子 01.HelloWorld:在C#中,创建和销毁Lua虚拟机 和 简单调用。 02.ScriptsFromFile:在C#中,对一个lua文件的执行调用 03.CallLuaFunction:在C#中,对lua函数的操作 04.AccessingLuaVariables:在C#中,对lua变量的操作 05.LuaCoroutine:在Lua中,

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

Lipowerline5.0 雷达电力应用软件下载使用

1.配网数据处理分析 针对配网线路点云数据,优化了分类算法,支持杆塔、导线、交跨线、建筑物、地面点和其他线路的自动分类;一键生成危险点报告和交跨报告;还能生成点云数据采集航线和自主巡检航线。 获取软件安装包联系邮箱:2895356150@qq.com,资源源于网络,本介绍用于学习使用,如有侵权请您联系删除! 2.新增快速版,简洁易上手 支持快速版和专业版切换使用,快速版界面简洁,保留主

如何免费的去使用connectedpapers?

免费使用connectedpapers 1. 打开谷歌浏览器2. 按住ctrl+shift+N,进入无痕模式3. 不需要登录(也就是访客模式)4. 两次用完,关闭无痕模式(继续重复步骤 2 - 4) 1. 打开谷歌浏览器 2. 按住ctrl+shift+N,进入无痕模式 输入网址:https://www.connectedpapers.com/ 3. 不需要登录(也就是

19.手写Spring AOP

1.Spring AOP顶层设计 2.Spring AOP执行流程 下面是代码实现 3.在 application.properties中增加如下自定义配置: #托管的类扫描包路径#scanPackage=com.gupaoedu.vip.demotemplateRoot=layouts#切面表达式expression#pointCut=public .* com.gupaoedu

17.用300行代码手写初体验Spring V1.0版本

1.1.课程目标 1、了解看源码最有效的方式,先猜测后验证,不要一开始就去调试代码。 2、浓缩就是精华,用 300行最简洁的代码 提炼Spring的基本设计思想。 3、掌握Spring框架的基本脉络。 1.2.内容定位 1、 具有1年以上的SpringMVC使用经验。 2、 希望深入了解Spring源码的人群,对 Spring有一个整体的宏观感受。 3、 全程手写实现SpringM