手写数字识别全部代码--全连接神经网络方法

本文介绍了使用全连接神经网络(DNN)实现手写数字识别的完整代码,包括定义前向传播、损失函数、学习率策略以及训练过程。通过TensorFlow库实现,程序展示了如何利用L2正则化减少过拟合,并在MNIST数据集上进行训练,监测训练过程中的损失变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

'''
#2018-06-25 272015 June Monday the 26 week, the 176 day SZ
手写字体识别程序文件1:
这个程序使用了全连接神经网络也就是DNN。
定义了前向传播的过程以及神经网络中的参数,无论训练还是测试,都可以直接调用inference这个函数
问题代码:
#regularizer正则化矩阵,变量属性:维度,shape;

tf.truncated_normal_initializer 从截断的正态分布中输出随机值。

seed:一个Python整数。用于创建随机种子。查看 tf.set_random_seed 行为。  

 tf.nn.relu() 激活函数实现去线性化 
神经网络结果加上激活函数和偏置项:f(Wx +b); f(x)是激活函数,b是偏置项
每个神经元的输出经过非线性函数,整个模型就不是非线性了。这个非线性函数就是激活函数。
三个常见激活函数:ReLU激活函数,Sigmoid激活函数,tanh函数;
'''

import tensorflow as tf 

#定义输入,输出,隐藏层1的节点个数
INPUT_NODE = 784 #28*28
OUTPUT_NODE = 10 #输出10个结点,十种分类结果,对应0-9数字
LAYER1_NODE = 500 #隐藏层有500个结点

def get_weight_variable(shape, regularizer): #regularizer正则化矩阵,变量属性:维度,shape;truncated缩短了的;被删节的;切去顶端的
	weights = tf.get_variable('weights',shape, initializer = tf.truncated_normal_initializer(stddev=0.1))
	#张量加入集合losses
	if regularizer != None:
		tf.add_to_collection('losses', regularizer(weights))
	return weights

#定义前向传播过程
def inference(input_tensor, regularizer):
	#声明第一层神经网络的过程并完成前向传播的过程
	with tf.variable_scope('layer1'):
		weights = get_weight_variable([INPUT_NODE, LAYER1_NODE], regularizer) #[INPUT_NODE, LAYER1_NODE]之间的权重
		biases = tf.get_variable('biases', [LAYER1_NODE], initializer = tf.constant_initializer(0.0))
		layer1 = tf.nn.relu(tf.matmul(input_tensor, weights) + biases)

	#声明第2层神经网络的过程并完成前向传播的过程
	with tf.variable_scope('layer2'):
		weights = get_weight_variable([LAYER1_NODE, OUTPUT_NODE], regularizer) #[LAYER1_NODE, OUTPUT_NODE]之间的权重
		biases = tf.get_variable('biases', [OUTPUT_NODE], initializer = tf.constant_initializer(0.0))
		layer2 = tf.matmul(layer1, weights) + biases
	#返回前向传播结果
	return layer2


###########################################以下是训练部分###########################################

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference
import os

BATCH_SIZE = 100  #每批次取100个;一个批次中训练个数。
LEARNING_RATE_BASE = 0.8 #学习率初始值
LEARNING_RATE_DECAY = 0.99 #学习率衰减率
REGULARIZATION_RATE = 0.0001 #正则化系数
TRAINING_STEPS = 30000 #训练轮数
MOVING_AVERAGE_DECAY = 0.99 #滑动平均衰减率,控制模型更新的速度,让模型在测试数据上更健壮
MODEL_SAVE_PATH = 'D:\\ST\\Python_work\\program\\手写识别'
MODEL_NAME = "mnist_model"

def train(mnist):
    # 定义输入输出placeholder。placeholder定义了一个位置,程序运行时候给这个位置提供数据。这个机制提供输入数据
    x = tf.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input')
    y_ = tf.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input')

    regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE) #L2范数正则化
    y = mnist_inference.inference(x, regularizer) #预测值
    global_step = tf.Variable(0, trainable=False) #定义存储训练轮数的变量
    
    # 定义损失函数、学习率、滑动平均操作以及训练过程。
    variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    variables_averages_op = variable_averages.apply(tf.trainable_variables())
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
    cross_entropy_mean = tf.reduce_mean(cross_entropy)
    loss = cross_entr
Code provided by Ruslan Salakhutdinov and Geoff Hinton Permission is granted for anyone to copy, use, modify, or distribute this program and accompanying programs and documents for any purpose, provided this copyright notice is retained and prominently displayed, along with a note saying that the original programs are available from our web page. The programs and documents are distributed without any warranty, express or implied. As the programs were written for research purposes only, they have not been tested to the degree that would be advisable in any important application. All use of these programs is entirely at the user's own risk. How to make it work: 1. Create a separate directory and download all these files into the same directory 2. Download from http://yann.lecun.com/exdb/mnist the following 4 files: o train-images-idx3-ubyte.gz o train-labels-idx1-ubyte.gz o t10k-images-idx3-ubyte.gz o t10k-labels-idx1-ubyte.gz 3. Unzip these 4 files by executing: o gunzip train-images-idx3-ubyte.gz o gunzip train-labels-idx1-ubyte.gz o gunzip t10k-images-idx3-ubyte.gz o gunzip t10k-labels-idx1-ubyte.gz If unzipping with WinZip, make sure the file names have not been changed by Winzip. 4. Download Conjugate Gradient code minimize.m 5. Download Autoencoder_Code.tar which contains 13 files OR download each of the following 13 files separately for training an autoencoder and a classification model: o mnistdeepauto.m Main file for training deep autoencoder o mnistclassify.m Main file for training classification model o converter.m Converts raw MNIST digits into matlab format o rbm.m Training RBM with binary hidden and binary visible units o rbmhidlinear.m Training RBM with Gaussian hidden and binary visible units o backprop.m Backpropagation for fine-tuning an autoencoder o backpropclassify.m Backpropagation for classification using "encoder" network o CG_MNIST.m Conjugate Gradient optimization for fine-tuning an autoencoder o CG_CLASSIFY_INIT.m Co
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值