Tensorflow keras:从配置 'Keyword argument not understood:'、'axis' 创建层

Tensorflow keras: creating layer from config 'Keyword argument not understood:', 'axis'

代码:

config = MCM_feature_extractor.get_layer(index=132).get_config()
x = tf.keras.layers.Layer()
x = x.from_config(config)

配置:

{'axis': ListWrapper([3]),
 'beta_constraint': None,
 'beta_initializer': {'class_name': 'Zeros', 'config': {}},
 'beta_regularizer': None,
 'center': True,
 'dtype': 'float32',
 'epsilon': 1.001e-05,
 'gamma_constraint': None,
 'gamma_initializer': {'class_name': 'Ones', 'config': {}},
 'gamma_regularizer': None,
 'momentum': 0.99,
 'moving_mean_initializer': {'class_name': 'Zeros', 'config': {}},
 'moving_variance_initializer': {'class_name': 'Ones', 'config': {}},
 'name': 'conv4_block5_preact_bn',
 'scale': True,
 'trainable': True}

我做得对吗?我想从 MCM 模型复制几层并使用它们创建另一个使用函数 API 的模型,然后复制权重。

解法:

layer = MCM_feature_extractor.get_layer(index=132)
config = layer.get_config()
weights = layer.get_weights()

config['name'] = layer.name+'_2'
second_layer = type(layer).from_config(config)
second_layer.build(layer.input_shape)
second_layer.set_weights(weights)
second_layer = second_layer(title_input)