本文主要是介绍tensorflow之MNIST手写字符集训练可视化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
简介
很多人认为卷积神经是一个黑箱子,把图片输入,输出结果为有监督式的学习(supervised learning),贴标签的形式,即可达到分类的效果。那么计算机到底做了什么事情呢?训练过程结果如何可视化?下面进行简单的介绍。
模型的搭建
@author XT
#第1层convolutional
W1 = tf.Variable(tf.truncated_normal([5,5,1,K],stddev=0.1),dtype=tf.float32,name='W1') #[filterheight,filterwith,input_channel,output_channel]
b1 = tf.Variable(tf.ones([K])/10,dtype=tf.float32,name='b1')
#第2层convolutional
W2 = tf.Variable(tf.truncated_normal([4,4,K,L],stddev=0.1),dtype=tf.float32,name='W2')
b2 = tf.Variable(tf.ones([L])/10,dtype=tf.float32,name='b2')
#第3层convolutional
W3 = tf.Variable(tf.truncated_normal([4,4,L,M],stddev=0.1),dtype=tf.float32,name='W3')
b3 = tf.Variable(tf.ones([M])/10,dtype=tf.float32,name='b3')
#convolutional out fully connected layer
W4 = tf.Variable(tf.truncated_normal([7*7*M,N],stddev=0.1),dtype=tf.float32,name='W4')
b4 = tf.Variable(tf.ones([N])/10,dtype=tf.float32,name='b4')
#output
W5 = tf.Variable(tf.truncated_normal([N,n_class],stddev=0.1),dtype=tf.float32,name='W5')#要随最后层修改
b5 = tf.Variable(tf.ones([n_class]),dtype=tf.float32,name='b5')
这里搭建了较简单的卷积神经网络,使用了3层卷积的权值,后加全连接层,最后是输出。结构为:
代码
@author XT
#Model
pkeep = tf.placeholder(tf.float32)
x = tf.placeholder(tf.float32, [None,784])#!!!注意图片格式大小
x_image = tf.reshape(x,[-1,28,28,1])stride=1 # output is 28x28
Y1 = tf.nn.relu(tf.nn.conv2d(x_image,W1,strides=[1,stride,stride,1],padding='SAME')+b1)#sigmoid很差,要用relu
YF1 = tf.nn.dropout(Y1,pkeep)
h_poolYF1 = tf.nn.max_pool(YF1,ksize=[1,2,2,1],strides=[1,1,1,1],padding='SAME')#池化步长为1stride=2 # output is 14x14
Y2 = tf.nn.relu(tf.nn.conv2d(h_poolYF1,W2,strides=[1,stride,stride,1],padding='SAME')+b2)#sigmoid
YF2 = tf.nn.dropout(Y2,pkeep)
h_poolYF2 = tf.nn.max_pool(YF2,ksize=[1,2,2,1],strides=[1,1,1,1],padding='SAME')#池化步长为1stride = 2 # output is 7x7
Y3 = tf.nn.relu(tf.nn.conv2d(h_poolYF2,W3,strides=[1,stride,stride,1],padding='SAME')+b3)#sigmoid
YF3 = tf.nn.dropout(Y3,pkeep)
h_poolYF3 = tf.nn.max_pool(YF3,ksize=[1,2,2,1],strides=[1,1,1,1],padding='SAME')#池化步长为1# reshape the output from the third convolution for the fully connected layer
YY = tf.reshape(h_poolYF3, shape=[-1, 7*7*M])Y4 = tf.nn.relu(tf.matmul(YY,W4)+b4)#sigmoid
YF4 = tf.nn.dropout(Y4,pkeep)Ylogits = tf.matmul(YF4, W5)+b5
Y = tf.nn.softmax(Ylogits)#softmax
训练结果
1、总测试
2、Test One
输出概率:
第一卷积层:
部分代码
def plot_images(x, labels,max_index,name):'''plot one batch sizeimages:images_batchsize,4D tensor - [batch_size, width, height, channel]label_batch: 1D tensor - [batch_size]'''i = 0for one_pic_vic in x:one_pic_arr = np.reshape(one_pic_vic,(28,28))plt.subplot(1,1,i+1)plt.axis('off')plt.title('Label: %d Forecast: %d'%(labels[i],max_index[i]), fontsize = 14)#采用A=0标签 +' Forecast: '+max_index[i]plt.subplots_adjust(top=0.9)plt.imshow(one_pic_arr,cmap='gray')i+=1figure_title = nameax3 = plt.subplot(1,1,1)plt.text(0.5, -0.05, figure_title,horizontalalignment='center',fontsize=20,transform = ax3.transAxes)pylab.show()def show_rich_feature(x_relu,Node):print(x_relu.shape[1],"X",x_relu.shape[2])feature_map = tf.reshape(x_relu, [x_relu.shape[1],x_relu.shape[2],Node])images = tf.image.convert_image_dtype (feature_map, dtype=tf.uint8)images = sess.run(images)plt.figure(figsize=(10, 10))#if Node > 25,plot(5,5)for i in np.arange(0, Node):plt.subplot(2, 2, i + 1)#you need to change the subplot size if you use other layerplt.axis('off')plt.imshow(images[:,:,i])plt.show()
参考
【1】Tensorflow教程-VGG论文导读+Tensorflow实现+参数微调(fine-tuning)
http://v.youku.com/v_show/id_XMjcyNzYwMjkxMg==.html?spm=a2hzp.8244740.0.0
【2】谷歌云大会教程:没有博士学位如何玩转TensorFlow和深度学习(附资源)
http://www.sohu.com/a/128686069_465975
这篇关于tensorflow之MNIST手写字符集训练可视化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!