今天开始试着编写程序,但是因为时间问题还是没有一下写完。而且在写代码时由于无法安装书中所指示的库,只有自己编写拥有相似功能的代码。不过在编写过程中遇到了一个之前就遇到过一次的问题。
a = a.append(b)
AttributeError: 'NoneType' object has no attribute 'append'
由于工程比较正式,这次用的是jupyter notebook,错误是因为append函数没有返回值。修改为a.append(b)后无异常。
记录下今天的工作:
import pickle
import random
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import OneHotEncoder
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0)
%load_ext autoreload
%autoreload 2
#定义函数
def unpickle(file):
with open(file, 'rb')as fo:
dict = pickle.load(fo, encoding='ISO-8859-1')
return dict
#解压数据
labels = []
X_train = []
one_hot = OneHotEncoder()
for i in range(1,6): #解压训练集
data = unpickle('./CIAFR-10/cifar-10-batches-py/data_batch_%d'%(i))
labels.append(data['labels'])
X_train.append(data['data'])
#训练集变换为ndarray格式
X_train = np.array(X_train)
X_train = np.transpose(X_train.reshape(-1, 3, 32, 32), [0, 2, 3, 1])
y_train = np.array(labels).reshape(-1)
#测试集变换为ndarray格式
test = unpickle('./CIAFR-10/cifar-10-batches-py/test_batch')
X_test = test['data']
X_test = np.transpose(X_test.reshape(-1, 3, 32, 32), [0, 2, 3, 1])
#标签变为onehot
y_train = one_hot.fit_transform(y_train.reshape(-1, 1)).toarray()
y_test = one_hot.transform(np.array(test['labels']).reshape(-1, 1)).toarray()
#数据划分
X_val = [] #样本验证数据
X_sample = [] #样本训练数据
for i in range(100): #样本验证数据获取随机100个
random_num = random.randint(0,50000-1)
X_val.append(X_train[random_num])
for i in range(250): #样本验证数据获取随机100个
random_num = random.randint(0,50000-1)
X_sample.append(X_train[random_num])
X_val = np.array(X_val)
X_sample = np.array(X_sample)
#数据形状转换
X_train = np.reshape(X_train,(X_train.shape[0],-1))
X_val = np.reshape(X_val,(X_val.shape[0],-1))
X_test = np.reshape(X_test,(X_test.shape[0],-1))
X_sample = np.reshape(X_sample,(X_sample.shape[0],-1))
经过测试,基本能实现目的。
其中解压数据参考了八、(机器学习)-Tensorflow-CNN实现Cifar-10图像识别__23__的博客-CSDN博客
以及《深度学习实战》。