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
开启训练
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:%d。mini_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:1。mini_batch loss:3.1455。训练数据上的准确率:0.0938。测试数据上准确率:0.2853
iter count:2。mini_batch loss:3.9139。训练数据上的准确率:0.2891。测试数据上准确率:0.2853
iter count:3。mini_batch loss:5.1961。训练数据上的准确率:0.1562。测试数据上准确率:0.2853
iter count:4。mini_batch loss:3.9102。训练数据上的准确率:0.2344。测试数据上准确率:0.2853
iter count:5。mini_batch loss:4.1278。训练数据上的准确率:0.1719。测试数据上准确率:0.2853
.....
iter count:97。mini_batch loss:1.5752。训练数据上的准确率:0.4844。测试数据上准确率:0.2853
iter count:98。mini_batch loss:1.8480。训练数据上的准确率:0.3906。测试数据上准确率:0.2853
iter count:99。mini_batch loss:1.5662。训练数据上的准确率:0.5391。测试数据上准确率:0.2853
iter count:100。mini_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:%d。mini_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:101。mini_batch loss:1.4157。训练数据上的准确率:0.5234。测试数据上准确率:0.4711
iter count:102。mini_batch loss:1.6045。训练数据上的准确率:0.4375。测试数据上准确率:0.4711
....
iter count:748。mini_batch loss:0.6842。训练数据上的准确率:0.7734。测试数据上准确率:0.4711
iter count:749。mini_batch loss:0.6560。训练数据上的准确率:0.8203。测试数据上准确率:0.4711
iter count:750。mini_batch loss:0.7151。训练数据上的准确率:0.7578。测试数据上准确率:0.4711
iter count:751。mini_batch loss:0.8092。训练数据上的准确率:0.7344。测试数据上准确率:0.4711
iter count:752。mini_batch loss:0.7394。训练数据上的准确率:0.7422。测试数据上准确率:0.4711
iter count:753。mini_batch loss:0.8732。训练数据上的准确率:0.7188。测试数据上准确率:0.4711
iter count:754。mini_batch loss:0.8762。训练数据上的准确率:0.6953。测试数据上准确率:0.4711
准确率高达80%,博主亲测,以上准确率数据部分展示,大家可以多训练几次。哈哈哈~~~