本次作业讲了一些keras的基本用法,允许搭建自由度很高的CNN。
(参数是参考的)
Keras非常适合快速制作原型,你可以在很短的时间内,建立一个能够获得出色结果的模型。
这是Keras中的模型构建示例:
def model(input_shape):
# Define the input placeholder as a tensor with shape input_shape. Think of this as your input image!
X_input = Input(input_shape)
# Zero-Padding: pads the border of X_input with zeroes
X = ZeroPadding2D((3, 3))(X_input)
# CONV -> BN -> RELU Block applied to X
X = Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0')(X)
X = BatchNormalization(axis = 3, name = 'bn0')(X)
X = Activation('relu')(X)
# MAXPOOL
X = MaxPooling2D((2, 2), name='max_pool')(X)
# FLATTEN X (means convert it to a vector) + FULLYCONNECTED
X = Flatten()(X)
X = Dense(1, activation='sigmoid', name='fc')(X)
# Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
model = Model(inputs = X_input, outputs = X, name='HappyModel')
return model
请注意,Keras使用变量名与我们之前使用numpy和TensorFlow不同。不是在正向传播的每个步骤上创建和分配新变量,例如X
, Z1
, A1
, Z2
, A2
等,以用于不同层的计算, Keras代码上面的每一行只是使用X = ...
将X
重新分配给新值。换句话说,在正向传播的每个步骤中,我们只是将计算中的最新值写入相同的变量X
。唯一的例外是X_input
,我们将其分开并没有覆盖,因为我们最终需要它来创建Keras模型实例(上面的model = Model(inputs = X_input, ...)
)。
练习:实现一个HappyModel()
。我们建议你首先使用我们建议的结构来实现模型,然后再使用该模型作为初始模型来完成本任务的其余部分。之后请返回并主动尝试其他模型架构。例如,你可能会从上面的模型中获得启发,但是随后根据需要更改网络体系结构和超参数。你还可以使用其他函数,例如AveragePooling2D()
, GlobalMaxPooling2D()
, Dropout()
。
注意:注意数据的维度。利用你在视频中学到的知识,确保卷积,池化和全连接层适用。
# GRADED FUNCTION: HappyModel
def HappyModel(input_shape):
"""
Implementation of the HappyModel.
Arguments:
input_shape -- shape of the images of the dataset
Returns:
model -- a Model() instance in Keras
"""
### START CODE HERE ###
# Feel free to use the suggested outline in the text above to get started, and run through the whole
# exercise (including the later portions of this notebook) once. The come back also try out other
# network architectures as well.
X_input = Input(shape=input_shape)
X = ZeroPadding2D(padding=(1, 1))(X_input)
X = Conv2D(8, kernel_size=(3,3), strides=(1,1))(X)
X = BatchNormalization(axis=3)(X)
X = Activation('relu')(X)
X = MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid')(X)
X = ZeroPadding2D(padding=(1, 1))(X)
X = Conv2D(16, kernel_size=(3,3), strides=(1,1))(X)
X = BatchNormalization(axis=3)(X)
X = Activation('relu')(X)
X = MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid')(X)
X = ZeroPadding2D(padding=(1, 1))(X)
X = Conv2D(32, kernel_size=(3,3), strides=(1,1))(X)
X = BatchNormalization(axis=3)(X)
X = Activation('relu')(X)
X = MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid')(X)
# FC
X = Flatten()(X)
Y = Dense(1, activation='sigmoid')(X)
model = Model(inputs = X_input, outputs = Y, name='HappyModel')
### END CODE HERE ###
return model
现在,你已经构建了一个描述模型的函数。为了训练和测试该模型,Keras中有四个步骤:
model.compile(optimizer = "...", loss = "...", metrics = ["accuracy"])
编译模型model.fit(x = ..., y = ..., epochs = ..., batch_size = ...)
训练模型model.evaluate(x = ..., y = ...)
测试模型如果你想进一步了解model.compile()
,model.fit()
,model.evaluate()
及其参数,请参考官方 Keras documentation
练习:第一步,创建模型
happyModel = HappyModel((64, 64, 3))
练习:实施第2步,编译模型以配置学习过程。 正确选择 compile()
的3个参数。
提示:“快乐挑战”是一个二进制分类问题。
### START CODE HERE ### (1 line)
happyModel.compile(optimizer=keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0), loss='binary_crossentropy', metrics=['accuracy'])
### END CODE HERE ###
练习:实施第3步,训练模型。选择epoch和批次大小。
### START CODE HERE ### (1 line)
happyModel.fit(x=X_train, y=Y_train, batch_size=16, epochs=20)
### END CODE HERE ###
如果再次运行fit()
,model
将继续使用已经学习的参数进行训练,而不是重新初始化它们。
练习:实施第4步,测试/评估模型。
### START CODE HERE ### (1 line)
preds = happyModel.evaluate(x=X_test, y=Y_test)
### END CODE HERE ###
print()
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))