TensorFlow的mnist例子
通过几个不同神经网络模型评估训练结果
单层网络 (梯度下降)
只有输入和输出
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("mnist/",one_hot=True)
print(mnist.train.images.shape,mnist.train.labels.shape)
sess=tf.InteractiveSession()
x=tf.placeholder(tf.float32,[None,784])
w=tf.Variable(tf.zeros([784,10]))
b=tf.Variable(tf.zeros([10]))
y=tf.nn.softmax(tf.matmul(x,w)+b)
y_=tf.placeholder(tf.float32,[None,10])
corss_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
train=tf.train.GradientDescentOptimizer(0.5).minimize(corss_entropy)
tf.global_variables_initializer().run()
for i in range(1000):
batch_xs,batch_ys=mnist.train.next_batch(100)
train.run({x:batch_xs,y_:batch_ys})
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels}))
多层神经网络(全连接 MLP)
这里使用2个隐藏层
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("mnist/",one_hot=True)
sess=tf.InteractiveSession()
in_units=784
h1_units=300
h2_units=200
w1=tf.Variable(tf.truncated_normal([in_units,h1_units],stddev=0.1))
w0=tf.Variable(tf.truncated_normal([h1_units,h2_units],stddev=0.1))
b1=tf.Variable(tf.zeros([h1_units]))
b0=tf.Variable(tf.zeros([h2_units]))
w2=tf.Variable(tf.zeros([h2_units,10]))
b2=tf.Variable(tf.zeros([10]))
x=tf.placeholder(tf.float32,[None,in_units])
keep_prob=tf.placeholder(tf.float32)
hidden1=tf.nn.relu(tf.matmul(x,w1)+b1)
hidden1_drop=tf.nn.dropout(hidden1,keep_prob)
hidden0=tf.nn.relu(tf.matmul(hidden1,w0)+b0)
#
hidden0_drop=tf.nn.dropout(hidden0,keep_prob)
y=tf.nn.softmax(tf.matmul(hidden0,w2)+b2)
#
y_=tf.placeholder(tf.float32,[None,10])
corss_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
train=tf.train.AdagradOptimizer(0.1).minimize(corss_entropy)
tf.global_variables_initializer().run()
for i in range(3000):
batch_xs,batch_ys=mnist.train.next_batch(100)
train.run({x:batch_xs,y_:batch_ys,keep_prob:0.75})
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0}))
卷积神经网络(卷积)
这里训练好后固化模型供以后使用
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("mnist/",one_hot=True)
sess=tf.InteractiveSession()
def weight_variable(shape,name):
initial=tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(initial,name=name)
def bias_variable(shape,name):
initial=tf.constant(0.1,shape=shape)
return tf.Variable(initial,name=name)
def conv2d(x,W):
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
def max_pool(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
x=tf.placeholder(tf.float32,[None,784],name='x')
y_=tf.placeholder(tf.float32,[None,10],name='y')
#
x_image=tf.reshape(x,[-1,28,28,1])
w_conv1=weight_variable([5,5,1,32],'w_conv1')
b_conv1=bias_variable([32],'b_conv1')
h_conv1=tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1)
h_pool1=max_pool(h_conv1)
#
w_conv2=weight_variable([5,5,32,64],'w_conv2')
#
b_conv2=bias_variable([64],'b_conv2')
#
h_conv2=tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)
#
h_pool2=max_pool(h_conv2)
w_fc1=weight_variable([7*7*64,1024],'w_fc1')
b_fc1=bias_variable([1024],'b_fc1')
h_pool_flat=tf.reshape(h_pool2,[-1,7*7*64])
h_fc1=tf.nn.relu(tf.matmul(h_pool_flat,w_fc1)+b_fc1)
keep_prob=tf.placeholder(tf.float32,name='keep_prob')
h_drop=tf.nn.dropout(h_fc1,keep_prob)
w_fc2=weight_variable([1024,10],'w_fc2')
b_fc2=bias_variable([10],'b_fc2')
y_conv=tf.nn.softmax(tf.matmul(h_drop,w_fc2)+b_fc2)
corss_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv),reduction_indices=[1]))
train=tf.train.AdamOptimizer(1e-4).minimize(corss_entropy)
tf.global_variables_initializer().run()
correct_prediction=tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
for i in range(10000):
batch_xs,batch_ys=mnist.train.next_batch(50)
if i%100==0:
train_accuracy=accuracy.eval({x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0})
print(i,train_accuracy)
train.run({x:batch_xs,y_:batch_ys,keep_prob:0.5})
saver = tf.train.Saver()
saver.save(sess,'save/model.ckpt',global_step=1000)
print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0}))
使用刚刚训练好的模型
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("mnist/",one_hot=True)
sess=tf.InteractiveSession()
def conv2d(x,W):
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
def max_pool(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
y_=tf.placeholder(tf.float32,[None,10],name='y')
x=tf.placeholder(tf.float32,[None,784],name='x')
keep_prob=tf.placeholder(tf.float32,name='keep_prob')
#First let's load meta graph and restore weights
saver = tf.train.import_meta_graph('save/model.ckpt-1000.meta')
saver.restore(sess,tf.train.latest_checkpoint('save/'))
graph = tf.get_default_graph()
x_image=tf.reshape(x,[-1,28,28,1])
w_conv1=graph.get_tensor_by_name('w_conv1:0')
b_conv1=graph.get_tensor_by_name('b_conv1:0')
h_conv1=tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1)
w_conv2=graph.get_tensor_by_name('w_conv2:0')
b_conv2=graph.get_tensor_by_name('b_conv2:0')
h_pool1=max_pool(h_conv1)
h_conv2=tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)
h_pool2=max_pool(h_conv2)
h_pool_flat=tf.reshape(h_pool2,[-1,7*7*64])
w_fc1=graph.get_tensor_by_name('w_fc1:0')
b_fc1=graph.get_tensor_by_name('b_fc1:0')
h_fc1=tf.nn.relu(tf.matmul(h_pool_flat,w_fc1)+b_fc1)
h_drop=tf.nn.dropout(h_fc1,keep_prob)
b_fc2=graph.get_tensor_by_name('b_fc2:0')
w_fc2=graph.get_tensor_by_name('w_fc2:0')
y_conv=tf.nn.softmax(tf.matmul(h_drop,w_fc2)+b_fc2)
correct_prediction=tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
train_accuracy = accuracy.eval({x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})
print(train_accuracy)
识别自己手写的图片
代码照之前稍做改动
import tensorflow as tf
from PIL import Image, ImageFilter
import matplotlib.pyplot as plt
# from tensorflow.examples.tutorials.mnist import input_data
#
# mnist=input_data.read_data_sets("mnist/",one_hot=True)
sess=tf.InteractiveSession(config=tf.ConfigProto(allow_soft_placement=True))
def imageprepare():
im = Image.open('8.png') #读取的图片所在路径,注意是28*28像素
plt.imshow(im) #显示需要识别的图片
plt.show()
im = im.convert('L')
tv = list(im.getdata())
tva = [(255-x)*1.0/255.0 for x in tv]
return tva
result=imageprepare()
def conv2d(x,W):
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
def max_pool(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
y_=tf.placeholder(tf.float32,[None,10],name='y')
x=tf.placeholder(tf.float32,[None,784],name='x')
keep_prob=tf.placeholder(tf.float32,name='keep_prob')
#First let's load meta graph and restore weights
saver = tf.train.import_meta_graph('save/model.ckpt-1000.meta')
saver.restore(sess,tf.train.latest_checkpoint('save/'))
graph = tf.get_default_graph()
x_image=tf.reshape(x,[-1,28,28,1])
w_conv1=graph.get_tensor_by_name('w_conv1:0')
b_conv1=graph.get_tensor_by_name('b_conv1:0')
h_conv1=tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1)
w_conv2=graph.get_tensor_by_name('w_conv2:0')
b_conv2=graph.get_tensor_by_name('b_conv2:0')
h_pool1=max_pool(h_conv1)
h_conv2=tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)
h_pool2=max_pool(h_conv2)
h_pool_flat=tf.reshape(h_pool2,[-1,7*7*64])
w_fc1=graph.get_tensor_by_name('w_fc1:0')
b_fc1=graph.get_tensor_by_name('b_fc1:0')
h_fc1=tf.nn.relu(tf.matmul(h_pool_flat,w_fc1)+b_fc1)
h_drop=tf.nn.dropout(h_fc1,keep_prob)
b_fc2=graph.get_tensor_by_name('b_fc2:0')
w_fc2=graph.get_tensor_by_name('w_fc2:0')
y_conv=tf.nn.softmax(tf.matmul(h_drop,w_fc2)+b_fc2)
prediction=tf.argmax(y_conv,1)
predint = prediction.eval(feed_dict={x: [result], keep_prob: 1.0}, session=sess)
print(predint[0])
# correct_prediction=tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
# #
# accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
# #
# train_accuracy = accuracy.eval({x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})
#
# print(train_accuracy)
1: https://cv-tricks.com/tensorflow-tutorial/save-restore-tensorflow-models-quick-complete-tutorial/
2: 黄文坚《TensorFlow 实战》
3:https://blog.csdn.net/qq_38269418/article/details/78991649