网络结构:
使用2个卷积层,2个全连接层
layer1:conv2d, relu, maxpooling, dropout
layer2: conv2d,batch_normalization, maxpooling, dropout
fc1: 全连接, relu, dropout
fc2: 全连接 ,softmax
weights={
'wc1':tf.Variable(tf.random_normal([3,3,1,64],stddev=0.1)),
'wc2':tf.Variable(tf.random_normal([3,3,64,128],stddev=0.1)),
'wd1':tf.Variable(tf.random_normal([2*2*128,1024],stddev=0.1)),
'wd2':tf.Variable(tf.random_normal([1024,n_output],stddev=0.1))
}
bias={
'bc1':tf.Variable(tf.random_normal([64],stddev=.1)),
'bc2':tf.Variable(tf.random_normal([128],stddev=.1)),
'bd1':tf.Variable(tf.random_normal([1024],stddev=.1)),
'bd2':tf.Variable(tf.random_normal([n_output],stddev=.1))
}
#%%
def conv_basic(_input,_w,_b,_keepratio):
'input'
_input_r=tf.reshape(_input,shape=[-1,28,28,1])
'conv layer 1'
_conv1=tf.nn.conv2d(_input_r,_w['wc1'],strides=[1,2,2,1],padding='SAME')
#_mean,_var=tf.nn.moments(_conv1,[0,1,2])
#_conv1=tf.nn.batch_normalization(_conv1,_mean,_var,0,1,.0001)
_conv1=tf.nn.relu(tf.nn.bias_add(_conv1,bias['bc1']))
_pool1=tf.nn.max_pool(_conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
_pool1_dr=tf.nn.dropout(_pool1,_keepratio)
'conv layer2'
_conv2=tf.nn.conv2d(_pool1_dr,_w['wc2'],strides=[1,2,2,1],padding='SAME')
_mean,_var=tf.nn.moments(_conv2,[0,1,2])
_conv2=tf.nn.batch_normalization(_conv2,_mean,_var,0,1,.0001)
_pool2=tf.nn.max_pool(_conv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
_pool2_dr=tf.nn.dropout(_pool2,_keepratio)
'vectorize'
_dense1=tf.reshape(_pool2_dr,[-1,_w['wd1'].get_shape().as_list()[0]])
'fully connected layer 1'
_fc1=tf.nn.relu(tf.add(tf.matmul(_dense1,_w['wd1']),_b['bd1']))
_fc1_dr=tf.nn.dropout(_fc1,_keepratio)
'fully connected layer 2'
_out=tf.add(tf.matmul(_fc1_dr,_w['wd2']),_b['bd2'])
out={
'input_r':_input_r,'conv1':_conv1,'pool1':_pool1,'pool_dr1':_pool1_dr,
'conv2':_conv2,'pool_dr2':_pool2_dr,'dense1':_dense1,'fc1':_fc1,'fc_dr1':_fc1_dr,
'out':_out
}
return out
完整代码:
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 15 17:07:51 2018
@author:
"""
import numpy as np
import tensorflow as tf
import input_data
#%%
mnist=input_data.read_data_sets('data/',one_hot=True)
trainimg = mnist.train.images
trainlabel=mnist.train.labels
testimg=mnist.test.images
testlabel=mnist.test.labels
print ("MNIST ready")
#%%
n_input=784
n_output=10
weights={
'wc1':tf.Variable(tf.random_normal([3,3,1,64],stddev=0.1)),
'wc2':tf.Variable(tf.random_normal([3,3,64,128],stddev=0.1)),
'wd1':tf.Variable(tf.random_normal([2*2*128,1024],stddev=0.1)),
'wd2':tf.Variable(tf.random_normal([1024,n_output],stddev=0.1))
}
bias={
'bc1':tf.Variable(tf.random_normal([64],stddev=.1)),
'bc2':tf.Variable(tf.random_normal([128],stddev=.1)),
'bd1':tf.Variable(tf.random_normal([1024],stddev=.1)),
'bd2':tf.Variable(tf.random_normal([n_output],stddev=.1))
}
#%%
def conv_basic(_input,_w,_b,_keepratio):
'input'
_input_r=tf.reshape(_input,shape=[-1,28,28,1])
'conv layer 1'
_conv1=tf.nn.conv2d(_input_r,_w['wc1'],strides=[1,2,2,1],padding='SAME')
#_mean,_var=tf.nn.moments(_conv1,[0,1,2])
#_conv1=tf.nn.batch_normalization(_conv1,_mean,_var,0,1,.0001)
_conv1=tf.nn.relu(tf.nn.bias_add(_conv1,bias['bc1']))
_pool1=tf.nn.max_pool(_conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
_pool1_dr=tf.nn.dropout(_pool1,_keepratio)
'conv layer2'
_conv2=tf.nn.conv2d(_pool1_dr,_w['wc2'],strides=[1,2,2,1],padding='SAME')
_mean,_var=tf.nn.moments(_conv2,[0,1,2])
_conv2=tf.nn.batch_normalization(_conv2,_mean,_var,0,1,.0001)
_pool2=tf.nn.max_pool(_conv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
_pool2_dr=tf.nn.dropout(_pool2,_keepratio)
'vectorize'
_dense1=tf.reshape(_pool2_dr,[-1,_w['wd1'].get_shape().as_list()[0]])
'fully connected layer 1'
_fc1=tf.nn.relu(tf.add(tf.matmul(_dense1,_w['wd1']),_b['bd1']))
_fc1_dr=tf.nn.dropout(_fc1,_keepratio)
'fully connected layer 2'
_out=tf.add(tf.matmul(_fc1_dr,_w['wd2']),_b['bd2'])
out={
'input_r':_input_r,'conv1':_conv1,'pool1':_pool1,'pool_dr1':_pool1_dr,
'conv2':_conv2,'pool_dr2':_pool2_dr,'dense1':_dense1,'fc1':_fc1,'fc_dr1':_fc1_dr,
'out':_out
}
return out
print ("CNN READY")
#%%
x=tf.placeholder(tf.float32,[None,n_input])
y=tf.placeholder(tf.float32,[None,n_output])
keepratio=tf.placeholder(tf.float32)
# FUNCTIONS
_pred=conv_basic(x,weights,bias,keepratio)['out']
_pred_all=conv_basic(x,weights,bias,keepratio)
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=_pred,labels=y))
optm=tf.train.AdamOptimizer(learning_rate=.0001).minimize(cost)
_corr=tf.equal(tf.argmax(_pred,1),tf.argmax(y,1))
accr=tf.reduce_mean(tf.cast(_corr,tf.float32))
init=tf.global_variables_initializer()
# SAVER
print ("GRAPH READY")
#%%
sess=tf.Session()
sess.run(init)
training_epochs=100
batch_size=16
display_step=1
for epoch in range(training_epochs):
avg_cost=0.
total_batch=10
for i in range(total_batch):
batch_xs,batch_ys=mnist.train.next_batch(batch_size)
sess.run(optm,feed_dict={x:batch_xs,y:batch_ys,keepratio:.7})
avg_cost+=sess.run(accr,feed_dict={x:batch_xs,y:batch_ys,keepratio:1.})
if epoch % display_step==0:
print ("Epoch: %03d/%03d cost: %.9f" % (epoch, training_epochs, avg_cost))
train_acc = sess.run(accr, feed_dict={x: batch_xs, y: batch_ys, keepratio:1.})
print (" Training accuracy: %.3f" % (train_acc))
#test_acc = sess.run(accr, feed_dict={x: testimg, y: testlabel, keepratio:1.})
#print (" Test accuracy: %.3f" % (test_acc))
print ("OPTIMIZATION FINISHED")
#%%
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
MNIST Ready!
CNN ready!!
graph ready
Epoch: 100/000 cost:1.701020661
train accuracy:0.9300 test accuracy:0.9500
Epoch: 100/001 cost:0.418071359
train accuracy:0.9750 test accuracy:0.9550
Epoch: 100/002 cost:0.272725333
train accuracy:0.9600 test accuracy:0.9600
Epoch: 100/003 cost:0.218292159
train accuracy:0.9450 test accuracy:0.9750
Epoch: 100/004 cost:0.172331328
train accuracy:0.9800 test accuracy:0.9950
Epoch: 100/005 cost:0.151555697
train accuracy:0.9900 test accuracy:0.9700
Epoch: 100/006 cost:0.134637995
train accuracy:0.9850 test accuracy:0.9850
Epoch: 100/007 cost:0.120957906
train accuracy:0.9900 test accuracy:0.9850
Epoch: 100/008 cost:0.109265696
train accuracy:0.9950 test accuracy:0.9600
Epoch: 100/009 cost:0.099880041
train accuracy:0.9950 test accuracy:0.9750
Epoch: 100/010 cost:0.100129452
train accuracy:0.9800 test accuracy:1.0000