CNN-cr-1 0-優化


import numpy as np

 

# 序列化和反序列化

import pickle

 

from sklearn.preprocessing import OneHotEncoder

 

import warnings

warnings.filterwarnings('ignore')

 

import tensorflow as tf

數據加載(使用pickle

def unpickle(file):

    import pickle

    with open(file, 'rb') as fo:

        dict = pickle.load(fo, encoding='ISO-8859-1')

    return dict

labels = []

X_train = []

for i in range(1,6):

    data = unpickle('./cifar-10-batches-py/data_batch_%d'%(i))

    labels.append(data['labels'])

    X_train.append(data['data'])

# list類型轉換爲ndarray

X_train = np.array(X_train)

 

y_train = np.array(labels).reshape(-1)

 

# reshape

X_train = X_train.reshape(-1,3072)

 

# 目標值概率

one_hot = OneHotEncoder()

y_train =one_hot.fit_transform(y_train.reshape(-1,1)).toarray()

 

# 測試數據加載

test = unpickle('./cifar-10-batches-py/test_batch')

X_test = test['data']

y_test = one_hot.transform(np.array(test['labels']).reshape(-1,1)).toarray()

# 從總數據中獲取一批數據

index = 0

def next_batch(X,y):

    global index

    batch_X = X[index*128:(index+1)*128]

    batch_y = y[index*128:(index+1)*128]

    index+=1

    if index == 390:

        index = 0

    return batch_X,batch_y

構建神經網絡

1.生成對應卷積核

2.tf.nn.conv2d進行卷積運算

3.歸一化操作 tf.layers.batch_normalization

4.激活函數(relu

5.池化操作

X = tf.placeholder(dtype=tf.float32,shape = [None,3072])

y = tf.placeholder(dtype=tf.float32,shape = [None,10])

kp = tf.placeholder(dtype=tf.float32)

 

def gen_v(shape,std = 5e-2):

    return tf.Variable(tf.truncated_normal(shape = shape,stddev=std))

 

def conv(input_,filter_,b):

    conv = tf.nn.conv2d(input_,filter_,strides=[1,1,1,1],padding='SAME') + b

    conv = tf.layers.batch_normalization(conv,training=True)

    conv = tf.nn.relu(conv)

    return tf.nn.max_pool(conv,[1,3,3,1],[1,2,2,1],'SAME')

 

def net_work(X,kp):

#     形狀改變,4

    input_ = tf.reshape(X,shape = [-1,32,32,3])

#     第一層

    filter1 = gen_v(shape = [3,3,3,64])

    b1 = gen_v(shape = [64])

    pool1 = conv(input_,filter1,b1)

    

#     第二層

    filter2 = gen_v([3,3,64,128])

    b2 = gen_v(shape = [128])

    pool2 = conv(pool1,filter2,b2)

    

#     第三層

    filter3 = gen_v([3,3,128,256])

    b3 = gen_v([256])

    pool3 = conv(pool2,filter3,b3)

    

#     第一層全連接層

    dense = tf.reshape(pool3,shape = [-1,4*4*256])

    fc1_w = gen_v(shape = [4*4*256,1024])

    fc1_b = gen_v([1024])

    bn_fc_1 = tf.layers.batch_normalization(tf.matmul(dense,fc1_w) + fc1_b,training=True)

    function(){ //智匯返傭 http://www.kaifx.cn/broker/thinkmarkets.html

    relu_fc_1 = tf.nn.relu(bn_fc_1)

#     fc1.shape = [-1,1024]

    

    

#     dropout

    dp = tf.nn.dropout(relu_fc_1,keep_prob=kp)

    

#     fc2 輸出層

    out_w = gen_v(shape = [1024,10])

    out_b = gen_v(shape = [10])

    out = tf.matmul(dp,out_w) + out_b

    return out

損失函數準確率&最優化

out = net_work(X,kp)

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y,logits=out))

# 準確率

y_ = tf.nn.softmax(out)

# equal 相當於 ==

equal = tf.equal(tf.argmax(y,axis = -1),tf.argmax(y_,axis = 1))

accuracy = tf.reduce_mean(tf.cast(equal,tf.float32))

 

opt = tf.train.AdamOptimizer(0.01).minimize(loss)

opt

<tf.Operation 'Adam_1' type=NoOp>

開啓訓練

saver = tf.train.Saver()

epoches = 100

with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())

    for i in range(epoches):

        batch_X,batch_y = next_batch(X_train,y_train)

        opt_,loss_ ,score_train= sess.run([opt,loss,accuracy],feed_dict = {X:batch_X,y:batch_y,kp:0.5})

        print('iter count:%dmini_batch loss:%0.4f。訓練數據上的準確率:%0.4f。測試數據上準確率:%0.4f'%

              (i+1,loss_,score_train,score_test))

        if score_train > 0.6:

            saver.save(sess,'./model/estimator',i+1)

    saver.save(sess,'./model/estimator',i+1)

    score_test = sess.run(accuracy,feed_dict = {X:X_test,y:y_test,kp:1.0})

    print('測試數據上的準確率:',score_test)

iter count:1mini_batch loss:3.1455。訓練數據上的準確率:0.0938。測試數據上準確率:0.2853

iter count:2mini_batch loss:3.9139。訓練數據上的準確率:0.2891。測試數據上準確率:0.2853

iter count:3mini_batch loss:5.1961。訓練數據上的準確率:0.1562。測試數據上準確率:0.2853

iter count:4mini_batch loss:3.9102。訓練數據上的準確率:0.2344。測試數據上準確率:0.2853

iter count:5mini_batch loss:4.1278。訓練數據上的準確率:0.1719。測試數據上準確率:0.2853

 

.....

iter count:97mini_batch loss:1.5752。訓練數據上的準確率:0.4844。測試數據上準確率:0.2853

iter count:98mini_batch loss:1.8480。訓練數據上的準確率:0.3906。測試數據上準確率:0.2853

iter count:99mini_batch loss:1.5662。訓練數據上的準確率:0.5391。測試數據上準確率:0.2853

iter count:100mini_batch loss:1.7489。訓練數據上的準確率:0.4141。測試數據上準確率:0.2853

測試數據上的準確率: 0.4711

epoches = 1100

with tf.Session() as sess:

    saver.restore(sess,'./model/estimator-100')

    for i in range(100,epoches):

        batch_X,batch_y = next_batch(X_train,y_train)

        opt_,loss_ ,score_train= sess.run([opt,loss,accuracy],feed_dict = {X:batch_X,y:batch_y,kp:0.5})

        print('iter count:%dmini_batch loss:%0.4f。訓練數據上的準確率:%0.4f。測試數據上準確率:%0.4f'%

              (i+1,loss_,score_train,score_test))

        if score_train > 0.6:

            saver.save(sess,'./model/estimator',i+1)

    saver.save(sess,'./model/estimator',i+1)

    if (i%100 == 0) and (i != 100):

        score_test = sess.run(accuracy,feed_dict = {X:X_test,y:y_test,kp:1.0})

        print('----------------測試數據上的準確率:---------------',score_test)

iter count:101mini_batch loss:1.4157。訓練數據上的準確率:0.5234。測試數據上準確率:0.4711

iter count:102mini_batch loss:1.6045。訓練數據上的準確率:0.4375。測試數據上準確率:0.4711

....

iter count:748mini_batch loss:0.6842。訓練數據上的準確率:0.7734。測試數據上準確率:0.4711

iter count:749mini_batch loss:0.6560。訓練數據上的準確率:0.8203。測試數據上準確率:0.4711

iter count:750mini_batch loss:0.7151。訓練數據上的準確率:0.7578。測試數據上準確率:0.4711

iter count:751mini_batch loss:0.8092。訓練數據上的準確率:0.7344。測試數據上準確率:0.4711

iter count:752mini_batch loss:0.7394。訓練數據上的準確率:0.7422。測試數據上準確率:0.4711

iter count:753mini_batch loss:0.8732。訓練數據上的準確率:0.7188。測試數據上準確率:0.4711

iter count:754mini_batch loss:0.8762。訓練數據上的準確率:0.6953。測試數據上準確率:0.4711

準確率高達80%,博主親測,以上準確率數據部分展示,大家可以多訓練幾次。哈哈哈~~~


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章