【tensorflow CNN】构建cnn网络,识别mnist手写数字识别

2024-09-07 06:38

本文主要是介绍【tensorflow CNN】构建cnn网络,识别mnist手写数字识别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

#coding:utf8
"""
构建cnn网络,识别mnist
input                   conv1                   padding   max_pool([2,2],strides=[2,2])               conv2      
x[-1,28,28,1] 卷积 [5,5,1,32] -> [-1,24,24,32]->[-1,28,28,32]->[-1,14,14,32]       卷积      [5,5,32,64]->[-1,10,10,64]padding   max_pool([2,2],strides=[2,2])  flatten    full connected layer 64个节点    dropout   softmax
-> [-1,14,14,64]->[-1,7,7,64] ->           [7*7*64,-1]  矩阵相乘 [64,7*7*64]  """from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
import mathmnist = input_data.read_data_sets("./mnist/",one_hot=True)print("Training set:",mnist.train.images.shape)
print("Training set labels:",mnist.train.labels.shape)
print("Dev Set(Cross Validation set):",mnist.validation.images.shape)
print("Dev Set labels:",mnist.validation.labels.shape)
print("Test Set:",mnist.test.images.shape)
print("Test Set labels:",mnist.test.labels.shape)x_train = mnist.train.images
y_train = mnist.train.labels
x_dev = mnist.validation.images
y_dev = mnist.validation.labels
x_test = mnist.test.images
y_test = mnist.test.labelsdef create_placeholders(n_x, n_y):"""Creates the placeholders for the tensorflow session.Arguments:n_x -- scalar, size of an image vector (num_px * num_px = 64 * 64 * 3 = 12288)n_y -- scalar, number of classes (from 0 to 5, so -> 6)Returns:X -- placeholder for the data input, of shape [n_x, None] and dtype "float"Y -- placeholder for the input labels, of shape [n_y, None] and dtype "float"Tips:- You will use None because it let's us be flexible on the number of examples you will for the placeholders.In fact, the number of examples during test/train is different."""### START CODE HERE ### (approx. 2 lines)X = tf.placeholder(tf.float32, [None,n_x], name="X")Y = tf.placeholder(tf.float32, [None,n_y], name="Y")### END CODE HERE ###return X, Ydef random_mini_batches(X, Y, mini_batch_size=64):"""Creates a list of random minibatches from (X, Y)Arguments:X -- input data, of shape (input size, number of examples)Y -- true "label" vector (1 for blue dot / 0 for red dot), of shape (1, number of examples)mini_batch_size -- size of the mini-batches, integerReturns:mini_batches -- list of synchronous (mini_batch_X, mini_batch_Y)"""m = X.shape[1]  # 训练样本个数mini_batches = []# Step 1: Shuffle (X, Y)permutation = list(np.random.permutation(m))shuffled_X = X[permutation,:]shuffled_Y = Y[permutation,:]# Step 2: Partition (shuffled_X, shuffled_Y). Minus the end case.num_complete_minibatches = math.floor(m / mini_batch_size)  # number of mini batches of size mini_batch_size in your partitionningfor k in range(0, num_complete_minibatches):mini_batch_X = shuffled_X[k * mini_batch_size:(k + 1) * mini_batch_size,:]mini_batch_Y = shuffled_Y[k * mini_batch_size:(k + 1) * mini_batch_size,:]mini_batch = (mini_batch_X, mini_batch_Y)mini_batches.append(mini_batch)# Handling the end case (last mini-batch < mini_batch_size)if m % mini_batch_size != 0:mini_batch_X = shuffled_X[mini_batch_size * num_complete_minibatches:,:]mini_batch_Y = shuffled_Y[mini_batch_size * num_complete_minibatches:,:]mini_batch = (mini_batch_X, mini_batch_Y)mini_batches.append(mini_batch)return mini_batchesdef parameter_init(shape):return tf.Variable(tf.random_normal(shape))#定义一个函数,用于构建卷积层
def conv2d(x, W):return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')#定义一个函数,用于构建池化层
def max_pool(x):return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')def init_parameters():parameters = {}parameters["W_conv1"] = parameter_init([5,5,1,32])parameters["b_conv1"] = parameter_init([32])parameters["W_conv2"] = parameter_init([5,5,32,64])parameters["b_conv2"] = parameter_init([64])parameters["W_fc1"] = parameter_init([7*7*64,256])parameters["b_fc1"] = parameter_init([256])parameters["W_out"] = parameter_init([256,10])parameters["b_out"] = parameter_init([10])return parametersdef forward_propagation(x,parameters):x = tf.reshape(x,[-1,28,28,1])W_conv1 = parameters["W_conv1"]b_conv1 = parameters["b_conv1"]h_conv1 = tf.nn.relu(conv2d(x,W_conv1)+b_conv1)  #第一个卷积层h_pool1 = max_pool(h_conv1)                      #第一个池化层W_conv2 = parameters["W_conv2"]b_conv2 = parameters["b_conv2"]h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)  #第二个全连接层h_pool2 = max_pool(h_conv2)                            #第二个池化层W_fc1 = parameters["W_fc1"]             #第一个全连接层,256个节点b_fc1 = parameters["b_fc1"]h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])           #第二个池化层flatten to 1 dimensional vectorh_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1) + b_fc1)#keep_prob = tf.placeholder("float")#h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)                  #dropout层W_out = parameters["W_out"]b_out = parameters["b_out"]Z = tf.matmul(h_fc1,W_out) + b_outreturn Zdef compute_cost(Z, Y):"""Computes the costArguments:Z -- output of forward propagation (output of the last LINEAR unit), of shape (10, number of examples)Y -- "true" labels vector placeholder, same shape as Z3Returns:cost - Tensor of the cost function"""# to fit the tensorflow requirement for tf.nn.softmax_cross_entropy_with_logits(...,...)logits = Zlabels = Ycost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))return costdef tf_cnn_model(X_train,Y_train,X_test,Y_test,learning_rate=0.001,num_epochs=100,minibatch_size=64,print_cost=True):(m,n_x) = X_train.shape  # (n_x: input size, m : number of examples in the train set)n_y = Y_train.shape[1]  # n_y : output sizecosts = []  # to keep track of the costX = tf.placeholder(tf.float32, [None,n_x], name="X")Y = tf.placeholder(tf.float32, [None,n_y], name="Y")parameters = init_parameters()Z = forward_propagation(X,parameters)cost = compute_cost(Z, Y)optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)init = tf.global_variables_initializer()with tf.Session() as sess:sess.run(init)for epoch in range(num_epochs):epoch_cost = 0.  # Defines a cost related to an epochnum_minibatches = int(m / minibatch_size)minibatches = random_mini_batches(X_train, Y_train, minibatch_size)for minibatch in minibatches:(minibatch_X, minibatch_Y) = minibatch_, minibatch_cost = sess.run([optimizer, cost], feed_dict={X: minibatch_X, Y: minibatch_Y})epoch_cost += minibatch_cost / num_minibatches#if print_cost == True and epoch % 10 == 0:print("Cost after epoch %i: %f" % (epoch, epoch_cost))#if print_cost == True and epoch % 5 == 0:costs.append(epoch_cost)# plot the costplt.plot(np.squeeze(costs))plt.ylabel('cost')plt.xlabel('iterations (per tens)')plt.title("Learning rate =" + str(learning_rate))plt.show()parameters = sess.run(parameters)print("Parameters have been trained!")correct_prediction = tf.equal(tf.argmax(Z,1), tf.argmax(Y,1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))print("Test Accuracy:", accuracy.eval({X: X_test, Y: Y_test}))"""# Training cyclefor epoch in range(num_epochs):epoch_cost = 0.total_batch = int(mnist.train.num_examples/minibatch_size)# Loop over all batchesfor i in range(total_batch):batch_x, batch_y = mnist.train.next_batch(minibatch_size)# Run optimization op (backprop) and cost op (to get loss value)_, minibatch_cost = sess.run([optimizer, cost], feed_dict={X: batch_x,Y: batch_y})# Compute average lossepoch_cost += minibatch_cost / total_batchif print_cost == True and epoch % 10 == 0:print("Cost after epoch %i: %f" % (epoch, epoch_cost))if print_cost == True and epoch % 5 == 0:costs.append(epoch_cost)plt.plot(np.squeeze(costs))plt.ylabel('cost')plt.xlabel('iterations (per tens)')plt.title("Learning rate =" + str(learning_rate))plt.show()parameters = sess.run(parameters)        print("Optimization Finished!")# Test modelcorrect_prediction = tf.equal(tf.argmax(Z, 1), tf.argmax(Y, 1))# Calculate accuracyaccuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))print("Accuracy:", accuracy.eval({x: X_test, y: Y_test}))"""return parameterstf_cnn_model(x_train, y_train, x_test, y_test,learning_rate=0.001, num_epochs=40,minibatch_size=64, print_cost=True)

这篇关于【tensorflow CNN】构建cnn网络,识别mnist手写数字识别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Linux系统之主机网络配置方式

《Linux系统之主机网络配置方式》:本文主要介绍Linux系统之主机网络配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、查看主机的网络参数1、查看主机名2、查看IP地址3、查看网关4、查看DNS二、配置网卡1、修改网卡配置文件2、nmcli工具【通用

使用PyTorch实现手写数字识别功能

《使用PyTorch实现手写数字识别功能》在人工智能的世界里,计算机视觉是最具魅力的领域之一,通过PyTorch这一强大的深度学习框架,我们将在经典的MNIST数据集上,见证一个神经网络从零开始学会识... 目录当计算机学会“看”数字搭建开发环境MNIST数据集解析1. 认识手写数字数据库2. 数据预处理的

java字符串数字补齐位数详解

《java字符串数字补齐位数详解》:本文主要介绍java字符串数字补齐位数,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java字符串数字补齐位数一、使用String.format()方法二、Apache Commons Lang库方法三、Java 11+的St

使用Python和python-pptx构建Markdown到PowerPoint转换器

《使用Python和python-pptx构建Markdown到PowerPoint转换器》在这篇博客中,我们将深入分析一个使用Python开发的应用程序,该程序可以将Markdown文件转换为Pow... 目录引言应用概述代码结构与分析1. 类定义与初始化2. 事件处理3. Markdown 处理4. 转

使用Python高效获取网络数据的操作指南

《使用Python高效获取网络数据的操作指南》网络爬虫是一种自动化程序,用于访问和提取网站上的数据,Python是进行网络爬虫开发的理想语言,拥有丰富的库和工具,使得编写和维护爬虫变得简单高效,本文将... 目录网络爬虫的基本概念常用库介绍安装库Requests和BeautifulSoup爬虫开发发送请求解

Pytorch微调BERT实现命名实体识别

《Pytorch微调BERT实现命名实体识别》命名实体识别(NER)是自然语言处理(NLP)中的一项关键任务,它涉及识别和分类文本中的关键实体,BERT是一种强大的语言表示模型,在各种NLP任务中显著... 目录环境准备加载预训练BERT模型准备数据集标记与对齐微调 BERT最后总结环境准备在继续之前,确

讯飞webapi语音识别接口调用示例代码(python)

《讯飞webapi语音识别接口调用示例代码(python)》:本文主要介绍如何使用Python3调用讯飞WebAPI语音识别接口,重点解决了在处理语音识别结果时判断是否为最后一帧的问题,通过运行代... 目录前言一、环境二、引入库三、代码实例四、运行结果五、总结前言基于python3 讯飞webAPI语音

Java使用Mail构建邮件功能的完整指南

《Java使用Mail构建邮件功能的完整指南》JavaMailAPI是一个功能强大的工具,它可以帮助开发者轻松实现邮件的发送与接收功能,本文将介绍如何使用JavaMail发送和接收邮件,希望对大家有所... 目录1、简述2、主要特点3、发送样例3.1 发送纯文本邮件3.2 发送 html 邮件3.3 发送带

Python结合Flask框架构建一个简易的远程控制系统

《Python结合Flask框架构建一个简易的远程控制系统》这篇文章主要为大家详细介绍了如何使用Python与Flask框架构建一个简易的远程控制系统,能够远程执行操作命令(如关机、重启、锁屏等),还... 目录1.概述2.功能使用系统命令执行实时屏幕监控3. BUG修复过程1. Authorization

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像