Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: How to get layer index when already know layer name?

Tags:

python

keras

I already knew the name of a layer of a model, now I want to know that layer's index. Is there any available function to do that? Thank you all.

like image 899
Nguyễn Công Minh Avatar asked May 03 '18 09:05

Nguyễn Công Minh


People also ask

How to get the layer by name in keras?

It would seem that you would just pass name = "name" to your layer constructor. To get the layer by name use: from keras.layers import Input, Embedding, LSTM, Dense, merge from keras.models import Model # headline input: meant to receive sequences of 100 integers, between 1 and 10000.

What is the difference between Line 7 and Line 9 in keras?

Line 7 creates a new model using Sequential API. Line 9 creates a new Dense layer and add it into the model. Dense is an entry level layer provided by Keras, which accepts the number of neurons or units (32) as its required parameter. If the layer is first layer, then we need to provide Input Shape, (16,) as well.

What is the difference between constraints and regularizer in keras?

In between, constraints restricts and specify the range in which the weight of input data to be generated and regularizer will try to optimize the layer (and the model) by dynamically applying the penalties on the weights during optimization process. To summarise, Keras layer requires below minimum details to create a complete layer.

How to get the IDX of layer name from model name?

Suppose your model is model and the layerName is name of the layer. index = None for idx, layer in enumerate (model.layers): if layer.name == layerName: index = idx break Here index is the idx of required name. def getLayerIndexByName (model, layername): for idx, layer in enumerate (model.layers): if layer.name == layername: return idx


2 Answers

Suppose your model is model and the layerName is name of the layer.

index = None
for idx, layer in enumerate(model.layers):
    if layer.name == layerName:
        index = idx
        break

Here index is the idx of required name.

like image 109
Akhilesh Avatar answered Oct 20 '22 05:10

Akhilesh


The answer of Akhilesh as function:

def getLayerIndexByName(model, layername):
    for idx, layer in enumerate(model.layers):
        if layer.name == layername:
            return idx
like image 5
maniac Avatar answered Oct 20 '22 05:10

maniac