开始 Keras 序列模型(Sequential model)
序列模型是一个线性的层次堆栈。
你可以通过传递一系列 layer 实例给构造器来创建一个序列模型。
The Sequential
model is a linear stack of layers.
You can create a Sequential
model by passing a list of layer instances to the constructor:
from keras.models import Sequential
from keras.layers import Dense, Activation
model = Sequential([
Dense(32, input_shape=(784,)),
Activation('relu'),
Dense(10),
Activation('softmax'),
])
也可以简单的添加 layer 通过 .add() 函数。
You can also simply add layers via the .add()
method:
model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))
Specifying the input shape
The model needs to know what input shape it should expect. For this reason, the first layer in a Sequential
model (and only the first, because following layers can do automatic shape inference) needs to receive information about its input shape. There are several possible ways to do this:
- Pass an
input_shape
argument to the first layer. This is a shape tuple (a tuple of integers orNone
entries, whereNone
indicates that any positive integer may be expected). Ininput_shape
, the batch dimension is not included. - Some 2D layers, such as
Dense
, support the specification of their input shape via the argumentinput_dim
, and some 3D temporal layers support the argumentsinput_dim
andinput_length
. - If you ever need to specify a fixed batch size for your inputs (this is useful for stateful recurrent networks), you can pass a
batch_size
argument to a layer. If you pass bothbatch_size=32
andinput_shape=(6, 8)
to a layer, it will then expect every batch of inputs to have the batch shape(32, 6, 8)
.
As such, the following snippets are strictly equivalent:
model = Sequential()
model.add(Dense(32, input_shape=(784,)))
model = Sequential()
model.add(Dense(32, input_dim=784))
Compilation
Before training a model, you need to configure the learning process, which is done via the compile
method. It receives three arguments:
- An optimizer. This could be the string identifier of an existing optimizer (such as
rmsprop
oradagrad
), or an instance of theOptimizer
class. See: optimizers. - A loss function. This is the objective that the model will try to minimize. It can be the string identifier of an existing loss function (such as
categorical_crossentropy
ormse
), or it can be an objective function. See: losses. - A list of metrics. For any classification problem you will want to set this to
metrics=['accuracy']
. A metric could be the string identifier of an existing metric or a custom metric function.
# 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=