当前位置:网站首页>CNN MNIST handwriting recognition
CNN MNIST handwriting recognition
2022-07-03 12:34:00 【kvnew】
#!/usr/bin/python3
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)# Read picture data set
sess = tf.InteractiveSession()# establish session
# One , Function declaration part
def weight_variable(shape):
# Normal distribution , The standard deviation is 0.1, Default maximum is 1, The minimum is -1, The mean for 0
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
# Create a structure for shape A matrix can also be said to be an array shape Declare its ranks , Initialize all values to 0.1
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
# The number of convolution steps in each direction 1,SAME: Automatic filling outside the edge 0, Ergodic multiplication
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
# Pooled convolution results (conv2d) The pool layer adopts kernel The size is 2*2, The number of steps is also 2, Peripheral supplement 0, Taking the maximum . The amount of data has shrunk 4 times
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME')
# Two , Define the input / output structure
# Declare a placeholder ,None Indicates that the number of input pictures is variable ,28*28 Image resolution
xs = tf.placeholder(tf.float32, [None, 28*28])
# Category is 0-9 in total 10 Categories , Corresponding output classification result
ys = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
# x_image Also put xs reshape a 28*28*1 The shape of the , Because it's a gray picture , So the passage is 1. As a training input,-1 The number of pictures is variable
x_image = tf.reshape(xs, [-1, 28, 28, 1])
# 3、 ... and , Build network , Define the algorithm formula , That is to say forward The calculation of time
## The first convolution operation ##
# The first and second parameters are the convolution kernel size , namely patch, The third parameter is the number of image channels , The fourth parameter is the number of convolution kernels , Represents how many convolution feature images will appear ;
W_conv1 = weight_variable([5, 5, 1, 32])
# For each convolution kernel, there is a corresponding offset .
b_conv1 = bias_variable([32])
# Multiply the picture by the convolution kernel , And add paranoia , Convolution results 28x28x32
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
# Pooling results 14x14x32 Multiply the convolution result by the pooled convolution kernel
h_pool1 = max_pool_2x2(h_conv1)
## Second layer convolution operation ##
# 32 Channel convolution , Convolution 64 Features
w_conv2 = weight_variable([5,5,32,64])
# 64 Paranoid data
b_conv2 = bias_variable([64])
# Be careful h_pool1 It is the pool result of the upper layer ,# Convolution results 14x14x64
h_conv2 = tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)
# Pooling results 7x7x64
h_pool2 = max_pool_2x2(h_conv2)
# Original image size 28*28, The first image is reduced to 14*14, share 32 Zhang , After the second round, the image is reduced to 7*7, share 64 Zhang
## Third layer full connection operation ##
# Two dimensional tensor , The first parameter 7*7*64 Of patch, It can also be thought that there is only one line 7*7*64 Convolution of data , The second parameter represents the total number of convolutions 1024 individual
W_fc1 = weight_variable([7*7*64, 1024])
# 1024 Paranoid data
b_fc1 = bias_variable([1024])
# Pool the convolution result of the second layer reshape Cheng has only one line 7*7*64 Data # [n_samples, 7, 7, 64] ->> [n_samples, 7*7*64]
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
# Convolution operation , The result is 1*1*1024, A single row multiplied by a single column equals 1*1 matrix ,matmul Realize the most basic matrix multiplication , differ tf.nn.conv2d Traversal multiplication of , It is automatically regarded as the forward vector and the backward vector
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
# dropout operation , Reduce overfitting , In fact, it is to reduce the weight of some inputs on the upper layer scale, Even set it to 0, Increase the weight of some inputs , Even set it to 2, Prevent the evaluation curve from shaking , I think it is necessary when there are few samples
# Use placeholders , from dropout Automatically determine scale, You can also customize it , such as 0.5, according to tensorflow It can be seen from the documents , The actual value used in the program is 1/0.5=2, That is, some inputs are multiplied by 2, At the same time, some inputs are multiplied by 0
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob) # Perform... On the convolution result dropout operation
## Fourth layer output operation ##
# Two dimensional tensor ,1*1024 matrix convolution , common 10 A convolution , Corresponding to what we started ys The length is 10
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
# The final classification , The result is 1*1*10 softmax and sigmoid It's all based on logistic Classification algorithm , One is multi classification, the other is two classification
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
# Four , Definition loss( Minimum error probability ), Select optimization optimization loss,
cross_entropy = -tf.reduce_sum(ys * tf.log(y_conv)) # Define the cross entropy as loss function
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) # Call the optimizer to optimize , In fact, it is through feeding data to win cross_entropy To minimize the
# 5、 ... and , Start data training and evaluation
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
tf.global_variables_initializer().run()
for i in range(20000):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={x:batch[0], ys: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], ys: batch[1], keep_prob: 0.5})
print("test accuracy %g"%accuracy.eval(feed_dict={x: mnist.test.images, ys: mnist.test.labels, keep_prob: 1.0}))
边栏推荐
- [ManageEngine] the role of IP address scanning
- 手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
- Use of QT OpenGL camera
- SLF4J 日志门面
- [combinatorics] permutation and combination (example of permutation and combination)
- Adult adult adult
- 20. Valid brackets
- flinksql是可以直接客户端建表读mysql或是kafka数据,但是怎么让它自动流转计算起来呢?
- How to deploy web pages to Alibaba cloud
- 2.7 overview of livedata knowledge points
猜你喜欢

Summary of error prone knowledge points: Calculation of define s (x) 3*x*x+1.

Swagger

C language improvement article (wchar_t) character type
![[MySQL special] read lock and write lock](/img/ac/e01c26882cc664ea2e5e731c5a8bab.png)
[MySQL special] read lock and write lock
![[download attached] password acquisition tool lazagne installation and use](/img/21/eccf87ad9946d4177b600d96e17322.png)
[download attached] password acquisition tool lazagne installation and use

QT OpenGL texture map

If you can't learn, you have to learn. Jetpack compose writes an im app (II)

1-2 project technology selection and structure

ES6新特性

Sword finger offer07 Rebuild binary tree
随机推荐
2.6 preliminary cognition of synergetic couroutines
网上炒股开户安不安全?谁给回答一下
Is it safe to open an account for online stock speculation? Who can answer
init. RC service failed to start
What is more elegant for flutter to log out and confirm again?
Flutter: about monitoring on flutter applications
Sword finger offer10- I. Fibonacci sequence
Wechat applet development - page Jump transfer parameters
(construction notes) learning experience of MIT reading
257. All paths of binary tree
Implement verification code verification
4000字超详解指针
Shutter: about inheritedwidget
Integer string int mutual conversion
Cloud Computing future - native Cloud
Alibaba is bigger than sending SMS (user microservice - message microservice)
Symlink(): solution to protocol error in PHP artisan storage:link on win10
在网上炒股开户可以吗?资金安全吗?
145. Post order traversal of binary tree
elastic_ L02_ install