Funktion get_weights
gibt eine Liste von numpy Arrays ohne Name Informationen in ihnen zurück.
Wie für Model.get_weights()
, ist es nur die Verkettung von Layer.get_weights()
für jede der [abgeflachten] Schichten.
Allerdings gibt Layer.weights
direkten Zugriff auf die Back-End-Variablen, und diese, ja, können einen Namen haben. Die Lösung besteht dann darin, jedes Gewicht jeder Schicht zu durchlaufen und dessen name
Attribut abzurufen.
Ein Beispiel mit VGG16:
from keras.applications.vgg16 import VGG16
model = VGG16()
names = [weight.name for layer in model.layers for weight in layer.weights]
weights = model.get_weights()
for name, weight in zip(names, weights):
print(name, weight.shape)
die Ausgänge:
block1_conv1_W_6:0 (3, 3, 3, 64)
block1_conv1_b_6:0 (64,)
block1_conv2_W_6:0 (3, 3, 64, 64)
block1_conv2_b_6:0 (64,)
block2_conv1_W_6:0 (3, 3, 64, 128)
block2_conv1_b_6:0 (128,)
block2_conv2_W_6:0 (3, 3, 128, 128)
block2_conv2_b_6:0 (128,)
block3_conv1_W_6:0 (3, 3, 128, 256)
block3_conv1_b_6:0 (256,)
block3_conv2_W_6:0 (3, 3, 256, 256)
block3_conv2_b_6:0 (256,)
block3_conv3_W_6:0 (3, 3, 256, 256)
block3_conv3_b_6:0 (256,)
block4_conv1_W_6:0 (3, 3, 256, 512)
block4_conv1_b_6:0 (512,)
block4_conv2_W_6:0 (3, 3, 512, 512)
block4_conv2_b_6:0 (512,)
block4_conv3_W_6:0 (3, 3, 512, 512)
block4_conv3_b_6:0 (512,)
block5_conv1_W_6:0 (3, 3, 512, 512)
block5_conv1_b_6:0 (512,)
block5_conv2_W_6:0 (3, 3, 512, 512)
block5_conv2_b_6:0 (512,)
block5_conv3_W_6:0 (3, 3, 512, 512)
block5_conv3_b_6:0 (512,)
fc1_W_6:0 (25088, 4096)
fc1_b_6:0 (4096,)
fc2_W_6:0 (4096, 4096)
fc2_b_6:0 (4096,)
predictions_W_6:0 (4096, 1000)
predictions_b_6:0 (1000,)