今天是我的第一篇博客,就从最基本的用神经网络识别手写数字mnist数据集开始。。。本博客资源来源于网络,为了提供给自己和刚开始接触机器学习和深度学习的同学参考一下,如有雷同请自行忽略。。。
以下三块程序是初学者可以学习用的,不包含图片预处理和可视化部分,采用CPU运算。
mnist_inference.py代码部分,主要定义了神经网络的结构参数和前向传播的过程。(先上传代码,后期会加上注释)
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 10 11:36:35 2017
@author: cxl
"""
import tensorflow as tf
INPUT_NODE = 784
OUTPUT_NODE = 10
LAYER1_NODE = 500
def get_weight_variable(shape,regularizer):
weights = tf.get_variable("weights",shape,
initializer = tf.truncated_normal_initializer(stddev=0.1))
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)
biases = tf.get_variable("biases",[LAYER1_NODE],
initializer = tf.constant_initializer(0.0))
layer1 = tf.nn.relu(tf.matmul(input_tensor,weights)+biases)
with tf.variable_scope(