我想在 for 回圈中多次構建相同的模型:
### Building the model ###
def build_model():
# create
model = Sequential([
InputLayer(input_shape = (28, 28, 1)),
Conv2D(32, (3, 3)),
Activation('relu'),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3)),
Activation('relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(num_classes),
Activation('softmax')
])
# compile
model.compile(
loss = 'categorical_crossentropy',
optimizer = 'adam',
metrics = [ 'accuracy' ]
)
# return
return model
### Fit 100 models ###
for i in range(2):
model = build_model()
model.summary()
我得到以下結果。
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 26, 26, 32) 320
_________________________________________________________________
activation (Activation) (None, 26, 26, 32) 0
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 13, 13, 32) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 11, 11, 64) 18496
_________________________________________________________________
..........
_________________________________________________________________
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_2 (Conv2D) (None, 26, 26, 32) 320
_________________________________________________________________
activation_3 (Activation) (None, 26, 26, 32) 0
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 13, 13, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 11, 11, 64) 18496
_________________________________________________________________
..........
_________________________________________________________________
我想將“conv2d”作為我的第一個 Conv2D 圖層名稱,將“conv2d_1”作為我的第二個 Conv2D 圖層名稱。
有沒有辦法可以在我的所有模型中獲得相同的圖層名稱/圖層參考 ID?
uj5u.com熱心網友回復:
這是使用name所有keras.layers.Layer子項繼承的關鍵字引數的好機會:
model = Sequential([InputLayer(input_shape=(28,28,1), name="my_input"),Conv2D(32, 3, name="my_conv"),...])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/327210.html
上一篇:tensorflow.python.framework.errors_impl.ResourceExhaustedError:無法分配記憶體[Op:AddV2]
