TensorFlow tf.nn.rnn函数...如何使用训练结果通过RNN进行单次前向通过 [英] TensorFlow tf.nn.rnn function ... how to use the results of your training to do a single forward-pass through the RNN
问题描述
在tf.nn.rnn函数中使用初始状态"参数时,我很难过.
I'm having a tough time using the 'initial state' argument in the tf.nn.rnn function.
val, _ = tf.nn.rnn(cell1, newBatch, initial_state=stateP, dtype=tf.float32)
newBatch.shape =>(1、1、11)
stateP.shape =>(2,2,1,11)
newBatch.shape => (1, 1, 11)
stateP.shape => (2, 2, 1, 11)
通常,我已经完成了LSTM神经网络的培训,现在我想使用它的值.我该怎么做呢?我知道tf.nn.rnn()函数将返回状态...但是我不知道如何将其插入.
In general, I've gone through the training for my LSTM neural net and now I want to use the values of it. How do I do this? I know that the tf.nn.rnn() function will return state... but I don't know how to plug it in.
fyi stateP.shape =>(2,2,1,11)......也许是因为我使用了堆叠的LSTM单元格?
fyi stateP.shape => (2, 2, 1, 11) ..... maybe because I used stacked LSTM cells?
我也尝试过:
val, _ = tf.nn.dynamic_rnn(stacked_lstm, newBatch, initial_state=stateP, dtype=tf.float32)
但是出现错误"AttributeError:'NoneType'对象没有属性'op'".
我很确定正在讨论的'NoneType'对象是我给的stateP元组,但是我不确定在这里做什么.
but I get the error "AttributeError: 'NoneType' object has no attribute 'op'".
I'm pretty sure that the 'NoneType' object being talked about is the stateP tuple I gave, but I'm not sure what to do here.
我终于通过使用以下命令来运行它:
init_state = cell.zero_state(batch_size, tf.float32)
要确定确切的形状,我需要传递"initial_state"参数.在我的情况下,它是一个由4个张量组成的TUPLE,每个张量具有(1,11)的形状.我是这样的:
To determine the exact shape I need to pass into the 'initial_state' argument. In my case, it was a TUPLE of 4 tensors, each with the shape of (1, 11). I made it like this:
stateP0 = tf.convert_to_tensor(stateP[0][0])
stateP1 = tf.convert_to_tensor(stateP[0][1])
stateP2 = tf.convert_to_tensor(stateP[1][0])
stateP3 = tf.convert_to_tensor(stateP[1][1])
newStateP = stateP0, stateP1, stateP2, stateP3
好的!现在tf.dynamic_rnn()函数正在运行,但是每次运行它都会给我带来不同的结果....那么传递初始状态有什么意义呢?我想使用经过训练的状态来查找...,但我不想改变它.我想实际使用我的训练结果!
Alright! Now the tf.dynamic_rnn() function is working, but it's giving me different results every time I run it.... so what's the point of passing in the initial state? I want to use the state I trained to find... and I don't want it to change. I want to actually use the results of my training!
推荐答案
您可能正在使用不推荐使用(或即将使用)的行为.在您的情况下,stateP
表示c
(单元状态)和h
(从展开的最后一步输出的lstm)的串联.因此,您需要沿维度1切片状态以获取实际状态.
You are probably using the deprecated (or soon to be) behavior. stateP
in your case represents the concatenation of c
(cell state) and h
(output of lstm from the final step of unrolling). So you need to slice the state along dimension 1 to get the actual state.
或者,您可以使用state_is_tuple=True
初始化LSTM单元,这是我建议的方式,这样您可以通过为状态stateP[0]
编制索引来轻松获得最终状态(如果您想修改它).或者,您可以将状态元组直接传递给rnn(或dynamic_rnn).
Or, you can initialize your LSTM cell with state_is_tuple=True
, which I would recommend, so that you could easily get the final state (if you want to tinker with it) by indexing the state stateP[0]
. Or you could just pass the state tuple directly to rnn (or dynamic_rnn).
除了您没有提供初始化代码之外,我什么也不能说.所以我会猜测.
I cant say anything beyond that because you have not provided your initialization code. So I would be guessing.
如果您仍然遇到问题,您可以编辑问题以提供更多详细信息,我将编辑答案.
You can edit your question to provide more details if you still face problems and I would edit the answer.
这篇关于TensorFlow tf.nn.rnn函数...如何使用训练结果通过RNN进行单次前向通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!