[AI] Understanding Weights and Biases in a Keras LSTM

AI
higashi
higashi

Hi, I’m Higashi.

 

In this article, I’ll explain what the weights and biases in a TensorFlow Keras LSTM do.

 

We’ll look at how Keras stores those parameters and how they’re used in the calculation.

 

Let’s get started!

 

Sponsored Links

Where the weights and biases are used

Let’s start by looking at where each parameter appears in the calculation.

 

The diagram below shows the structure of the model.

illustration of LSTM model structure

The red labels mark the weights and biases.

There are five parameter arrays in this example:

(W, U, b, Wout, bout)

 

Sponsored Links

Inspecting the model parameters

Now let’s look at the actual arrays.

 

In TensorFlow Keras, call model.get_weights() to retrieve them.

Here’s what that method returns for this model.

Display result of LSTM model parameters

The result contains the five arrays shown in the diagram.

 

The arrays have the following shapes:

Display result of the parameters shape

This model has 3 input features, 5 hidden units, and 1 output.

 

Those dimensions show up in the array shapes.

 

But where does 20 come from?

 

 

Sponsored Links

Why the LSTM parameter shapes include a factor of four

 

Let xu, hu, and yu be the input dimension, number of hidden units, and output dimension. The arrays have these shapes:

W_shape=(xu, hu×4)

U_shape=(hu, hu×4)

b_shape=(hu×4,)

Wout_shape=(hu, yu)

bout_shape=(yu,)

 

What does the ×4 in W, U, and b represent?

 

Here’s the model diagram again.

illustration of LSTM model structure

As the diagram shows, W, U, and b are used to compute four vectors: it, ft, ot, and ctt.

 

Each vector has one entry per hidden unit. With five hidden units, that gives 4 × 5 = 20 entries.

This is how W is arranged.

illustration of inside parameter

U and b are grouped the same way.

 

Sponsored Links

Calculating the LSTM output from its parameters

Let’s use the parameters we retrieved to calculate the LSTM output.

 

Trained model: model

Input dimension: xdim=3

Hidden units: hl_unit=5

Time steps: look_back=4

 

In the code below, the sample input Xt has shape (look_back, xdim).

#definition of each unit
xdim=3
hl_unit=5
look_back=4
#get weights and bias
W,U,b,Wout,bout=model.get_weights()
Wi=W[:,0:hl_unit]
Wf=W[:,hl_unit:2*hl_unit]
Wc=W[:,2*hl_unit:3*hl_unit]
Wo=W[:,3*hl_unit:]
Ui=U[:,0:hl_unit]
Uf=U[:,hl_unit:2*hl_unit]
Uc=U[:,2*hl_unit:3*hl_unit]
Uo=U[:,3*hl_unit:]
bi=b[0:hl_unit]
bf=b[hl_unit:2*hl_unit]
bc=b[2*hl_unit:3*hl_unit]
bo=b[3*hl_unit:]
#difinition of sigmoid function
def sigmoid(x):
    return(1.0/(1.0+np.exp(-x)))

#LSTM calculation
c=np.zeros(hl_unit)
h=np.zeros(hl_unit)
for i in range(look_back):
    x=Xt[i]
    it=sigmoid(np.dot(x,Wi)+np.dot(h,Ui)+bi)
    ft=sigmoid(np.dot(x,Wf)+np.dot(h,Uf)+bf)
    ctt=np.tanh(np.dot(x,Wc)+np.dot(h,Uc)+bc)
    ot=sigmoid(np.dot(x,Wo)+np.dot(h,Uo)+bo)
    c=ft*c+it*ctt
    h=np.tanh(c)*ot
Yt=np.dot(h,Wout)+bout

 

The calculation follows these steps:

1. Get the weights and biases.

2. Split the LSTM weights and biases into four groups.

3. Use those groups to calculate the LSTM output.

 

The result, Yt, should match the output of model.predict(Xt) in TensorFlow Keras.

Try running both calculations and comparing the results.

 

Thanks for reading!

コメント