Tensorflow学习笔记2-MNIST数字识别

很早之前写的,鉴于之前Tensorflow学习笔记有1,不能这么轻易弃坑,所以又加了这一部分。

import tensorflow as tf
import read_data
import time
import numpy

mnist = read_data.DataSets(dlimit = 300)
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32,shape=[None,784])
yy = tf.placeholder(tf.float32,shape=[None,10])
w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
sess.run(tf.global_variables_initializer())


y = tf.nn.softmax(tf.matmul(x,w)+b)
cross_entropy = -tf.reduce_sum(yy*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.0001).minimize(cross_entropy)
for i in range(0,600):
    print("Iteration %d"%i)
    batch_xs,batch_ys = mnist.train.next_batch(50)
    train_step.run(feed_dict={x:batch_xs,yy:batch_ys})

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(yy,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(accuracy.eval(feed_dict={x: mnist.test.images, yy:mnist.test.labels}))

这是最简单的线性回归算法,理解了这种算法,其实就可以搞懂Tensorflow的运行机制了,大概就是把梯度下降的图提前建出来,然后在对于训练数据一次次在图上迭代。

接下来是我自己写的CNN代码,自以为是的用了多个不同的卷积核,具体有没有用?Who Knows?毕竟玄学

import tensorflow as tf
import read_data
import numpy
import random

def col_calc(x):
    x_level1 = tf.reshape(x,[-1,28,28,1])
    w_level1 = tf.Variable(tf.truncated_normal([3,16,1,64],stddev=.1))
    b_level1 = tf.Variable(tf.constant(.1,shape=[64]))
    r_level1 = tf.nn.relu(tf.nn.conv2d(x_level1,w_level1,strides=[1,1,4,1],padding='VALID') + b_level1)

    x_level2 = tf.reshape(r_level1,[-1,26*4*64])
    w_level2 = tf.Variable(tf.truncated_normal([26*4*64,2048],stddev=.1))
    b_level2 = tf.Variable(tf.constant(.1,shape=[2048]))
    r_level2 = tf.nn.relu(tf.matmul(x_level2,w_level2)+b_level2)
    return r_level2

def row_calc(x):
    x_level1 = tf.reshape(x,[-1,28,28,1])
    w_level1 = tf.Variable(tf.truncated_normal([16,3,1,32],stddev=.1))
    b_level1 = tf.Variable(tf.constant(.1,shape=[32]))
    r_level1 = tf.nn.relu(tf.nn.conv2d(x_level1,w_level1,strides=[1,4,1,1],padding='VALID') + b_level1)

    x_level2 = tf.reshape(r_level1,[-1,26*4*32])
    w_level2 = tf.Variable(tf.truncated_normal([26*4*32,1024],stddev=.1))
    b_level2 = tf.Variable(tf.constant(.1,shape=[1024]))
    r_level2 = tf.nn.relu(tf.matmul(x_level2,w_level2)+b_level2)
    return r_level2

def block_calc(x):
    '''
    第一层:使用5*5卷积核做卷积,特征数取为16个
    '''
    x_level1 = tf.reshape(x,[-1,28,28,1])
    w_level1 = tf.Variable(tf.truncated_normal([5,5,1,32],stddev=.1))
    b_level1 = tf.Variable(tf.constant(.1,shape=[32]))
#28*28*1
    r_level1 = tf.nn.relu(tf.nn.conv2d(x_level1,w_level1,strides=[1,1,1,1],padding='VALID') + b_level1)
#24*24*16
    h_level1 = tf.nn.max_pool(r_level1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
#12*12*16

    '''
    第二层:使用5*5卷积核做卷积
    '''
    x_level2 = h_level1
    w_level2 = tf.Variable(tf.truncated_normal([5,5,32,64],stddev=.1));
    b_level2 = tf.Variable(tf.constant(.1,shape=[64]));
    r_level2 = tf.nn.relu(tf.nn.conv2d(x_level2,w_level2,strides=[1,1,1,1],padding='SAME') + b_level2)
    h_level2 = tf.nn.max_pool(r_level2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

#12*12*256
    x_level3 = tf.reshape(h_level2,[-1,6*6*64])
    w_level3 = tf.Variable(tf.truncated_normal([6*6*64,1024],stddev=.1))
    b_level3 = tf.Variable(tf.constant(.1,shape=[1024]))
    r_level3 = tf.nn.relu(tf.matmul(x_level3,w_level3)+b_level3)

    return r_level3

if __name__ == '__main__':
#预处理数组
    mnist = read_data.DataSets(dlimit=-1)

#初始化session
    sess = tf.InteractiveSession()
    x = tf.placeholder(tf.float32,shape=[None,784])
    yy = tf.placeholder(tf.float32,shape=[None,10])
    kp = tf.placeholder(tf.float32)

    r_block = block_calc(x)
    r_row = row_calc(x)
    r_col = col_calc(x)

    r_all = tf.concat(axis=1,values=[r_block,r_row,r_col])

    x_level4 = tf.nn.dropout(r_all,kp)
    w_level4 = tf.Variable(tf.truncated_normal([4096,10],stddev=.1))
    b_level4 = tf.Variable(tf.constant(.1,shape=[10]))
    r_level4 = tf.matmul(x_level4,w_level4)+b_level4

    y = tf.nn.softmax(r_level4)
    cross_entropy = tf.reduce_sum(-tf.log(y) * yy)
    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(yy,1)) # 计算准确度
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    train_step = tf.train.AdamOptimizer(5*1e-5).minimize(cross_entropy)
    sess.run(tf.global_variables_initializer())
    lastans = -1e10
    for i in range(0,1000000):
        if (i%50 == 0):
            print("Iteration #%d..."%i)
        batch_xs,batch_ys = mnist.train.next_batch(60)
        train_step.run(feed_dict={x:batch_xs,yy:batch_ys,kp:0.5})
        if i%200 == 20:
            train_accuracy = accuracy.eval(feed_dict={x:batch_xs, yy: batch_ys,kp: 1.0})
            print("step %d, current training accuracy %g"%(i, train_accuracy))
        if i%1000 == 20:
            train_accuracy = accuracy.eval(feed_dict={x:mnist.test.images, yy:mnist.test.labels,kp: 1.0})
            print("step %d, training accuracy %g"%(i, train_accuracy))
            if train_accuracy>=lastans:
                print("update result...")
                lastans = train_accuracy
                correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(yy,1))
                final_prediction = tf.argmax(y,1);
                result = final_prediction.eval(feed_dict={x:mnist.uploads.images,kp:1})
                print("saving result...")
                mnist.uploads.labels = result
                mnist.uploads.save('result.csv')
                mnist.uploads.save2('result2.csv')

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据