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')#池化步长为1

stride=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')#池化步长为1

stride = 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 size
    images:images_batchsize,4D tensor - [batch_size, width, height, channel]
    label_batch: 1D tensor - [batch_size]
    '''
    i = 0
    for 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+=1

    figure_title = name
    ax3  = 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 layer
        plt.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值