Karan Bhatia
Karan Bhatia

Reputation: 33

How can we get weights after training RNN in Keras?

I have trained an RNN in Keras. Now, I want to get the values of the trained weights:

model = Sequential() 
model.add(SimpleRNN(27, return_sequences=True , input_shape=(None, 27), activation = 'softmax'))<br>
model.compile(loss='categorical_crossentropy',
          optimizer='rmsprop',
          metrics=['accuracy'])
model.get_weights()

This gives me 2 arrays of shape (27,27) and 1 array of shape (27,1). I am not getting the meaning of these arrays. Also, I should get 2 more array of shape (27,27) and (27,1) that will calculate the hidden state 'a' activation. How can I get these weights?

Upvotes: 1

Views: 899

Answers (1)

Maxim
Maxim

Reputation: 53768

The arrays returned by model.get_weights() directly correspond to the weights used by SimpleRNNCell. They include:

  • The kernel matrix of size (input_shape[-1], units). In your case, input_shape=(None, 27) and units=27, so it's (27, 27). The kernel gets multiplied by the input.
  • The recurrent_kernel matrix of size (units, units), which also happens to be (27, 27). This matrix gets multiplied by the previous state.
  • The bias array of shape (units,) == (27,).

These arrays correspond to the standard formula:

# W = kernel
# U = recurrent_kernel
# B = bias
output = new_state = act(W * input + U * state + B)

Note that keras implementation uses a single bias vector, so all in all there are exactly three arrays.

Upvotes: 1

Related Questions