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.
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.
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.
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.
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
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.
The answer of Akhilesh as function:
def getLayerIndexByName(model, layername):
for idx, layer in enumerate(model.layers):
if layer.name == layername:
return idx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With