Weights by name in Keras -


after training model using keras, can list of weight arrays using:

mymodel.get_weights()  

or

mylayer.get_weights() 

i'd know names corresponding each weight array. know how indirectly saving model , parsing hdf5 file surely there must direct way accomplish this?

function get_weights returns list of numpy arrays no name information in them.

as model.get_weights(), it's concatenation of layer.get_weights() each 1 of [flattened] layers.

however, layer.weights gives direct access backend variables, , these, yes, may have name. solution iterate through each weight of each layer, retrieving name attribute.

an example vgg16:

from keras.applications.vgg16 import vgg16   model = vgg16()  names = [weight.name layer in model.layers weight in layer.weights] weights = model.get_weights()  name, weight in zip(names, weights):     print(name, weight.shape) 

which outputs:

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,) 

Comments

Popular posts from this blog

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - SSE Emitter : Manage timeouts and complete() -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -