I want to get the gate values for the cell state, the reset gate, the update gate of a trained GRU network. According to this issue I'm able to get weights of kernel, recurrent_kernel and bias for each gate:
from keras.preprocessing import sequence
from keras.models import Sequential, load_model
from keras.layers import Dense, Embedding, Dropout
from keras.layers import GRU
from keras.datasets import imdb
import keras
import tensorflow as tf
import random as rn
import numpy as np
import math
max_features = 20000
# cut texts after this number of words (among top max_features most common words)
maxlen = 80
batch_size = 128
print('Loading data...')
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('Build model...')
model = Sequential()
model.add(Embedding(max_features, 100))
model.add(GRU(128))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
optimizer = keras.optimizers.Adam(lr=1e-5)
model.compile(loss='binary_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
import keras.backend as K
print('Train...')
epochs = 50
for i in range(epochs):
print('Epoch', i, '/', epochs)
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=1,
validation_data=(x_test, y_test))
#callbacks=[lrate])
for layer in model.layers:
if 'GRU' in str(layer):
#print('states[0] = {}'.format(K.get_value(layer.states[0])))
#print('states[1] = {}'.format(K.get_value(layer.states[1])))
weights = layer.get_weights()
for e in zip(layer.trainable_weights, layer.get_weights()):
print('Param\n%s:\n%s' % (e[0], e[1]))
I want to save the values of each gate to visualize each of them. I could not find any way to find the values of each gate. I am planning to recalculate the gates, however I can't access the states using layer.states[0] or layer.states[1]. Is there an easy way to find the gates values at each epoch? Or how can find states values in a GRU cell?