【发布时间】:2016-12-28 08:46:33
【问题描述】:
我正在尝试使用小批量在 Tensorflow 中训练 LSTM,但在训练完成后,我想通过一次提交一个示例来使用该模型。我可以在 Tensorflow 中设置图表来训练我的 LSTM 网络,但之后我无法以我想要的方式使用训练后的结果。
设置代码如下所示:
#Build the LSTM model.
cellRaw = rnn_cell.BasicLSTMCell(LAYER_SIZE)
cellRaw = rnn_cell.MultiRNNCell([cellRaw] * NUM_LAYERS)
cell = rnn_cell.DropoutWrapper(cellRaw, output_keep_prob = 0.25)
input_data = tf.placeholder(dtype=tf.float32, shape=[SEQ_LENGTH, None, 3])
target_data = tf.placeholder(dtype=tf.float32, shape=[SEQ_LENGTH, None])
initial_state = cell.zero_state(batch_size=BATCH_SIZE, dtype=tf.float32)
with tf.variable_scope('rnnlm'):
output_w = tf.get_variable("output_w", [LAYER_SIZE, 6])
output_b = tf.get_variable("output_b", [6])
outputs, final_state = seq2seq.rnn_decoder(input_list, initial_state, cell, loop_function=None, scope='rnnlm')
output = tf.reshape(tf.concat(1, outputs), [-1, LAYER_SIZE])
output = tf.nn.xw_plus_b(output, output_w, output_b)
...注意两个占位符,input_data 和 target_data。我没有打扰包括优化器设置。训练完成并结束训练后,我想设置一个新的会话,使用经过训练的 LSTM 网络,其输入由完全不同的占位符提供,例如:
with tf.Session() as sess:
with tf.variable_scope("simulation", reuse=None):
cellSim = cellRaw
input_data_sim = tf.placeholder(dtype=tf.float32, shape=[1, 1, 3])
initial_state_sim = cell.zero_state(batch_size=1, dtype=tf.float32)
input_list_sim = tf.unpack(input_data_sim)
outputsSim, final_state_sim = seq2seq.rnn_decoder(input_list_sim, initial_state_sim, cellSim, loop_function=None, scope='rnnlm')
outputSim = tf.reshape(tf.concat(1, outputsSim), [-1, LAYER_SIZE])
with tf.variable_scope('rnnlm'):
output_w = tf.get_variable("output_w", [LAYER_SIZE, nOut])
output_b = tf.get_variable("output_b", [nOut])
outputSim = tf.nn.xw_plus_b(outputSim, output_w, output_b)
第二部分返回以下错误:
tensorflow.python.framework.errors.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
...大概是因为我正在使用的图表仍然将旧的训练占位符附加到经过训练的 LSTM 节点上。什么是“提取”经过训练的 LSTM 并将其放入具有不同输入风格的新的不同图表的正确方法? Tensorflow 的变量范围功能似乎解决了这样的问题,但是示例in the documentation 都在讨论使用变量范围作为管理变量名称的一种方式,以便同一段代码将在同一图中生成类似的子图。 “重用”功能似乎接近我想要的,但我发现上面链接的 Tensorflow 文档根本不清楚它的作用。不能给单元格本身命名(换句话说,
cellRaw = rnn_cell.MultiRNNCell([cellRaw] * NUM_LAYERS, name="multicell")
无效),虽然我可以为 seq2seq.rnn_decoder() 命名,但如果我未更改地使用该节点,我可能无法删除 rnn_cell.DropoutWrapper()。
问题:
将经过训练的 LSTM 权重从一张图移动到另一张图的正确方法是什么?
说启动一个新会话“释放资源”,但不会擦除内存中的图形是否正确?
在我看来,“重用”功能允许 Tensorflow 在当前变量范围之外搜索具有相同名称(存在于不同范围内)的变量,并在当前范围内使用它们。它是否正确?如果是,链接到该变量的非当前范围内的所有图边会发生什么情况?如果不是,如果您尝试在两个不同的范围内使用相同的变量名,为什么 Tensorflow 会抛出错误?在两个不同的范围内定义两个具有相同名称的变量似乎是完全合理的,例如conv1/sum1 和 conv2/sum1。
在我的代码中,我在一个新的范围内工作,但是如果没有数据从初始默认范围输入到占位符中,该图表将无法运行。出于某种原因,默认范围是否始终“在范围内”?
如果图边可以跨越不同的范围,并且不同范围内的名称不能共享,除非它们引用完全相同的节点,那么这似乎违背了首先具有不同范围的目的。我在这里误会了什么?
谢谢!
【问题讨论】:
标签: python tensorflow lstm