图形模式下的GRU/RNN状态与渴望执行模式



我有同样的代码,先是在热切执行模式下写的,然后在图形模式下写。现在,我不太明白为什么GRU状态没有保留在图形模式中,而它在渴望模式中工作得很好。

这是热切的模式代码:

import tensorflow as tf 
import xxhash
import numpy as np 
tf.enable_eager_execution()
rnn_units = 1024 
def hash_code(arr): 
return xxhash.xxh64(arr).hexdigest()
model = tf.keras.Sequential([tf.keras.layers.GRU(rnn_units,
return_sequences=True,
stateful=True,
recurrent_initializer='glorot_uniform', batch_input_shape=[1, None, 256])])
lstm_wt = np.load('lstm_wt.npy', allow_pickle=True) # fixed weights for comparison 
lstm_re_wt = np.load('lstm_re_wt.npy', allow_pickle=True)
lstm_bias = np.load('lstm_bias.npy', allow_pickle=True)
model.layers[0].set_weights([lstm_wt, lstm_re_wt, lstm_bias])
op_embed = np.load('op_embed.npy', allow_pickle=True) # fixed input 
op_lstm = model(op_embed)
print(hash_code(op_lstm.numpy()))
op_lstm = model(op_embed)
print(hash_code(op_lstm.numpy()))
model.layers[0].reset_states() # now reset the state, you'll get back the initial output. 
op_lstm = model(op_embed)
print(hash_code(op_lstm.numpy()))

此代码的输出:

d092fdb4739588a3
cdfdf8b8e292c6e8
d092fdb4739588a3

现在,图形模型代码:

import tensorflow as tf 
import xxhas
import numpy as np 
# checking lstm 
op_embed = np.load('op_embed.npy', allow_pickle=True)
# load op_embed, lstm weights 
lstm_wt = np.load('lstm_wt.npy', allow_pickle=True)
lstm_re_wt = np.load('lstm_re_wt.npy', allow_pickle=True)
lstm_bias = np.load('lstm_bias.npy', allow_pickle=True)
rnn_units = 1024 
layers = tf.keras.layers.GRU(rnn_units,
return_sequences=True,
stateful=True,
recurrent_initializer='glorot_uniform')
x_placeholder = tf.placeholder(shape=op_embed.shape, dtype=tf.float32)
op_lstm = layers(x_placeholder)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
layers.set_weights([lstm_wt, lstm_re_wt, lstm_bias])
tf.assign(layers.weights[0],lstm_wt ).eval(sess)
tf.assign(layers.weights[1], lstm_re_wt).eval(sess)
tf.assign(layers.weights[2], lstm_bias).eval(sess)
print('keras op hash',xxhash.xxh64(sess.run(op_lstm, feed_dict={x_placeholder:op_embed})).hexdigest())
print('keras op hash',xxhash.xxh64(sess.run(op_lstm, feed_dict={x_placeholder:op_embed})).hexdigest())

输出:

keras op hash d092fdb4739588a3
keras op hash d092fdb4739588a3

关于如何在图形模式下修复这种模糊性并保留状态,有什么见解吗?以前也有人问过类似的问题,但没有得到回答。渴望模式下的状态与非渴望模式

在此处指定解决方案(答案部分(,即使它存在于问题中提供的链接中,也是为了社区的利益。

默认情况下,Recurrent Neural Network(RNNGRULSTM(在Non-Eager-Mode/Graph-Mode中执行时会丢失其state

如果我们想保留state,我们需要在RNN调用期间传递Initial State,如下所示:

current_state = np.zeros((1,1))
state_placeholder = tf.placeholder(tf.float32, shape=[1, 1])
output, state = rnn(x, initial_state=state_placeholder)

然后,在执行Output时,除了feed_dictInput之外,我们还需要传递State

所以,代码,

print('keras op hash',xxhash.xxh64(sess.run(op_lstm, feed_dict={x_placeholder:op_embed})).hexdigest())

可以用代替

for _ in range(No_Of_TimeSteps):
op_val, state_val = sess.run([op_lstm, state], feed_dict={x_placeholder:op_embed})).hexdigest(),
state_placeholder: current_state.astype(np.float32)})
current_state = state_val
print('keras op hash',xxhash.xxh64(op_val))

希望这能有所帮助。快乐学习!

最新更新