3.3 keras模型构建的三种方式
程序员文章站
2022-03-16 18:05:16
...
3.3 keras
模型构建的三种方式
1. 使用tf.keras.Sequential
按层顺序构建模型,代码示例:
model = Sequential()
#卷积层conv_1_1
model.add(Cov2D(input_shape = (64, 64, 3),filters = 32,
kernel_size = 3, activation = 'relu', kernel_initializer = 'he_uniform', name = 'conv_1_1)
#卷积层 conv_1_2
model.add(Cov2D(filters = 32, kernel_size = 3,
activation = 'relu', kernel_initializer = 'he_uniform', name = 'conv_1_2)
#池化层max_pool_1
model.add(MaxPool2D(pool_size = 32, name = 'max_pool_1))
#展平层
model.add(Flatten(name = 'flatten'))
#全连接层
model.add(Dense(unit = 6, activation = 'softmax', name = 'logit'))
#设置损失函数loss、优化器optimizer、评价指标metrics
model.compile(loss="categorical_crossentropy",
optimizer = tf.keras.optimizers.SGD(learning_rate = 0.001),
metrics = ["accuracy"])
或者:
model = Sequential([
Conv2D(input_shape = (64, 64, 3), filters = 32,
kernel_size = 3, activation = 'relu', name = 'conv_1_1'),
Conv2D(filters = 32, kernel_size = 3, activation = 'relu', name = 'conv_1_2'),
MaxPool2D(poo_size = 2, anme = 'max_pool_1'),
Flatten(name = 'flatten'),
Dense(units = 6, activation = "softmax", name = 'logit')])
#设置损失函数loss、优化器optimizer、评价标准metrics
model.compile(loss="categorical_crossentropy",
optimizer = tf.keras.optimizers.SGD(learning_rate = 0.001),
metrics = ["accuracy"])
适用场合:对于顺序结构的模型(没有多个输入输出,也没有分支),优先使用Sequential
方法构建。
缺点:不能创建以下模型结构
- 共享层
- 模型分支
- 多个输入分支
- 多个输出分支
2. Keras
函数式API创建模型,代码示例:
#输入层input
input = input(shape = (64, 64, 3), name = 'input')
#卷积层conv_1_2
x = Conv2D(filters = 32, kernel_size = 3, activation = 'relu', name = 'conv_1_1')(input)
#卷积层con_1_2
x = Conv2D(filters = 32, kernel_size = 3, activation = 'relu', name = 'conv_1_2)(x)
#池化层max_pool_1
x = MaxPool2D(pool_size = 2, name = 'max_pool_1)(x)
#展平层
x = Flatten(name = 'flatten')(x)
#全连接层
output = Dense(units = 6, activation = "softmax", name = 'logit')(x)
model = Model(inputs = input, outputs = output)
#设置损失函数loss、优化器optimizer、评价标准metrics
model.compile(loss="categorical_crossentropy",
optimizer = tf.keras.optimizers.SGD(learning_rate = 0.001),
metrics = ["accuracy"])
适用场合:如果模型有多输入或者多输出,或者模型需要共享权重,或者模型具有分支连接、循环连接等非顺序结构,推荐使用函数式API进行创建。
3. Keras Model Subclassing
方式,代码示例:
#定义一个子类来搭建模型
class ConvModel(Model):
def __init__(self):
#父类初始化
super(ConvModel, self).__init__()
#卷积层conv_1_1
self.conv_1_1 = Conv2D(input_shape = (64, 64, 3),
filters = 32, kernel_size = 3, activation = 'relu', name = 'con_1_1')
#卷积层conv_1_2
self.conv_1_2 = Conv2D(filters = 32, kernel_size = 3,
activation = 'relu', name = 'conv_1_2')
#池化层max_pool_1
self.max_pool_1 = MaxPool2D(pool.size = 2, name = 'max_pool_1')
#展平层flatten
self.dense = Dense(units = 6, activation = "softmax", name = 'logit')
def call(selfm, x):
x = self.conv_1_1(x)
x = self.conv_1_2(x)
x = self.max_pool_1(x)
x = self.conv_2_1(x)
x = self.conv_2_2(x)
x = self.max_pool_2(x)
x = self.flatten(x)
x = self.dense(x)
return x
#类实例化
model = ConvModel()
构造tf.keras.Model
的子类来编写模型,需要覆写Model类中的__init__
方法和call
方法。__init__
方法中定义我们要使用的层,这里可以使用Keras
自带的层;call
方法中实现模型的网络层。
适用场合:需要编写自定义的模型,如在网络中使用自定义的层、自定义的损失函数、自定义的**函数等。