“AI” How to Get Weight and Bias of Learned MLP Model by Using Keras.


Hi, I’m Higashi.


In this page, I introduce how to get weight and bias of learned MLP (Multi Layer Perceptron)  model built by “Tensorflow-Keras”.


This time, MLP model is used, but same method can apply to other neural network model ,for example RNN or LSTM and so on.


So, let’s get started!!


Sponsored Links

Introduction of Sample MLP Model

This time, I use below simple MLP model.

It’s not shown in the image above, input layer’s dimension is 4, and intermediate layers has 20 and 10 dimension and output layer’s dimension is 1.


I will get all weight and bias from this model.


From now, I will proceed on the prerequisite that this learned MLP model is stored in variable “model”.


Sponsored Links

How to Cut off a layer from Model

At first, we should cut off one layer from model.


The method is below.

layer = model.layers[ i ]
layer : Variable of After Cutting off the Layer
i  : Index of Layer


By this method, variable “layer” has the information of one designated layer from model.

Of course weight and bias is include in this information.


Sponsored Links

How to get Weight and Bias from Layer

How to get weight and bias from cut layer is below.

*In this case, variable “layer”  is made by above mothod.



You can conduct directly without cutting off a layer shown below.

weights = model.layers[i].get_weights()[0]


Sponsored Links

Result of Obtaining Weight and Bias on All Layers

By adopting these method, I will get all weight and bias of MLP model shown in the beginning of this page.


The sample program is below.

#First Layer's Process
layer1 = model.layers[0]
#Second Layer's Process
layer2 = model.layers[1]
#Third Layer's Process
layer3 = model.layers[2]


By conducted this program, each variable has weight and bias value.


Let’s check the size of each variables.

print('First_Weight_Shape=', L1_weights.shape)
print('First_Bias_Shape=', L1_bias.shape)
print('Second_Weight_Shape=', L2_weights.shape) 
print('Second_Bias_Shape=', L2_bias.shape)
print('Third_Weight_Shape=', L3_weights.shape) 
print('Third_Bias_Shape=', L3_bias.shape)


The Result is below.

Let’s compare the Keras’s modell summary.

*It’s not shown in the image above, input layer’s dimension is 4.


You can see these information is match with each other.


That’s all. Thank you!!