序惯模型是多个网络层的线性堆叠。
可以通过Sequential模型传递一个layer的list来构造该模型:
from keras.models import Sequential
from keras.layers import Dense,Activation
model =Sequential([
Dense(32,units=784),
Activation('relu'),
Dense(10)
Activation('softmax'),
])
也可以通过.add()方法一个个的将模型放入:
model =Sequential()
model.add(Dense(32,input_shape(784,)))
model.add(Activation('relu')
指定输入数据的shape
模型需要知道数据的shape,Sequential的第一层需要接受一个关于输入数据shape的参数,后面的各个层可以自动推导出中间数据的shape。
- 传递一个input_shape的关键字给第一层,input_shape是一个tuple类型数据,其中也可以填None,如果填入None则表示此位置是任何正整数。batch大小不包含其中。
- 有些2D层,如Dense支持通过指定其输入维度input_dim来隐含指定输入数据shape。一些3D的时域支持通过参数input_dim和input_length来指定输入shape.
- 如果需要为输入指定一个固定大小的batch_size,可以传递batch_size参数到一个层中,例如你想指定输入张量的batch大小是32,数据shape是(6,8),则你需要传递batch_size=32和input_shape=(6,8)。
model=Sequential()
model.add(Dense(32,input_shape=(784,)))
model=Sequential()
model.add(Dense(32,input_shape(784,))
编译
在模型训练前,我们需要通过compile来对学习过程进行配置。compile接受三个参数:
- 优化器optimizer:该参数可指定为已预定义的优化器名,如rmsprop、adagrad,或一个Optimizer类对象,详情见optimizer
- 损失函数loss:该参数为模型试图最小化的目标函数,它可为预定义的损失函数名,如categorical_crossentropy、mse,也可以为一个损失函数,可见losses
- 指标列表metrics:对分类问题,我们一般将该列表设置为metrics=[‘accuracy’]。指标可以使一个预定义指标名字,也可以是一个用户定制的函数,指标函数应该返回单个张量,或一个完成metric_name->metric_vallue映射的字典,请参考性能评估
# For a multi-class classification problem
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# For a binary classification problem
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
# For a mean squared error regression problem
model.compile(optimizer='rmsprop',
loss='mse')
# For custom metrics
import keras.backend as K
def mean_pred(y_true, y_pred):
return K.mean(y_pred)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy', mean_pred])
训练
keras以Numpy数组作为输入数据和标签的数据类型。训练模型一般使用fit函数,该函数详情[见这里](http://keras-cn.readthedocs.io/en/latest/models/sequential/)。
例子:
#for a single-input model with 2 classes(二分类)
model =Sequential()
model.add(Dense(32,activation='relu',input_dim=100))
model.add(Dense(1,activation='sigmoid'))
model.compile(optimizer='rmsprop'
loss = 'binary_crossentropy'
metrics = ['accuracy'])
#Generate dummy data
import numpy as np
data=np.random.random((1000,100))
labels = np.random.randint(2,size(1000,1))
#Train the model,iterating on the data in batches of 32 samples
model.fit(data,labels,epoches=10,batch_size=32)
# For a single-input model with 10 classes (categorical classification):
model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Generate dummy data
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(10, size=(1000, 1))
# Convert labels to categorical one-hot encoding
one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)
# Train the model, iterating on the data in batches of 32 samples
model.fit(data, one_hot_labels, epochs=10, batch_size=32)
例子:
在Keras代码包的examples文件夹中,你将找到使用真实数据的示例模型:
- CIFAR10 小图片分类:使用CNN和实时数据提升
-
MNIST手写数字识别:使用多层感知器和CNN
基于多层感知机的softmax分类:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD
# Generate dummy data
import numpy as np
x_train = np.random.random((1000, 20))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(Dense(64, activation='relu', input_dim=20))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
model.fit(x_train, y_train,
epochs=20,
batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)
类似VGG的卷积神经网络:
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD
# Generate dummy data
x_train = np.random.random((100, 100, 100, 3))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)
x_test = np.random.random((20, 100, 100, 3))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)
model = Sequential()
# input: 100x100 images with 3 channels -> (100, 100, 3) tensors.
# this applies 32 convolution filters of size 3x3 each.
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
model.fit(x_train, y_train, batch_size=32, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=32)