最近在读paper,看keras复现模型时看到了除了用Sequential()不断add(layers)的写法之外还有另一种方式,即在layer类后加(layer)这种方式,花了些时间去理解和学习,在此分享一下两种表述。
以下两种代码搭建的是相同的网络结构。
[1] 利用Sequential()作为起始,不断add()后面的layer,架构线性表示,即
y = f ( x ) → z = g ( y ) → m = h ( z ) → … y=f(x) \rightarrow z=g(y) \rightarrow m=h(z) \rightarrow \dots y=f(x)→z=g(y)→m=h(z)→…
from keras.models import Sequential
from keras.layers import Conv2d, Maxpooling2D
from keras.layers import Dense, Dropout, Flatten
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)
[2] 采用一种layer类后加“(layer)”的方式进行搭建,理解上更接近嵌套式:
f ( x ) → g ( f ( x ) ) → h ( g ( f ( x ) ) ) f(x)\rightarrow g(f(x)) \rightarrow h(g(f(x))) f(x)→g(f(x))→h(g(f(x)))
(以下未经程序验证是否可行,仅分享表达方式的理解)
from keras.models import Sequential
from keras.layers import Conv2d, Maxpooling2D
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Input, Output
from keras.models import Model
inputs = Input(shape=(100, 100, 3))
layer = Conv2d(32, (3, 3), activation='relu')(inputs)
# 便于理解,这种表示可以分解为两步
# convlayer = Conv2d(32, (3, 3), activation='relu')
# layer = convlayer(inputs)
layer = Conv2d(32, (3, 3), activation='relu')(layer)
layer = Maxpooling2D(pool_size=(2, 2))(layer)
layer = Dropout(0.25)(layer)
layer = Conv2d(64, (3, 3), activation='relu')(layer)
layer = Conv2d(64, (3, 3), activation='relu')(layer)
layer = Maxpooling2D(pool_size=(2, 2))(layer)
layer = Dropout(0.25)(layer)
layer = Flatten()(layer)
layer = Dense(256, activation='relu')(layer)
layer = Dropout(0.5)(layer)
outputs = Dense(10, activation='softmax')(layer)
model = Model(inputs=[inputs], outputs=[outputs])
# compile步骤相同
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
optimizer=sgd)