卷积神经网络(Convolutional Neural Network,CNN)是一种前馈神经网络,它的神经元可以相应一部分覆盖范围内的神经元,并保存了问题的空间结构,对计算机视觉和自然语言处理有出色的表现 。
基本结构包括两层,其一为特征提取层;其二是特征映射层。
通常包含以下类型的层:卷积层、油化层、全连接层
卷积层:
用局部感知提取特征,降低输入参数的层。
滤波器:该层的神经元,具有加权输入并产生输出值,输入是固定大小的图像样本。
特征图:卷积神经网络设定中的特征图对应各层神经元的信号输出。
池化层:
对输入的特征图进行压缩,使特征图变小,简化网络复杂度;进行特征压缩,提取主要特征。
忽略目标的倾斜,旋转之类的相对位置的变化,以提高精度,同时降低了特征图的维度,并且在一定程度上可以避免过拟合。
全连接层:
在整个卷积神经网络中起到“分类器”的作用。在卷积层和池化层执行特征提取和合并之后,在网络末端使用全连接层用于创建特征的最终非线性组合,并用于预测。
(池化层和全连接层之间一般有Flatten层,将多维数据转换为一维数据,其输出便于标准的全连接层的处理。)
手写数字识别
# -*- coding: utf-8 -*-
from keras.datasets import mnist
from matplotlib import pyplot as plt
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras import backend
from keras.utils import np_utils
backend.set_image_data_format('channels_first')
(X_train,y_train),(X_validation,y_validation) = mnist.load_data()
seed = 7
np.random.seed(seed)
#num_pixels = X_train.shape[1] * X_train.shape[2]
#print(num_pixels)
X_train = X_train.reshape(X_train.shape[0],1,28,28).astype('float32')
X_validation = X_validation.reshape(X_validation.shape[0],1,28,28).astype('float32')
#格式化数据0~1
X_train = X_train/255
X_validation = X_validation/255
#one-hot编码
y_train = np_utils.to_categorical(y_train)
y_validation = np_utils.to_categorical(y_validation)
#num_classes = y_validation.shape[1]
#print(num_classes)
def create_model():
model = Sequential()
model.add(Conv2D(32,(5,5),input_shape=(1,28,28),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(15,(3,3),activation='relu'))#large
model.add(MaxPooling2D(pool_size=(2,2)))#large
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(units=128,activation='relu'))#large
model.add(Dense(units=50,activation='relu'))
model.add(Dense(units=10,activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
return model
model = create_model()
model.fit(X_train,y_train,epochs=10,batch_size=200,verbose=2)
#score = model.evaluate(X_validation,y_validation)
#print('MLP: %.2f%%' % (score[1]*100))
score = model.evaluate(X_validation,y_validation,verbose=0)
# print('CNN_Small: %.2f%%' % (score[1]*100))
print('CNN_Large: %.2f%%' % (score[1]*100))
Keras中的图像增强
特征标准化:对整个图像数据集的像素标准化
ZCA白化:
图象的白化处理时线性代数操作,可以减少图像像素矩阵的冗余和相关性。包括主成分分析(PCA)技术白化处理和ZCA白化处理。
ZCA白化转换吼的图像保持原始尺寸,并显示出数据集再机器学习的模型中具有更好的结果。
随机旋转、移动、剪切和反转图像
from keras.datasets import mnist
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot as plt
from keras import backend
backend.set_image_data_format('channels_first')
(X_train,y_train),(X_validation,y_validation) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0],1,28,28).astype('float32')
X_validation = X_validation.reshape(X_validation.shape[0],1,28,28).astype('float32')
imgGen = ImageDataGenerator(featurewise_center=True,featurewise_std_normalization=True) #特征标准化
imgGen = ImageDataGenerator(zca_whitening=True) #zca白化
imgGen = ImageDataGenerator(rotation_range=90) #图像旋转
imgGen = ImageDataGenerator(width_shift_range=0.2,height_shift_range=0.2) #图像移动
imgGen = ImageDataGenerator(horizontal_flip=True,vertical_flip=True) #图像剪切
imgGen = ImageDataGenerator(horizontal_flip=True,vertical_flip=True) #图像反转
imgGen.fit(X_train)
for X_batch,y_batch in imgGen.flow(X_train,y_train,batch_size=9):
for i in range(0,9):
plt.subplot(331+i)
plt.imshow(X_batch[i].reshape(28,28),cmap=plt.get_cmap('gray'))
plt.show()
break
情感分析
词嵌入(Word Embeddings)是一种将词向量化的概念,随最近自然语言处理领域中的突破。
其原理是:单词在高维空间中被编码为实值向量,其中词语之间的像地形意味着向量空间中的接近度。离散词被映射到连续数的向量。
Keras通过嵌入层(Embeddings)将单词的正整数表示转化为词嵌入。嵌入层需要指定词汇大小预期的最大数量,以及输出的各个词向量的维度。
from keras.datasets import imdb
from keras.preprocessing import sequence
from keras.layers.embeddings import Embedding
from keras.layers import Dense,Flatten
from keras.models import Sequential
from keras.layers.convolutional import Conv1D,MaxPooling1D
import numpy as np
#Word EmBeddings
(x_train,y_train),(x_validation,y_validation) = imdb.load_data(num_words=5000)
x_train = sequence.pad_sequences(x_train,maxlen=500)
x_validation = sequence.pad_sequences(x_validation,maxlen=500)
Embedding(5000,32,input_length=500)
seed = 7
top_words = 5000
max_words = 500
out_dimension = 32
batch_size = 128
epochs = 2
#多层感知器模型
def create_model1():
model = Sequential()
model.add(Embedding(top_words,out_dimension,input_length=max_words))
model.add(Flatten())
model.add(Dense(250,activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.summary()
return mode
#卷积神经网络
def create_model2():
model = Sequential()
model.add(Embedding(top_words,out_dimension,input_length=max_words))
model.add(Conv1D(filters=32,kernel_size=3,padding='same',activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(250,activation='relu'))
model.add(Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.summary()
return model
if __name__ == '__main__':
np.random.seed(seed=seed)
(x_train,y_train),(x_validation,y_validation) = imdb.load_data(num_words=top_words)
x_train = sequence.pad_sequences(x_train,maxlen=max_words)
x_validation = sequence.pad_sequences(x_validation,maxlen=max_words)
model = create_model2()
model.fit(x_train,y_train,validation_data=(x_validation,y_validation),
batch_size=batch_size,epochs=epochs,verbose=2)
(来自19年草稿箱)