莫烦Tensorflow学习代码九(手写数字识别MNIST CNN学习)
作者:互联网
在之前的学习基础上使用卷积神经网络CNN的训练,准确率提升了许多。
与之前不同的地方就是添加了两层卷积神经,之前学习理论没弄明白的全连接层写完这个代码后也弄明白了。
而且运用了dropout解决过拟合问题。
最后准确率达到了0.9688,比之前0.87还是要高不少
以下是重要代码
一、定义conv2d和max_pool_2x2函数
x是输入,W是权重,stride=[1,1,1,1]是前后上下步长都为1,padding=‘SAME’,指卷积后输出的大小不变,假设原图像是28x28,那么输出还是28x28,padding使用的话能够更好的保留图像特征,如果有不懂的小伙伴还请自行搜索,
def conv2d(x,W):
#步长[1,x,y,1]
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
池化过程:x是输入,ksize是池化核的大小为2x2,另外两个参数同conv2d
def max_pool_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
二、两个卷积层
第一层是32个卷积核,大小是5x5,因为是灰度图,所以卷积核是单通道,bias也是32个。
第二层是64个卷积核,卷积核的数量是自己定的,但是是32通道,这是由上一层的输出即这一层的输入决定,由于输入是32,所以有32通道,大小同样设置成5x5.
两层都要用relu函数,然后池化,这里用的是max_pool.池化后的每个输出缩小为原来0.5倍。
拿第一个举例说一下输出。
首先输入28x28的单通道的图像,经过卷积输出28x28的32通道,池化后变为14x14x32,32是32通道
#conv1
W_conv1 = weight_variable([5,5,1,32])#卷积核大小5x5,单通道,32个卷积核,卷积核个数一般为2的倍数
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
#conv2
W_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
最后池化输出的是7x7x64
二、全连接层
第一层先处理卷积层流过来的数据,将其reshape成一维7764,输出是一维1024大小,之后用relu函数,最后解决过拟合
第二层就是输出层了,输入1024行一列,输出10行一列,这一层的激励函数用softmax,因为是多分类任务
##func1 layer
W_fc1=weight_variable([7*7*64,1024])
b_fc1 = bias_variable([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)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
#func2_layer
W_fc2 = weight_variable([1024,10])
b_fc2 = bias_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
最后总代码
import random
import tensorflow as tff
import tensorflow._api.v2.compat.v1 as tf
from keras.utils.np_utils import to_categorical
import matplotlib.pyplot as plt
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
tf.disable_v2_behavior()
(train_images, train_labels), (test_images, test_labels) = tff.keras.datasets.mnist.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0#图像归一化
train_labels, test_labels = to_categorical(train_labels), to_categorical(test_labels)
#转换成784列
train_images = train_images.reshape([-1, 784])
test_images = test_images.reshape([-1, 784])
def compute_accuracy(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict={xs: v_xs,keep_prob:1})
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))#如果预测值和测试的真实值的每一行最大元素相同则为True
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))#转换为float32后算平均值得出准确率
return sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys,keep_prob:1})
def weight_variable(shape):
inital = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(inital)
def bias_variable(shape):
inital = tf.constant(0.1, shape=shape)
return tf.Variable(inital)
def conv2d(x,W):
#步长[1,x,y,1]
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')#ksize是池化核大小2x2
xs = tf.placeholder(tf.float32, [None, 784])
ys = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
x_image=tf.reshape(xs,[-1,28,28,1])
#conv1
W_conv1 = weight_variable([5,5,1,32])#卷积核大小5x5,单通道,32个卷积核,卷积核个数一般为2的倍数
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
#conv2
W_conv2 = weight_variable([5,5,32,64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
##func1 layer
W_fc1=weight_variable([7*7*64,1024])
b_fc1 = bias_variable([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)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
#func2_layer
W_fc2 = weight_variable([1024,10])
b_fc2 = bias_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(1000):
index = random.sample(range(train_images.shape[0]), 100)
batch_xs = train_images[index,]
batch_ys = train_labels[index,]
sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys,keep_prob:0.5})
if i % 50 == 0:
print(compute_accuracy(test_images, test_labels))
标签:fc1,卷积,32,variable,train,tf,CNN,Tensorflow,MNIST 来源: https://blog.csdn.net/weixin_45164735/article/details/122647804