目录
简单示例
数据获取
数据预处理
序列填充Sequence Padding
One-Hot Encoding: 常用于类别标签的转换
模型结构
序列模型 Sequential Model
Multilayer Perceptron (MLP)
二分类 Binary Classification
多分类
回归
卷积网络Convolutional Neural Network (CNN)
循环神经网络RNN
划分数据集为训练/测试
数据标准化Standardization / Normalization
查看模型的配置 Inspect Model
模型的编译 Compile Model
模型的训练fit
模型的性能评估evaluate
模型的预测结果
模型的保存& 加载 Save/ Reload
模型的精调Model Fine-tuning
最优化参数
提前终止 Early Stopping
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)) # 添加一个全连接层,有32个神经元,激活函数为relu,输入的维度等于data的列数
model.add(Dense(1, activation='sigmoid')) #添加一个全连接层,作为输出层,用一个sigmoid输出预测的类别概率值
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy']) # 模型编译,以便于训练
model.fit(data,labels,epochs=10,batch_size=32) # 模型的训练/拟合,传入数据和标签,训练10轮,批量大小为32
predictions = model.predict(data) # 对data预测其label
'''导入常用的机器学习数据集'''
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() # cifar图片数据集
(x_train4,y_train4),(x_test4,y_test4) = imdb.load_data(num_words=20000) # imdb影评数据集
num_classes = 10 # 类别数
# 其它方式:爬虫
from urllib.request import urlopen
data = np.loadtxt(urlopen("http://archive.ics.uci.edu/ ml/machine-learning-databases/pima-indians-diabetes/ pima-indians-diabetes.data"),delimiter=",") # 爬取糖尿病数据集
X = data[:,0:8]
y = data [:,8]
padding 和 one-hot 操作经常会共同出现
from keras.preprocessing import sequence
x_train4 = sequence.pad_sequences(x_train4,maxlen=80)
x_test4 = sequence.pad_sequences(x_test4,maxlen=80)
'''将整型标签转为onehot。y为int数组,num_classes为标签类别总数,大于max(y)(标签从0开始的)。'''
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 keras.models import Sequential
model = Sequential()
model2 = Sequential()
model3 = Sequential()
''' 二分类的输出层用activation='sigmoid '''
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'))
''' 多个类别的最后一层用activation='softmax''''
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))
from keras.layers import Activation,Conv2D,MaxPooling2D,Flatten
''' Conv2D二维卷积 ,可以写多个像下面这样的层,再拍平Flatten'''
model2.add(Conv2D(32,(3,3),padding='same',input_shape=x_train.shape[1:]))
model2.add(Activation('relu'))
model2.add(MaxPooling2D(pool_size=(2,2))) # 最大池化
model2.add(Dropout(0.25))
model2.add(Flatten())
model2.add(Dense(512)) # 全连接层,靠近输出的全连接层通常要Dropout,
model2.add(Activation('relu'))
model2.add(Dropout(0.5))
model2.add(Dense(num_classes)) # 输出层,的神经元数目等于类别数,用softmax激活函数
model2.add(Activation('softmax'))
from keras.klayers import Embedding,LSTM
model3.add(Embedding(20000,128)) # RNN常用于处理序列问题,文本处理就属于序列问题,通常需要词嵌入Embedding
model3.add(LSTM(128,dropout=0.2,recurrent_dropout=0.2)) # 使用LSTM单元
model3.add(Dense(1,activation='sigmoid'))
'''训练集/验证集划分的模块train_test_split '''
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)
model.output_shape # 模型输出的维度 Model output shape
model.summary() # Model summary representation
model.get_config() # 模型配置信息 Model configuration
model.get_weights() #列出模型中所有的张量权重 List all weight tensors in the model
''' MLP: Binary Classification 二分类的损失用二分类的交叉熵binary_crossentropy'''
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
''' MLP: Multi-Class Classification,多元分类的损失categorical_crossentropy,用精度accuracy来评价模型'''
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
''' MLP: Regression 回归问题用mse均方误差,mae是平均绝对偏差 '''
model.compile(optimizer='rmsprop',loss='mse', metrics=['mae'])
'''Recurrent Neural Network '''
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))
#Evaluate Your Model's Performance
score = model3.evaluate(x_test,y_test, batch_size=32)
Prediction
model3.predict(x_test4, batch_size=32)
model3.predict_classes(x_test4,batch_size=32) # 预测类别predict_classes
from keras.models import load_model
model3.save('model_file.h5') # 保存,save里写保存的path路径
my_model = load_model('my_model.h5') # 加载
# Optimization Parameters
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])