全连接网络的局限性
- 合理的结构减少参数
卷积神经网络
CNN是一个多层的神经网络,每层由多个二维平面组成, 其中每个平面由多个独立神经元组成。
- CNN结构与全连接神经网络的异同:
- 比全连接神经网络多了 前面的卷积层跟降采样层次
卷积
注:红色权值矩阵即为卷积核
卷积动态图演示:https://blog.csdn.net/LN_IOS/article/details/80538570
池化(降采样)
池化操作跟卷积核基本一致(不再是求点积 而是求最大值或者中位数)
Tensorflow中CNN的相关函数
CIFAR-10图像识别
- 显示数据
完整代码
#数据加载
%matplotlib inline
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
def load_batch(name):
with open(name,'rb') as f:
data_dict = np.load(f,encoding='bytes')
images = data_dict[b'data']
labels = data_dict[b'labels']
images = images.reshape(10000,3,32,32)
images = images.transpose(0,2,3,1)
labels = np.array(labels)
return images,labels
def load_data():
images_train = []
labels_train = []
for i in range(5):
f = 'data_batch_%d'%(i+1)
image_batch,label_batch=load_batch(f)
images_train.append(image_batch)
labels_train.append(label_batch)
Xtrain = np.concatenate(images_train)
Ytrain = np.concatenate(labels_train)
del image_batch,label_batch
Xtest,Ytest = load_batch('test_batch')
print('finished loadding~~~')
return Xtrain,Ytrain,Xtest,Ytest
Xtrain,Ytrain,Xtest,Ytest = load_data()
plt.imshow(Xtrain[6])
label_dict = {0:"airplane",1:"automobile",2:"bird",3:"cat",4:"deer",5:"dog",6:"frog",7:"horse",8:"ship",9:"trunk"}
def plot_images_labels_prediction(images,labels,prediction,idx,num=10):
fig = plt.gcf()
fig.set_size_inches(12,6)
if num > 10:
num = 10
for i in range(0,num):
ax = plt.subplot(2,5,1+i)
ax.imshow(images[idx],cmap="binary")
title = str(i)+','+label_dict[labels[idx]]
if len(prediction)>0:
title+='=>'+label_dict[prediction[idx]]
ax.set_title(title,fontsize=10)
idx += 1
plt.show()
plot_images_labels_prediction(Xtest,Ytest,[],1,10)
#数据预处理
# 独热编码
from sklearn.preprocessing import OneHotEncoder
encoder = OneHotEncoder(sparse=False)
yy = [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9]]
encoder.fit(yy)
Ytrain_reshape =Ytrain.reshape(-1,1)
Ytrain_onehot = encoder.transform(Ytrain_reshape)
Ytest_reshape = Ytest.reshape(-1,1)
Ytest_onehot = encoder.transform(Ytest_reshape)
Ytrain[:10]
Ytrain_onehot.shape
Ytrain[:5]
Ytrain_onehot[:5]
#定义共享函数
#定义权值
def weight(shape):
return tf.Variable(tf.truncated_normal(shape,stddev=0.1),name='W')
#定义偏值
def bias(shape):
return tf.Variable(tf.constant(0.1,shape=shape),name='b')
#定义卷积操作
#步长为2,即原尺寸长和宽各除2
def max_pool_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')