TensorFlow入门学习,Mnist数据集手写识别

mnist数据集下载比较麻烦,用这个脚本方便一点,当然也可以自己提前下载好需要的数据集

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import gzip
import os
import tempfile

import numpy
from six.moves import urllib
from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets

以下是是普通的网络实现

#coding=utf-8
import tensorflow as tf
import numpy as np
import input_data

mnist = input_data.read_data_sets('data/',one_hot = True)
#获取训练集的维度,里面包含总数和类别
trainingimgs = mnist.train.images
traininglabels = mnist.train.labels
testimgs = mnist.test.images
testlabels = mnist.test.labels
#不清楚内容是什么的时候,打印出来,方便查看
train_num = trainingimgs.shape[0]
# print(mnist.train._num_examples)
test_num = testimgs.shape[0]
labels = trainingimgs.shape[1]

#input
input   = tf.placeholder(tf.float32,[None,784])
output  = tf.placeholder(tf.float32,[None,10])

#w,b
w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

out = tf.matmul(input,w) + b
out_act = tf.nn.softmax(out)
loss = tf.reduce_mean(-tf.reduce_sum(output * tf.log(out_act),reduction_indices=1))

pre = tf.equal(tf.argmax(output,1),tf.argmax(out_act,1))
accuray = tf.reduce_mean(tf.cast(pre,tf.float32))

optimizer = tf.train.AdamOptimizer(1e-3).minimize(loss)

init = tf.global_variables_initializer()

epochs = 50
batch_size = 64
display_step = 5
with tf.Session() as sess:
    sess.run(init)
    batch_num = int(train_num/batch_size)
    for epoch in range(epochs) :
        avg_cost = 0
        for _ in range(batch_num):
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            feeds = {input:batch_xs,output:batch_ys}
            sess.run(optimizer,feed_dict = feeds)
            avg_cost += sess.run(loss,feed_dict = feeds)
        avg_cost /= batch_num    
        if epoch % display_step == 0:
#             for _ in range(20):
#                 test_batch_x,test_batch_y = mnist.test.next_batch(batch_size)
#                 feeds_test = {input:test_batch_x,output:test_batch_y}
#                 test_acc += sess.run(accuray,feed_dict = feeds_test)/20
            feeds_test = {input:mnist.test.images,output:mnist.test.labels}
            test_acc = sess.run(accuray,feed_dict = feeds_test)
            print('Epch:{}/{},loss:{},test_acc:{}%'.format(epoch,epochs,avg_cost, test_acc*100))
    print('Done')     

使用神经网络NN进行网络实现

#coding=utf-8
import tensorflow as tf
import input_data

mnist = input_data.read_data_sets('data/', one_hot = True)
trainimages = mnist.train.images
trainlabels = mnist.train.labels
testimages  = mnist.test.images
testlabels  = mnist.test.labels

train_num = trainimages.shape[0]
input_num = trainimages.shape[1]
# print(input_num)
test_num  = testimages.shape[0]
nclass    = testlabels.shape[1]
print(train_num,input_num,test_num,nclass)

layer1_n   = 256
layer2_n   = 128

x = tf.placeholder(tf.float32, [None, input_num])
y = tf.placeholder(tf.float32, [None, nclass])

weight = {
            'w1':tf.Variable(tf.random_normal([input_num, layer1_n],stddev=0.1)),
            'w2':tf.Variable(tf.random_normal([layer1_n, layer2_n],stddev=0.1)),
            'out':tf.Variable(tf.random_normal([layer2_n, nclass],stddev=0.1))
    }

biase = {
            'b1':tf.Variable(tf.random_normal([layer1_n])),    
            'b2':tf.Variable(tf.random_normal([layer2_n])),
            'out':tf.Variable(tf.random_normal([nclass]))
    }

def compute_layers(_x,_w,_b):
    layer1 = tf.nn.sigmoid(tf.add(tf.matmul(_x,_w['w1']),_b['b1']))
    layer2 = tf.nn.sigmoid(tf.add(tf.matmul(layer1,_w['w2']),_b['b2']))
    out = tf.add(tf.matmul(layer2, _w['out']), _b['out'])
    return out

pred = compute_layers(x, weight, biase)
# print()
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = pred,labels = y))
optimize = tf.train.GradientDescentOptimizer(1e-3).minimize(loss)
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(pred,1),tf.argmax(y,1)),tf.float32))

init = tf.global_variables_initializer()

epochs = 20
batch_size = 32
display_step = 5

with tf.Session() as sess:
    sess.run(init)
    for epo in range(epochs):
        avg_cost = 0
        batch_num = int(train_num/batch_size)
        for _ in range(batch_num):
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            feed = {x:batch_xs,y:batch_ys}
            sess.run(optimize,feed_dict = feed)
            avg_cost += sess.run(loss,feed_dict = feed)
        avg_cost /= batch_num
        if epo % display_step == 0:
            feeds_test = {x:mnist.test.images,y:mnist.test.labels}
            test_acc = sess.run(accuracy,feed_dict = feeds_test)
            print('Epch:{}/{},loss:{},test_acc:{}'.format(epo,epochs,avg_cost, test_acc))
    print('Done')  

使用CNN的网络实现

#coding=utf-8
import tensorflow as tf
import input_data

mnist = input_data.read_data_sets('data/', one_hot = True)
trainimages = mnist.train.images
trainlabels = mnist.train.labels
testimages  = mnist.test.images
testlabels  = mnist.test.labels

train_num = trainimages.shape[0]
input_num = trainimages.shape[1]
# print(input_num)
test_num  = testimages.shape[0]
nclass    = testlabels.shape[1]
print(train_num,input_num,test_num,nclass)

x = tf.placeholder(tf.float32, [None, input_num])
y = tf.placeholder(tf.float32, [None, nclass])
keepratio = tf.placeholder(tf.float32)

#w,b,高斯初始化
weigths = {
            'conv1':tf.Variable(tf.random_normal([3,3,1,64],stddev = 0.1)),
            'conv2':tf.Variable(tf.random_normal([3,3,64,128],stddev = 0.1)), 
            'fc1':tf.Variable(tf.random_normal([7*7*128,1024],stddev = 0.1)),
            'fc2':tf.Variable(tf.random_normal([1024,10],stddev = 0.1))  
    }

biases = {
            'conv1_b':tf.Variable(tf.random_normal([64],stddev = 0.1)),
            'conv2_b':tf.Variable(tf.random_normal([128],stddev = 0.1)),
            'fc1_b':tf.Variable(tf.random_normal([1024],stddev = 0.1)),
            'fc2_b':tf.Variable(tf.random_normal([nclass],stddev = 0.1)),
    }
#定义网络的结构
def _con_pool(_x, _w, _b, _keepratio):
    _input = tf.reshape(_x,[-1,28,28,1])#batch_size,height,wight,chanel
    conv1 = tf.nn.conv2d(_input, _w['conv1'], [1,1,1,1], padding = 'SAME')
    conv1 = tf.nn.relu(tf.nn.bias_add(conv1, _b['conv1_b']))
    pool1 = tf.nn.max_pool(conv1, [1,2,2,1],  [1,2,2,1], padding = 'SAME')
    pool1_dr1 = tf.nn.dropout(pool1,_keepratio)
    
    conv2 = tf.nn.conv2d(pool1_dr1, _w['conv2'], [1,1,1,1], padding = 'SAME')
    conv2 = tf.nn.relu(tf.nn.bias_add(conv2, _b['conv2_b']))
    pool2 = tf.nn.max_pool(conv2, [1,2,2,1],  [1,2,2,1], padding = 'SAME')
    pool2_dr2 = tf.nn.dropout(pool2,_keepratio)
    
    dense = tf.reshape(pool2_dr2, [-1, _w['fc1'].get_shape().as_list()[0]])  
    dense = tf.nn.relu(tf.add(tf.matmul(dense,_w['fc1']),_b['fc1_b']))
    dense_dr3 = tf.nn.dropout(dense, _keepratio)
    
    out = tf.add(tf.matmul(dense_dr3, _w['fc2']), _b['fc2_b'])
    return out

pred = _con_pool(x, weigths, biases, keepratio)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y, logits = pred))
optimizer = tf.train.AdamOptimizer(1e-3).minimize(loss)
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(pred,1),tf.argmax(y,1)),tf.float32))

init = tf.global_variables_initializer()

savestep = 1
saver = tf.train.Saver(max_to_keep = 3)
save_path = 'save/nets/mnist_cnn_basic.ckpt-'

do_train = 1
train_epochs = 20
batch_size = 64
display_step = 4

sess = tf.Session()
sess.run(init)

if do_train == 1:
    for epoch in range(train_epochs):
        avg_cost = 0
        test_acc = 0
        total_batch = int(train_num/batch_size)
        total_test_batch = int(test_num/batch_size + 1)
        for _ in range(total_batch):
            batch_t_x,batch_t_y = mnist.train.next_batch(batch_size)
            feed_t = {x: batch_t_x, y: batch_t_y, keepratio: 0.7}
            sess.run(optimizer, feed_dict = feed_t)
            feed_t = {x: batch_t_x, y: batch_t_y, keepratio: 1.}
            avg_cost += sess.run(loss, feed_dict = feed_t)
        avg_cost /= total_batch
        
        if epoch % display_step == 0:
            #由于gpu显存不够,分批处理,在计算准确率
            for _ in range(total_test_batch):
                batch_test_x,batch_test_y = mnist.test.next_batch(batch_size)
                feed_t = {x: batch_t_x, y: batch_t_y, keepratio: 1.}
                test_acc += sess.run(accuracy, feed_dict = feed_t)
            test_acc /= total_test_batch
            print('Epoch:{}/{},loss:{},acc:{}'.format(epoch, train_epochs, avg_cost, test_acc))
            
        if epoch % savestep == 0:
            saver.save(sess,save_path + str(epoch))
    
    print('Done')
if do_train == 0:
    epoch = train_epochs - 1
    saver.restore(sess, save_path + str(epoch))
    test_acc = sess.run(accuracy, feed_dict = {x:mnist.test.images, y:mnist.test.lables,keepratio:1.})
    print(test_acc)

sess.close()

使用RNN实现网络

#coding=utf-8
import tensorflow as tf
import input_data

mnist = input_data.read_data_sets('data/', one_hot = True)
trainimages = mnist.train.images
trainlabels = mnist.train.labels
testimages  = mnist.test.images
testlabels  = mnist.test.labels

train_num = trainimages.shape[0]
input_num = trainimages.shape[1]
# print(input_num)
test_num  = testimages.shape[0]
nclass    = testlabels.shape[1]
print(train_num,input_num,test_num,nclass)

dim_input = 28
nsteps = 28
hiddenlayer = 128

x = tf.placeholder(tf.float32, [None, nsteps, dim_input])
y = tf.placeholder(tf.float32, [None, nclass])
#rnn中需要一个两倍长hiddenlayer
# istate = tf.placeholder("float", [None, 2 * hiddenlayer])
learingrate = 1e-3

weights = {
        'wh':tf.Variable(tf.random_normal([dim_input, hiddenlayer])),
        'wo':tf.Variable(tf.random_normal([hiddenlayer, nclass]))
    }

biases = {
        'wh':tf.Variable(tf.random_normal([hiddenlayer])),
        'wo':tf.Variable(tf.random_normal([nclass]))
    }

def _rnn(_x, _w, _b, _nsteps, _name):
    _x = tf.transpose(_x, [1,0,2])
    _x = tf.reshape(_x, [-1, dim_input])
    _h = tf.add(tf.matmul(_x, _w['wh']), _b['wh'])
    
    #split pices
    _hsplit = tf.split(_h, _nsteps, 0)
    print(_hsplit[0].shape)
#此处不能使用variable_scope,使用就会出错,还不知原因
#     with tf.variable_scope(_name) as scope:
#         scope.reuse_variables()
    """
        __init__(
            num_units,
            forget_bias=1.0,
            state_is_tuple=True,
            activation=None,
            reuse=None,
            name=None
        )
        #, forget_bias = 1.0, state_is_tuple=True)
    """
    lstm_cell = tf.nn.rnn_cell.BasicRNNCell(hiddenlayer)
    _lstm_o, _lstm_s = tf.nn.static_rnn(lstm_cell, _hsplit, dtype = tf.float32)
           
    out = tf.add(tf.matmul(_lstm_o[-1],_w['wo']),_b['wo'])
    return out

pred = _rnn(x, weights, biases, nsteps, 'basic')
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y, logits = pred))
optimizer = tf.train.AdamOptimizer(learingrate).minimize(loss)
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(pred,1),tf.argmax(y,1)),tf.float32))

init = tf.global_variables_initializer()

train_epochs = 40
batch_size = 32
display_step = 4

sess = tf.Session()
sess.run(init)

for epoch in range(train_epochs):
    avg_cost = 0
    test_acc = 0
    total_batch = int(train_num/batch_size)
    total_test_batch = int(test_num/batch_size + 1)
    for _ in range(total_batch):
        batch_t_x,batch_t_y = mnist.train.next_batch(batch_size)
        batch_t_x = batch_t_x.reshape([batch_size, nsteps,dim_input])
        feed_t = {x: batch_t_x, y: batch_t_y}
        sess.run(optimizer, feed_dict = feed_t)
        feed_t = {x: batch_t_x, y: batch_t_y}
        avg_cost += sess.run(loss, feed_dict = feed_t)
    avg_cost /= total_batch
         
    if epoch % display_step == 0:
        for _ in range(total_test_batch):
            batch_test_x,batch_test_y = mnist.test.next_batch(batch_size)
            batch_test_x = tf.reshape(batch_test_x,[batch_size, nsteps,dim_input])
            feed_t = {x: batch_t_x, y: batch_t_y}
            test_acc += sess.run(accuracy, feed_dict = feed_t)
        test_acc /= total_test_batch
        print('Epoch:{}/{},loss:{},acc:{}'.format(epoch, train_epochs, avg_cost, test_acc))      
print('Done')
    
sess.close()  
至此关于手写识别的几个已经搞完

你可能感兴趣的:(TF)