lenet-5网络结构
如图所示,下面是经典的lenet-5的网络结构,他包含3层卷积层,两层池化层
lenet-5训练过程
import convolutional
import tensorflow as tf
import data_import
mnist=data_import.Data_import()
#x为图像占位符,y_为标签占位符
x=tf.placeholder(tf.float32,[None,784])
y_=tf.placeholder(tf.float32,[None,10])
#将图片还原为28*28的图片
x_image=tf.reshape(x,[-1,28,28,1])
w_conv1=convolutional.weight_variable([5,5,1,32])#5*5的卷积核,32个卷积核,初始化他们的权重,属于为1个,输出为32个
b_conv1=convolutional.bias_variable([32])#初始化每个卷积核的偏置值
#第一层卷积层+池化层
h_conv1=tf.nn.relu(convolutional.conv2d(x_image,w_conv1)+b_conv1)
h_pool1=convolutional.max_pool(h_conv1)
#第二层卷积和池化
w_conv2=convolutional.weight_variable([5,5,32,64])#32->到64个卷积核
b_conv2=convolutional.bias_variable([64])
h_conv2=tf.nn.relu(convolutional.conv2d(h_pool1,w_conv2)+b_conv2)
h_pool2=convolutional.max_pool(h_conv2)
#全连接层
w_fc1=convolutional.weight_variable([7*7*64,1024])#输入为7*7*64,输出为1024
b_fc1=convolutional.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)
#使用dropout来防止过拟合
keep_prob=tf.placeholder(tf.float32)
h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)
#使用softmax分类
w_fc2=convolutional.weight_variable([1024,10])#输入为1024维,输出为10维
b_fc2=convolutional.bias_variable([10])
y_conv=tf.matmul(h_fc1_drop,w_fc2)+b_fc2#目标函数
#定义交叉熵函数
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits=y_conv))
#优化损失函数,采用梯度下降
train_step=tf.train.AdadeltaOptimizer(1e-4).minimize(cross_entropy)
#真实值与预测值相对比
correct_prediction=tf.equal(tf.arg_max(y_conv,1),tf.arg_max(y_,1))
#计算预测准确率
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
#创建一个绘图
sess=tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
#for i in range(20000):
#batch_xs,batch_ys=mnist.train.next_batch(50)
#sess.run(train_step,feed_dict={x:batch_xs,y_:batch_ys,keep_prob:0.5})#在训练集中取出前100的数据和标签
#真实值与预测值相对比
#correct_prediction=tf.equal(tf.arg_max(y_conv,1),tf.arg_max(y_,1))
#计算预测准确率
#accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
#print(sess.run(accuracy,feed_dict={x:mnist.test.images,y_:mnist.test.labels}))
for i in range(20000):
batch=mnist.train.next_batch(50)
#每一百步报告一次在验证集上的准确率
if i%100==0:
train_accuracy=accuracy.eval(feed_dict={x:batch[0],y_:batch[1],keep_prob:1.0})
# print("step %d,training accuracy is %g"%(i,train_accuracy))
train_step.run(feed_dict={x:batch[0],y_:batch[1],keep_prob:0.5})
print("test accuracy is %g"% accuracy.eval(feed_dict={x:batch[0],y_:batch[1],keep_prob:1.0}))
上面是用lenet5网络训练手写数字的过程。是利用tensorflow实现的。第一层卷积的实现
#将图片还原为28*28的图片
x_image=tf.reshape(x,[-1,28,28,1])
w_conv1=convolutional.weight_variable([5,5,1,32])#5*5的卷积核,32个卷积核,初始化他们的权重,属于为1个,输出为32个
b_conv1=convolutional.bias_variable([32])#初始化每个卷积核的偏置值
#第一层卷积层+池化层
h_conv1=tf.nn.relu(convolutional.conv2d(x_image,w_conv1)+b_conv1)
h_po
第一层池化的实现
h_pool1=convolutional.max_pool(h_conv1)
第二层卷积池化实现方法类似。因为原lenet-5第5层卷积的特征图为1*1,实际即为一个全连接层。在进入第五层卷积想要将第四层池化得到的特征图进行展平
w_fc1=convolutional.weight_variable([7*7*64,1024])#输入为7*7*64,输出为1024
b_fc1=convolutional.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)
#使
最后一层使用softmax分类器将1024个特征图映射到10个类别
w_fc2=convolutional.weight_variable([1024,10])#输入为1024维,输出为10维
b_fc2=convolutional.bias_variable([10])
y_conv=tf.matmul(h_fc1_drop,w_fc2)+b_fc2#目标函数
y_conv即为输出结果标签