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!!

## 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”.

## 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.

## 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.

bias=layer.get_weights()[1]

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

bias=model.layers[i].get_weights()[1]

## 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]
L1_weights=layer1.get_weights()[0]
L1_bias=layer1.get_weights()[1]
#Second Layer's Process
layer2 = model.layers[1]
L2_weights=layer2.get_weights()[0]
L2_bias=layer2.get_weights()[1]
#Third Layer's Process
layer3 = model.layers[2]
L3_weights=layer3.get_weights()[0]
L3_bias=layer3.get_weights()[1]
```

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!!

## コメント