Tensorflow:如何从 rnn_cell.BasicLSTM & 获取所有变量rnn_cell.MultiRNNCell [英] Tensorflow: How to get all variables from rnn_cell.BasicLSTM & rnn_cell.MultiRNNCell
问题描述
我有一个设置,我需要在使用 tf.initialize_all_variables()
的主要初始化之后初始化 LSTM.IE.我想调用 tf.initialize_variables([var_list])
I have a setup where I need to initialize an LSTM after the main initialization which uses tf.initialize_all_variables()
. I.e. I want to call tf.initialize_variables([var_list])
有没有办法收集两者的所有内部可训练变量:
Is there way to collect all the internal trainable variables for both:
- rnn_cell.BasicLSTM
- rnn_cell.MultiRNNCell
这样我就可以只初始化这些参数?
so that I can initialize JUST these parameters?
我想要这个的主要原因是因为我不想重新初始化一些之前训练过的值.
The main reason I want this is because I do not want to re-initialize some trained values from earlier.
推荐答案
解决问题的最简单方法是使用变量作用域.范围内变量的名称将以其名称作为前缀.这是一个简短的片段:
The easiest way to solve your problem is to use variable scope. The names of the variables within a scope will be prefixed with its name. Here is a short snippet:
cell = rnn_cell.BasicLSTMCell(num_nodes)
with tf.variable_scope("LSTM") as vs:
# Execute the LSTM cell here in any way, for example:
for i in range(num_steps):
output[i], state = cell(input_data[i], state)
# Retrieve just the LSTM variables.
lstm_variables = [v for v in tf.all_variables()
if v.name.startswith(vs.name)]
# [..]
# Initialize the LSTM variables.
tf.initialize_variables(lstm_variables)
它与 MultiRNNCell
的工作方式相同.
It would work the same way with MultiRNNCell
.
将 tf.trainable_variables
更改为 tf.all_variables()
这篇关于Tensorflow:如何从 rnn_cell.BasicLSTM & 获取所有变量rnn_cell.MultiRNNCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!