第1关:Tensorflow-构建手写字识别模型
# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]='3'
'''设置输入层节点个数为784
设置输出个数为10
'''
#********* Begin *********#
INPUT_NODE = 784
OUTPUT_NODE = 10
#********* End **********#
# 配置神经网络的参数
'''设置隐藏层节点个数为500,每轮喂入数据个数为100'''
LAYER1_NODE = 500
# 一次训练数据的个数
BATCH_SIZE = 100
#********* Begin *********#
# 隐藏层结点数
LAYER1_NODE = 500
# 一次训练数据的个数
BATCH_SIZE = 100
#********* End **********#
LEARNING_RATE_BASE = 0.8 # 基础的学习率
LEARNING_RATE_DECAY = 0.99 # 学习率的衰减率
REGULARIZATION_RATE = 0.0001 # 正则化项的系数
TRAINING_STEPS = 3000 # 训练轮
def get_regularizer_variable(weights, regularizer):
if regularizer != None:
tf.add_to_collection("losses", regularizer(weights))
def forward(input_tensor, regularizer):
#隐藏层参数定义与输出[784,500]
weights1 = tf.Variable(tf.truncated_normal([INPUT_NODE, LAYER1_NODE], stddev=0.1))
biases1 = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODE]))
get_regularizer_variable(weights1, regularizer)
#input_tensor[None,784],[784,500]
layer1 = tf.nn.relu(tf.matmul(input_tensor, weights1) + biases1)
# 输出层参数与输出
weights2 = tf.Variable(tf.truncated_normal([LAYER1_NODE, OUTPUT_NODE], stddev=0.1))
biases2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE]))
get_regularizer_variable(weights2, regularizer)
ouput = tf.matmul(layer1, weights2) + biases2
return ouput
if __name__=="__main__":#程序运行的起点
#读取mnist数据集[0,1,0,0,0,0,0,0,0,0]
#********* Begin *********#
mnist = input_data.read_data_sets("data", one_hot=True)
#********* End **********#
#定义输入层节点,None指的是每次向神经网络中喂入图像的张数
x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input')
#神经网络的输出,有10个节点
y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input')
regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
y= forward(x,regularizer)
#计算预测值y与真实值y_的交叉熵
#********* Begin *********#
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_,1))
#********* End **********#
cross_entropy_mean = tf.reduce_mean(cross_entropy) # 计算batch所有交叉熵的均值
loss = cross_entropy_mean + tf.add_n(tf.get_collection("losses"))
global_step = tf.Variable(0, trainable=False)
#用指数衰减函数设置学习率
#********* Begin *********#
learning_rate = tf.train.exponential_decay(LEARNING_RATE_BASE,global_step,mnist.train.num_examples/BATCH_SIZE,LEARNING_RATE_DECAY)
#********* End **********#
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
for i in range(TRAINING_STEPS):
xs, ys = mnist.train.next_batch(BATCH_SIZE)
_, _, _ = sess.run([train_step, loss, global_step], feed_dict={x: xs, y_: ys})
if (i % 1000 == 0):
sess.run(loss, feed_dict={x: xs, y_: ys})
print("模型正确运行")