有人说,2018年人工智能已经进入了全球爆发的时刻。个性化信息推送、人脸识别、语音操控等人工智能技术,已“入侵”日常生活的细枝末节。
十多年前,所有的企业都在想办法互联网化,如今,所有的互联网企业都在试图AI化,据数据统计,平均每 10.9 个小时会诞生一家 AI 企业。在这样的背景下,不难想象,未来机器学习技术将会是技术人的新门槛和领域。
那么问题来了,作为一名技术者,我该如何转型/学习 AI 技术?别着急,本文将带你入门AI第一课:《手把手教你Keras实现CNN》,让你实现手写数字识别准确率达到99.6%!(附完整代码)。
在我们安装过Tensorflow后,安装Keras默认将TF作为后端,Keras实现卷积网络的代码十分简洁,而且keras中的callback类提供对模型训练过程中变量的检测方法,能够根据检测变量的情况及时的调整模型的学习效率和一些参数. 下面的例子,MNIST数据作为测试:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as pimg
import seaborn as sb # 一个构建在matplotlib上的绘画模块,支持numpy,pandas等数据结构
%matplotlib inline
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix # 混淆矩阵
import itertools
# keras
from keras.utils import to_categorical #数字标签转化成one-hot编码
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPool2D
from keras.optimizers import RMSprop
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
Using TensorFlow backend.
# 设置绘画风格
sb.set(style='white', context='notebook', palette='deep')
# 加载数据
train_data = pd.read_csv('data/train.csv')
test_data = pd.read_csv('data/test.csv')
#train_x = train_data.drop(labels=['label'],axis=1) # 去掉标签列
train_x = train_data.iloc[:,1:]
train_y = train_data.iloc[:,0]
del train_data # 释放一下内存
# 观察一下训练数据的分布情况
g = sb.countplot(train_y)
train_y.value_counts()
1 4684
7 4401
3 4351
9 4188
2 4177
6 4137
0 4132
4 4072
8 4063
5 3795
Name: label, dtype: int64
train_x.isnull().describe() # 检查是否存在确实值
train_x.isnull().any().describe()
count 784
unique 1
top False
freq 784
dtype: object
test_data.isnull().any().describe()
count 784
unique 1
top False
freq 784
dtype: object