CNN卷积神经网络
用mnist数据训练卷积神经网络。该卷积网络有两个卷积层,两个池化层,和两个全连接层。
输入:
x = tf.placeholder(tf.float32,[None,784])
y_ = tf.placeholder(tf.float32,[None,10])
input_x = tf.reshape(x,[-1,28,28,1]) #对其进行变形
卷积层和池化层
#卷积核的形状5,5,1,32
conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(input_x,w1,strides=[1,1,1,1],padding='SAME'),b1))
pool1 = tf.nn.max_pool(conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
#卷积核的形状
conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(pool1,w2,strides=[1,1,1,1],padding='SAME'),b2))
pool2 = tf.nn.max_pool(conv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
全连接层
fc_input = tf.reshape(pool2,shape=[-1,7*7*64])
w3 = tf.Variable(tf.truncated_normal([7*7*64,1024],stddev=0.1))
b3 = tf.Variable(tf.constant(0.1,shape=[1024]))
w4 = tf.Variable(tf.truncated_normal([1024,10],stddev=0.1))
b4 = tf.Variable(tf.constant(0.1,shape=[10]))
fc1 = tf.nn.relu(tf.matmul(fc_input,w3)+b3)
fc1_dropout = tf.nn.dropout(fc1,0.25)
y = tf.nn.softmax(tf.matmul(fc1_dropout,w4)+b4)
损失函数和优化算法
loss = - tf.reduce_mean(tf.reduce_sum(y_*tf.log(y)))
optim = tf.train.AdamOptimizer(0.001).minimize(loss)
计算模型的准确率
prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1))
accu = tf.reduce_mean(tf.cast(prediction,tf.float32))
附录
# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
"""
def conv2d(input_,output_size,strides=2,name='deconv2d'):
with tf.variable_scope(name,reuse=reuse):
w = tf.get_variable('w',shape=[w,w,input_.shape[-1],output_size],initializer=tf.truncated_normal_initializer=stddev)
net = tf.nn.conv2d(input_,w,strides=[1,strides,strides,1],padding='SAME')
return net
"""
batch_size = 64
x = tf.placeholder(tf.float32,[None,784])
y_ = tf.placeholder(tf.float32,[None,10])
w1 = tf.Variable(tf.truncated_normal([5,5,1,32],stddev=0.1))
b1 = tf.Variable(tf.constant(0.1,shape=[32]))
w2 = tf.Variable(tf.truncated_normal([5,5,32,64],stddev=0.1))
b2 = tf.Variable(tf.constant(0.1,shape=[64]))
input_x = tf.reshape(x,[-1,28,28,1])
conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(input_x,w1,strides=[1,1,1,1],padding='SAME'),b1))
pool1 = tf.nn.max_pool(conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(pool1,w2,strides=[1,1,1,1],padding='SAME'),b2))
pool2 = tf.nn.max_pool(conv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
fc_input = tf.reshape(pool2,shape=[-1,7*7*64])
w3 = tf.Variable(tf.truncated_normal([7*7*64,1024],stddev=0.1))
b3 = tf.Variable(tf.constant(0.1,shape=[1024]))
w4 = tf.Variable(tf.truncated_normal([1024,10],stddev=0.1))
b4 = tf.Variable(tf.constant(0.1,shape=[10]))
fc1 = tf.nn.relu(tf.matmul(fc_input,w3)+b3)
fc1_dropout = tf.nn.dropout(fc1,0.25)
y = tf.nn.softmax(tf.matmul(fc1_dropout,w4)+b4)
loss = - tf.reduce_mean(tf.reduce_sum(y_*tf.log(y)))
prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1))
accu = tf.reduce_mean(tf.cast(prediction,tf.float32))
optim = tf.train.AdamOptimizer(0.001).minimize(loss)
def train():
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(10000):
batch_x,batch_y = mnist.train.next_batch(batch_size)
sess.run(optim,feed_dict={x:batch_x,y_:batch_y})
if(i%300==0):
accuracy = sess.run(accu,feed_dict={x:batch_x,y_:batch_y})
print("after %d trains ,the accuracy is %g"%(i,accuracy))
def main(argv=None):
train()
if __name__ == '__main__':
tf.app.run()