【发布时间】:2021-02-20 06:31:43
【问题描述】:
您好,我有以下代码,我正在将预先保存的自定义权重从 .cpkt 文件加载到 resnet 模型中。
'''
def resnet_model():
input_tensor = Input(shape=(224,224,3))
base_model = keras.applications.ResNet50(input_tensor=input_tensor,weights = 'imagenet', include_top = False)
for layer in base_model.layers:
layer.trainable = True
x = base_model.output
x = GlobalAveragePooling2D(data_format='channels_last')(x)
x = Dense(256)(x)
l2_norm_final = Lambda(lambda x: K.l2_normalize(x,axis=1))(x)
final_model = Model(inputs=base_model.input, outputs = l2_norm_final)
return final_model
model = resnet_model()
model.load_weights(weights_file_orig)
#this works i.e., W has the model's weights
W = model.get_weights()
#this does not work i.e., w,b have []
all_weights = [], all_biases = []
for layer in model.layers:
w,b = layer.get_weights()
all_weights.append(w)
all_biases.append(b)
'''
如何从保存的 .cpkt 文件中逐层获取权重和偏差?
非常感谢!
【问题讨论】:
-
哪一层不返回权重?您应该考虑到并非所有层都有权重。
-
没有层。它们都返回空列表。
标签: keras keras-layer