Tensorflow 1.0:从恢复的 RNN 中检索隐藏状态
Tensorflow 1.0 : Retreive hidden state from restored RNN
我想恢复 RNN 并获取隐藏状态。
我做了类似的事情来保存 RNN:
loc="path/to/save/rnn"
with tf.variable_scope("lstm") as scope:
outputs, state = tf.nn.dynamic_rnn(..)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
save_path = saver.save(sess,loc)
现在我想取回state
。
graph = tf.Graph()
sess = tf.Session(graph=graph)
with graph.as_default():
saver = tf.train.import_meta_graph(loc + '.meta', clear_devices=True)
saver.restore(sess, loc)
state= ...
您可以将 state
张量添加到图 collection, which is basically a key value store to track tensors, using tf.add_to_collection and retrieve it later using tf.get_collection 中。例如:
loc="path/to/save/rnn"
with tf.variable_scope("lstm") as scope:
outputs, state = tf.nn.dynamic_rnn(..)
tf.add_to_collection('state', state)
graph = tf.Graph()
with graph.as_default():
saver = tf.train.import_meta_graph(loc + '.meta', clear_devices=True)
state = tf.get_collection('state')[0] # Note: tf.get_collection returns a list.
我想恢复 RNN 并获取隐藏状态。
我做了类似的事情来保存 RNN:
loc="path/to/save/rnn"
with tf.variable_scope("lstm") as scope:
outputs, state = tf.nn.dynamic_rnn(..)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver()
save_path = saver.save(sess,loc)
现在我想取回state
。
graph = tf.Graph()
sess = tf.Session(graph=graph)
with graph.as_default():
saver = tf.train.import_meta_graph(loc + '.meta', clear_devices=True)
saver.restore(sess, loc)
state= ...
您可以将 state
张量添加到图 collection, which is basically a key value store to track tensors, using tf.add_to_collection and retrieve it later using tf.get_collection 中。例如:
loc="path/to/save/rnn"
with tf.variable_scope("lstm") as scope:
outputs, state = tf.nn.dynamic_rnn(..)
tf.add_to_collection('state', state)
graph = tf.Graph()
with graph.as_default():
saver = tf.train.import_meta_graph(loc + '.meta', clear_devices=True)
state = tf.get_collection('state')[0] # Note: tf.get_collection returns a list.