user3218279
user3218279

Reputation: 279

How to get hidden node representations of LSTM in keras

I've implemented a model using LSTM program in keras. I am trying to get the representations of hidden nodes of the LSTM layer. Is this the right way to get the representation (stored in activations variable) of hidden nodes?

model = Sequential()
model.add(LSTM(50, input_dim=sample_index))

activations = model.predict(testX)

model.add(Dense(no_of_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',  optimizer='adagrad', metrics=['accuracy'])
hist=model.fit(trainX, trainY, validation_split=0.15, nb_epoch=5, batch_size=20, shuffle=True, verbose=1)

Upvotes: 1

Views: 814

Answers (1)

iTS
iTS

Reputation: 448

Edit: your way to get the hidden representation is also correct. Reference: https://github.com/fchollet/keras/issues/41

After training your model, you can save your model and the weights. Like this:

from keras.models import model_from_json

json_model = yourModel.to_json()
open('yourModel.json', 'w').write(json_model)
yourModel.save_weights('yourModel.h5', overwrite=True)

And then you can visualize the weights of your LSTM layers. Like this:

from keras.models import model_from_json
import matplotlib.pyplot as plt

model = model_from_json(open('yourModel.json').read())
model.load_weights('yourModel.h5')

layers = model.layers[1]  # find the LSTM layer you want to visualize, [1] is just an example
weights, bias = layers.get_weights()
plt.matshow(weights, fignum=100, cmap=plt.cm.gray)
plt.show()

Upvotes: 2

Related Questions