全连接神经网络手写体识别Tensorflow
可以说是一个全连接神经网络的探索吧,后面还打算做一下CNN和unet的,其实就是为了应付图像处理的车牌识别作业才临时抱佛脚的QAQ
整个代码基于链接: link.
给代码加入了自己能看得懂的注释,以及标注一些重点函数之后要深入学习的。
神经网络真的挺有趣的,之前学过的智能算法,像是GA等,虽然都是寻找局部最优解,但是前者的核心思想:梯度下降法,使得其更加的泛用,以及可扩展性更高。神经网络(BP)就像一个来回震荡的水波,从输入端发起扰动,在终点施加激励,然后返回输入端的过程中,将其中的结构进行微调,使其更加接近终点的结果。
这个过程有点像Kalman滤波,Kalman滤波的核心思想虽然是方差最小化,但是在寻找预测的参变量更新值的时候,是同时考虑上一个时刻的参变量预报值以及这个时刻输入的观测量所修正的参变量预报值。这个和神经网络(BP)类似,后者也是通过这次的参变量所产生的预测值,和标准值进行比较,并且进一步修改参变量的值,形成更好的预测系统。
不过Kalman滤波有个很大的缺点,那就是需要知道观测量和参变量之间的物理方程联系,而神经网络就完全不用这么麻烦了,其可以说会自适应不同情况下的观测和参变量之间的关系,当然,要说是否满足所有的情况,也不一定,等到后续学习的时候应该会有更加深刻的理解。但是Kalman滤波启动肯定是非常快的,因为ta知道参变量和观测之间的联系,就像开挂一样,知道应该怎么去调整,而神经网络还是要通过大量的训练来找到符合现象的规律。
第一次写博客,在写的过程中不由自主地记下来随便思考得东西,算是杂谈吧。
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 27 10:46:42 2019
@author: wcx
"""
#Tensorflow 2.X need to make a change of "import tensorflow as tf"
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
#If the compiler can't find it,see the link "https://blog.csdn.net/qq_43060552/article/details/103189040"
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./data/", one_hot=True)
step = 20000
learning_rate = 0.001
batch = 128
x = tf.placeholder(tf.float32,shape=[None,784]) #Input layer
y = tf.placeholder(tf.float32,shape=[None,10]) #Output layer
w1 = tf.Variable(tf.random_normal([784,256])) #Weights matrix 1-2
w2 = tf.Variable(tf.random_normal([256,256])) #Weights matrix 2-3
w_out = tf.Variable(tf.random_normal([256,10])) #Weights matrix 3-4
b1 = tf.Variable(tf.random_normal([256])) #Bias vector(row) 1-2
b2 = tf.Variable(tf.random_normal([256])) #Bias vector(row) 2-3
b_out = tf.Variable(tf.random_normal([10])) #Bias vector(row) 3-4
#The nerual network's calculation
def Fully_neural_network(X):
#Layer1's output is X*w1+b1, is a row vector
layer_1 = tf.nn.relu(tf.add(tf.matmul(X,w1),b1))
#Layer2's output is Layer1*w2+b2, a row vector too
layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1,w2),b2))
#final output
layer_out = tf.matmul(layer_2,w_out)+b_out
return layer_out
net_out = Fully_neural_network(x) #x is a row vector
pre = tf.nn.softmax(net_out) #Dividend into one for the whole row
#Calculate the loss funcion,it is consist with two part:
#Softmax and cross entropy with logits(the standard output)
#Calculate the mean value of the inside function
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=net_out,labels = y ))
#Use Adam(adaptive moment estimation) to optimize
optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate)
train_op = optimizer.minimize(loss) #hind the variables of the second part
correct_pre = tf.equal(tf.argmax(pre,1),tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pre,tf.float32))
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(1,step+1):
batch_x, batch_y = mnist.train.next_batch(batch)
sess.run(train_op, feed_dict={x: batch_x, y: batch_y})
if i % 100 == 0 or i == 1:
l, acc = sess.run([loss, accuracy], feed_dict={x: batch_x,
y: batch_y})
print("Step " + str(i) + ", Minibatch Loss= " + "{:.4f}".format(l) + ", Training Accuracy= " + "{:.3f}".format(acc))
print("Optimization Finished!")
# Calculate accuracy for MNIST test images
print("Testing Accuracy:", \
sess.run(accuracy, feed_dict={x: mnist.test.images,
y: mnist.test.labels}))