卷积神经网络在图片和语言识别方面能够给出更优预测结果。
卷积神经网络最常被应用的方面是计算机的图像识别, 不过因为不断地创新, 它也被应用在视频分析, 自然语言处理, 药物发现, 等等。
神经网络是由一连串的神经层组成,每一层神经层里面存在有很多的神经元. 这些神经元就是神经网络识别事物的关键. 每一种神经网络都会有输入输出值, 当输入值是图片的时候, 实际上输入神经网络的并不是那些色彩缤纷的图案,而是一堆堆的数字
当神经网络需要处理这么多输入信息的时候, 也就是卷积神经网络就可以发挥它的优势的时候了.
卷积神经网络拆开为 “卷积” 和 “神经网络”
猫的图片:长、宽、高(高指的是图片的颜色信息,黑白高只有1;若是彩色,则可能有RGB三种信息,高度为3) 三个参数
过滤器就是图片上方不断移动的,不断在图片收集小批的像素块,收集完所有信息后,输出的值可以理解为一个高度更高,长宽更小的图片,图片中包含了一些边缘信息
以同样的步骤,再进行多次卷积,长宽再次压缩,高度增加,产生对输入图片更深的理解,将压缩的信息潜入普通的神经分析层上,就能对这些信息进行分类了
在每一次卷积的时候, 神经层可能会无意地丢失一些信息。
池化 (pooling) 可以解决该问题。
也就是说在卷积的时候, 不压缩长宽, 尽量地保留更多信息, 压缩的工作就交给池化。这样的一项附加工作能够很有效的提高准确性。
例:识别手写数字
"""
View more, visit my tutorial page: https://mofanpy.com/tutorials/
My Youtube Channel: https://www.youtube.com/user/MorvanZhou
Dependencies:
torch: 0.4
torchvision
matplotlib
"""
# library
# standard library
import os
# third-party library
import torch
import torch.nn as nn
import torch.utils.data as Data
import torchvision
import matplotlib.pyplot as plt
# torch.manual_seed(1) # reproducible
# Hyper Parameters
EPOCH = 1 # train the training data n times, to save time, we just train 1 epoch
BATCH_SIZE = 50
LR = 0.001 # learning rate
DOWNLOAD_MNIST = False
# Mnist digits dataset
if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
# not mnist dir or mnist is empyt dir
DOWNLOAD_MNIST = True
# Mnist 手写数字 下载数据
train_data = torchvision.datasets.MNIST(
root='./mnist/', # 保存位置
train=True, # this is training data 下载的数据是黑白的手写数字,故只有黑白两个层
transform=torchvision.transforms.ToTensor(), # Converts a PIL.Image or numpy.ndarray to 转换 PIL.Image or numpy.ndarray 成
# torch.FloatTensor (C x H x W), 训练的时候 normalize 成 [0.0, 1.0] 区间
# torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
download=DOWNLOAD_MNIST, # 没下载就下载, 下载了就不用再下了
)
# plot one example
print(train_data.train_data.size()) # (60000, 28, 28)
print(train_data.train_labels.size()) # (60000)
plt.imshow(train_data.train_data[0].numpy(), cmap='gray')
plt.title('%i' % train_data.train_labels[0])
plt.show()
# Data Loader for easy mini-batch return in training, the image batch shape will be (50, 1, 28, 28)
# 批训练 50samples, 1 channel, 28x28 (50, 1, 28, 28)
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
# pick 2000 samples to speed up testing 为了节约时间, 我们测试时只测试前2000个
test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)
test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000]/255. # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)
test_y = test_data.test_labels[:2000]
# 建立神经网络
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
# 一般卷积网络的一层包括了三个东西 卷积核,激活层,pool池化层
self.conv1 = nn.Sequential( # input shape (1, 28, 28)
# 卷积核就是一个过滤器
nn.Conv2d(
in_channels=1, # input height 该图片的厚度为1
out_channels=16, # n_filters 输出的高度(提取出了16个该区域的特征)
kernel_size=5, # filter size 过滤器的宽和高为5个像素点
stride=1, # filter movement/step 过滤器每隔多少步跳一下(指过滤器每次移动多少个像素)
padding=2, # if want same width and length of this image after Conv2d, padding=(kernel_size-1)/2 if stride=1 给图片一圈多加为0的数据
), # output shape (16, 28, 28)
nn.ReLU(), # activation 激励函数
# 筛选重要信息 这一步变成长宽更窄,但高度不会变的图片(pooling压缩)
nn.MaxPool2d(kernel_size=2), # 在 2x2 空间里向下采样 choose max value in 2x2 area, output shape (16, 14, 14)
)
self.conv2 = nn.Sequential( # input shape (16, 14, 14)
nn.Conv2d(16, 32, 5, 1, 2), # output shape (32, 14, 14)
nn.ReLU(), # activation
nn.MaxPool2d(2), # output shape (32, 7, 7)
)
self.out = nn.Linear(32 * 7 * 7, 10) # fully connected layer, output 10 classes 由于是识别数字,因此有10个分类
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size(0), -1) # flatten the output of conv2 to (batch_size, 32 * 7 * 7)
output = self.out(x)
return output, x # return x for visualization
cnn = CNN()
print(cnn) # net architecture
optimizer = torch.optim.Adam(cnn.parameters(), lr=LR) # optimize all cnn parameters
loss_func = nn.CrossEntropyLoss() # the target label is not one-hotted
# following function (plot_with_labels) is for visualization, can be ignored if not interested
from matplotlib import cm
try: from sklearn.manifold import TSNE; HAS_SK = True
except: HAS_SK = False; print('Please install sklearn for layer visualization')
def plot_with_labels(lowDWeights, labels):
plt.cla()
X, Y = lowDWeights[:, 0], lowDWeights[:, 1]
for x, y, s in zip(X, Y, labels):
c = cm.rainbow(int(255 * s / 9)); plt.text(x, y, s, backgroundcolor=c, fontsize=9)
plt.xlim(X.min(), X.max()); plt.ylim(Y.min(), Y.max()); plt.title('Visualize last layer'); plt.show(); plt.pause(0.01)
plt.ion()
# training and testing
for epoch in range(EPOCH):
for step, (b_x, b_y) in enumerate(train_loader): # gives batch data, normalize x when iterate train_loader
output = cnn(b_x)[0] # cnn output
loss = loss_func(output, b_y) # cross entropy loss
optimizer.zero_grad() # clear gradients for this training step
loss.backward() # backpropagation, compute gradients
optimizer.step() # apply gradients
if step % 50 == 0:
test_output, last_layer = cnn(test_x)
pred_y = torch.max(test_output, 1)[1].data.numpy()
accuracy = float((pred_y == test_y.data.numpy()).astype(int).sum()) / float(test_y.size(0))
print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)
if HAS_SK:
# Visualization of trained flatten layer (T-SNE)
tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)
plot_only = 500
low_dim_embs = tsne.fit_transform(last_layer.data.numpy()[:plot_only, :])
labels = test_y.numpy()[:plot_only]
plot_with_labels(low_dim_embs, labels)
plt.ioff()
# print 10 predictions from test data
test_output, _ = cnn(test_x[:10])
pred_y = torch.max(test_output, 1)[1].data.numpy()
print(pred_y, 'prediction number')
print(test_y[:10].numpy(), 'real number')