在之前二分类的基础上进行十分类
数据集下载
import tensorflow as tf
import numpy as np
import os
import pickle
# 路径
cifar_dir = '../../datas/cifar-10-batches-py'
# 查看目录
print(os.listdir(cifar_dir))
# 获取特征,标签
def load_data(filname):
with open(filname,'rb') as f:
data = pickle.load(f,encoding='bytes')
return data[b'data'],data[b'labels']
# 封装
class Cifar_Data:
# 构造方法
def __init__(self,filenames,need_shuffle):
all_data = []
all_labels = []
# 循环读取文件
for filename in filenames:
data , labels = load_data(filename)
all_data.append(data)
all_labels.append(labels)
self.data = np.vstack(all_data)
self.data = self.data /127.5 -1 #让标签在 -1~1之间
self.labels = np.hstack(all_labels)
print(self.data.shape)
print(self.labels.shape)
# 次数
self.num_examples = self.data.shape[0]
self.need_shuffle = need_shuffle
self.indicator = 0
# 洗牌
if self.need_shuffle:
self.shuffle_data()
# 随机洗牌
def shuffle_data(self):
p = np.random.permutation(self.num_examples)
self.data = self.data[p]
self.labels = self.labels[p]
# 批次
def next_batch(self,batch_size):
end_indicator = self.indicator + batch_size
if end_indicator >self.num_examples:
if self.need_shuffle:
self.shuffle_data()
self.indicator = 0
end_indicator = batch_size
else:
raise Exception('没有更多例子')
if end_indicator > self.num_examples:
raise Exception('批次大于所有例子')
batch_data = self.data[self.indicator:end_indicator]
batch_labels = self.labels[self.indicator:end_indicator]
self.indicator = end_indicator
return batch_data,batch_labels
# 获取文件
train_filenames = [os.path.join(cifar_dir,'data_batch_%d'%i)for i in range(1,6)]
test_filenames = [os.path.join(cifar_dir,'test_batch')]
# 实例化对象
train_batch = Cifar_Data(train_filenames,True)
test_batch = Cifar_Data(test_filenames,False)
# 站位
x = tf.placeholder(tf.float32,[None,3072])
y = tf.placeholder(tf.int64,[None])
# 使用方法来取代隐藏层
a1 = tf.layers.dense(x,400,activation=tf.nn.relu)
a2 = tf.layers.dense(a1,128,activation=tf.nn.relu)
a3 = tf.layers.dense(a2,80,activation=tf.nn.relu)
h = tf.layers.dense(a3,10)
# 代价
cost = tf.losses.sparse_softmax_cross_entropy(labels=y,logits=h)
# 优化器
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
# 准确率
pre = tf.argmax(h,1)
accuracy = tf.reduce_mean(tf.cast(tf.equal(pre,y),tf.float64))
# 开启会话
sess = tf.Session()
sess.run(tf.global_variables_initializer())
batch_size = 20
# 开始训练
for i in range(1,10001):
batch_x,batch_y = train_batch.next_batch(batch_size)
c,a,_ = sess.run([cost,accuracy,optimizer],feed_dict={x:batch_x,y:batch_y})
if i % 500 == 0:
print(i,c,a)
if i % 5000 == 0:
# 测试
test_batch = Cifar_Data(test_filenames, False)
all_acc = []
for k in range(1,101):
batch_x1, batch_y1 = test_batch.next_batch(batch_size)
a = sess.run(accuracy, feed_dict={x: batch_x1, y: batch_y1})
all_acc.append(a)
print('*'*52,np.mean(all_acc))
效果
500 1.5045621 0.55
1000 1.530426 0.65
1500 1.5572646 0.5
2000 1.318046 0.55
2500 1.7129911 0.45
3000 1.5259087 0.5
3500 1.3905205 0.55
4000 1.7939479 0.45
4500 1.1358879 0.6
5000 1.3265371 0.45
(10000, 3072)
(10000,)
**************************************************** 0.49599999999999994
5500 1.6910607 0.35
6000 1.5214736 0.5
6500 1.6452535 0.3
7000 1.2214502 0.55
7500 1.6512038 0.5
8000 1.6135814 0.55
8500 1.1990843 0.55
9000 1.6836264 0.45
9500 1.7168096 0.45
10000 1.2701477 0.55
(10000, 3072)
(10000,)
**************************************************** 0.49699999999999994