如果是为了学习,可以参考:https://www.cnblogs.com/Ran-Chen/p/9220739.html
这里只做一个问题记录。
基于CNN的MNIST数据集识别。
关于MNIST数据集:http://yann.lecun.com/exdb/mnist/
关于导包问题1
import input_data
把input_data复制出来放在当前项目文件下。
关于tensorflow导包
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
这样不会报错。
关于tensorboad,一个可视化工具。
打开cmd,运行下面。最后的代表log路径。(log是程序运行后保存的)
tensorboard --logdir=C:\Users\Administrator\log
全部代码
import input_data
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
print("start")
#权重初始化
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
#偏置项初始化
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
#卷积
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
#max池化
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# get the data source
mnist = input_data.read_data_sets("MNIST/", one_hot=True)
# input image:pixel 28*28 = 784
with tf.name_scope('input'):
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder('float', [None, 10]) # y_ is realistic result
with tf.name_scope('image'):
x_image = tf.reshape(x, [-1, 28, 28, 1]) # any dim, width, height, channel(depth)
tf.summary.image('input_image', x_image, 8)
# 卷积层1 the first convolution layer
with tf.name_scope('conv_layer1'):
W_conv1 = weight_variable([5, 5, 1, 32]) # convolution kernel: 5*5*1, number of kernel: 32
b_conv1 = bias_variable([32])
#卷积核与输入的x_image进行卷积,并通过relu激活函数,再最大池化处理
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # make convolution, output: 28*28*32
#最大池化
with tf.name_scope('pooling_layer'):
h_pool1 = max_pool_2x2(h_conv1) # make pooling, output: 14*14*32
# 卷积层2 the second convolution layer
with tf.name_scope('conv_layer2'):
W_conv2 = weight_variable([5, 5, 32, 64]) # convolution kernel: 5*5, depth: 32, number of kernel: 64
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) # output: 14*14*64
with tf.name_scope('pooling_layer'):
h_pool2 = max_pool_2x2(h_conv2) # output: 7*7*64
# 全连接层1 the first fully connected layer
with tf.name_scope('fc_layer3'):
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024]) # size: 1*1024
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # output: 1*1024
# dropout
with tf.name_scope('dropout'):
keep_prob = tf.placeholder(tf.float32)
tf.summary.scalar('dropout_keep_probability', keep_prob)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# the second fully connected layer
# train the model: y = softmax(x * w + b)
with tf.name_scope('output_fc_layer4'):
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10]) # size: 1*10
with tf.name_scope('softmax'):
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) # output: 1*10
with tf.name_scope('lost'):
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
tf.summary.scalar('lost', cross_entropy)
with tf.name_scope('train'):
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
tf.summary.scalar('accuracy', accuracy)
merged = tf.summary.merge_all()
train_summary = tf.summary.FileWriter(r'./log', tf.get_default_graph())
# init all variables
init = tf.global_variables_initializer()
# run session
with tf.Session() as sess:
sess.run(init)
# train data: get w and b
for i in range(2000): # train 2000 times
batch = mnist.train.next_batch(50)
result, _ = sess.run([merged, train_step], feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
# train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
if i % 100 == 0:
# train_accuracy = sess.run(accuracy, feed_dict)
train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0}) # no dropout
print('step %d, training accuracy %g' % (i, train_accuracy))
# result = sess.run(merged, feed_dict={x: batch[0], y_: batch[1]})
train_summary.add_summary(result, i)
train_summary.close()
print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
# open tensor_board in windows-cmd
# tensorboard --logdir=C:\Users\Administrator\tf