利用Keras搭建深度学习模型
Keras
Keras是一个强大且好用的Python函数库,它是建立在Tensorflow之上的一个高阶深度学习API。简单来说,我们使用Keras构建深度学习模型的时候,表面上我们是在用Keras语言来编写,但实际上后台的运行是通过Tensorflow来进行。换句话来说,Keras的内核实际上仍然是Tensorflow,只不过是用一些更为简单的语句来实现(这种方式在某种程度上也丧失了一部分的灵活性,因此并不能够完全替代Tensorflow)。
Keras的整体风格与Scikit-learn有些类似,构建模型基本也是一键fit的模式,只需要自己设置一下每一层网络的参数即可。先为大家送上一个简单的例子:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
# 随机生成一组数据
data = np.random.random((1000,100))
# 随机生成标签
labels = np.random.randint(2,size=(1000,1))
model = Sequential()
# 添加一层神经网络
model.add(Dense(32,
activation='relu',
input_dim=100))
# 添加激活函数(activate function)
model.add(Dense(1, activation='sigmoid'))
# 构建模型,定义优化器及损失函数
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
# 模型与数据一键fit
model.fit(data,labels,epochs=10,batch_size=32)
predictions = model.predict(data)
与其他Python函数库一样,Keras也有随库自带的数据,方便我们熟悉Keras,以下是四组经典数据的载入方式:
from keras.datasets import boston_housing, mnist, cifar10, imdb
(x_train,y_train),(x_test,y_test) = mnist.load_data()
(x_train2,y_train2),(x_test2,y_test2) = boston_housing.load_data()
(x_train3,y_train3),(x_test3,y_test3) = cifar10.load_data()
(x_train4,y_train4),(x_test4,y_test4) = imdb.load_data(num_words=20000)
num_classes = 10
1.数据预处理
数据要fit进Keras模型,自然少不了预处理的工作,Keras也为我们提供了一些预处理的方法:
将数据填充至指定长度(maxlen),默认填充值value = 0.0:
from keras.preprocessing import sequence
x_train4 = sequence.pad_sequences(x_train4,maxlen=80)
x_test4 = sequence.pad_sequences(x_test4,maxlen=80)
创建虚拟变量:
from keras.utils import to_categorical
Y_train = to_categorical(y_train, num_classes)
Y_test = to_categorical(y_test, num_classes)
Y_train3 = to_categorical(y_train3, num_classes)
Y_test3 = to_categorical(y_test3, num_classes)
训练集、测试集分离:
from sklearn.model_selection import train_test_split
X_train5,X_test5,y_train5,y_test5 = train_test_split(X, y,
test_size=0.33,
random_state=42)
标准化(归一化同理):
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(x_train2)
standardized_X = scaler.transform(x_train2)
standardized_X_test = scaler.transform(x_test2)
2.模型构造
序列模型:
from keras.models import Sequential
model = Sequential()
model2 = Sequential()
model3 = Sequential()
2-class分类模型:
from keras.layers import Dense
model.add(Dense(12, input_dim=8,
kernel_initializer='uniform',
activation='relu'))
model.add(Dense(8,kernel_initializer='uniform',activation='relu'))
model.add(Dense(1,kernel_initializer='uniform',activation='sigmoid'))
Multi-class分类模型:
from keras.layers import Dropout
model.add(Dense(512,activation='relu',input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512,activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10,activation='softmax'))
回归模型:
model.add(Dense(64,activation='relu',input_dim=train_data.shape[1]))
model.add(Dense(1))
构建卷积神经网络(CNN):
from keras.layers import Activation,Conv2D,MaxPooling2D,Flatten
model2.add(Conv2D(32,(3,3),padding='same',input_shape=x_train.shape[1:]))
model2.add(Activation('relu'))
model2.add(Conv2D(32,(3,3)))
model2.add(Activation('relu'))
model2.add(MaxPooling2D(pool_size=(2,2)))
model2.add(Dropout(0.25))
model2.add(Conv2D(64,(3,3), padding='same'))
model2.add(Activation('relu'))
model2.add(Conv2D(64,(3, 3)))
model2.add(Activation('relu'))
model2.add(MaxPooling2D(pool_size=(2,2)))
model2.add(Dropout(0.25))
model2.add(Flatten())
model2.add(Dense(512))
model2.add(Activation('relu'))
model2.add(Dropout(0.5))
model2.add(Dense(num_classes))
model2.add(Activation('softmax'))
构建循环神经网络(RNN):
from keras.klayers import Embedding,LSTM
model3.add(Embedding(20000,128))
model3.add(LSTM(128,dropout=0.2,recurrent_dropout=0.2))
model3.add(Dense(1,activation='sigmoid'))
模型细节提取:
# 模型输出形状
model.output_shape
# 模型总结
model.summary()
# get模型参数
model.get_config()
# get神经网络
weightsmodel.get_weights()
编译模型:
# 2-class分类问题
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
# multi-class分类问题
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# 回归问题
model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
# RNN
model3.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
训练模型:
model3.fit(x_train4,
y_train4,
batch_size=32,
epochs=15,
verbose=1,
validation_data=(x_test4,y_test4))
评价模型:
score = model3.evaluate(x_test,
y_test,
batch_size=32)
预测结果:
model3.predict(x_test4, batch_size=32)
model3.predict_classes(x_test4,batch_size=32)
保存/载入模型:
from keras.models import load_model
model3.save('model_file.h5')
my_model = load_model('my_model.h5')
模型参数调节:
from keras.optimizers import RMSprop
opt = RMSprop(lr=0.0001, decay=1e-6)
model2.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
模型提前终止:
from keras.callbacks import EarlyStopping
early_stopping_monitor = EarlyStopping(patience=2)
model3.fit(x_train4,
y_train4,
batch_size=32,
epochs=15,
validation_data=(x_test4,y_test4),
callbacks=[early_stopping_monitor])
结语
以上内容基本涵盖了建立Keras模型的基本步骤。Keras有着与Scikit-learn非常相似的语句模式,但是Keras的使用确实要更为复杂一点,其中主要的原因就在于神经网络本身结构的复杂性。尽管如此,Keras相比于Tensorflow和Pytorch等框架,仍是入门深度学习的最佳选择。
往期文章推荐:
想成为一名合格的数据科学家/AI工程师吗?关注“机器学习学社”获取每天一份的新鲜咨询,为你开拓属于你自己的AI之路
扫码关注机器学习学社,更多资讯早知道,更多学习干货等你来拿