Python 如何在Tensorflow中使用tf.nn.raw\n函数?
我试图实现基于LSTM的网络,在隐状态计算之后,我们还在每个时间步应用线性+S形变换。我发现and DESCRIPEPython 如何在Tensorflow中使用tf.nn.raw\n函数?,python,tensorflow,machine-learning,Python,Tensorflow,Machine Learning,我试图实现基于LSTM的网络,在隐状态计算之后,我们还在每个时间步应用线性+S形变换。我发现and DESCRIPEtf.nn.raw\n函数适合此任务,但我很难理解为什么它在我的特定情况下不起作用 输入说明 因此,让我们对LSTM的输入是一个小批量的[num\u steps x batch\u size x size],具体地说是[5,32,100]。让LSTM有200个隐藏单位。然后,LSTM的输出是[5,32,200]张量,我们可以在以后用于损耗计算。我假设输入的[5,32,100]张量首
tf.nn.raw\n
函数适合此任务,但我很难理解为什么它在我的特定情况下不起作用
输入说明
因此,让我们对LSTM的输入是一个小批量的[num\u steps x batch\u size x size]
,具体地说是[5,32,100]
。让LSTM有200个隐藏单位。然后,LSTM的输出是[5,32,200]
张量,我们可以在以后用于损耗计算。我假设输入的[5,32,100]
张量首先被分解成一个[32,100]
张量数组,然后如果我们在Tensorflow中使用tf.nn.dynamic\u rnn
和time\u major=True
,则被重新堆叠:
tf.nn.dynamic_rnn(LSTM)
LSTM t=0 LSTM t=1 LSTM t=2 LSTM t=3 LSTM t=4
[5, 32, 100] --> [[32, 100], [32, 100], [32, 100], [32, 100], [32, 100]] --> [5, 32, 200]
隐状态模型
此外,在每个LSTM单元之后,我需要执行线性+S形变换,以将每个[32200]
张量压缩为[32,1]
。我们的tf.nn.dynamic\u rnn
不适用于此,因为它只接受单元格。我们需要使用tf.nn.raw\n
API。因此,以下是我的尝试:
def _get_raw_rnn_graph(self, inputs):
time = tf.constant(0, dtype=tf.int32)
_inputs_ta = tf.TensorArray(dtype=tf.float32, size=5)
# our [5, 32, 100] tensor becomes [[32, 100], [32, 100], ...]
_inputs_ta = _inputs_ta.unstack(inputs)
# create simple LSTM cell
cell = tf.contrib.rnn.LSTMCell(config.hidden_size)
# create loop_fn for raw_rnn
def loop_fn(time, cell_output, cell_state, loop_state):
emit_output = cell_output # == None if time = 0
if cell_output is None: # time = 0
next_cell_state = cell.zero_state(32, tf.float32)
self._initial_state = next_cell_state
else:
next_cell_state = cell_state
elements_finished = (time >= 32)
finished = tf.reduce_all(elements_finished)
next_input = tf.cond(finished,
lambda: tf.zeros([32, config.input_size], dtype=tf.float32),
lambda: _inputs_ta.read(time))
# apply linear + sig transform here
next_input = self._linear_transform(next_input, activation=tf.sigmoid)
next_loop_state = None
return (elements_finished, next_input, next_cell_state, emit_output, next_loop_state)
outputs_ta, final_state, _ = tf.nn.raw_rnn(cell, loop_fn)
outputs = outputs_ta.stack()
return outputs, final_state
不幸的是,这不起作用。loop\u fn
只迭代两次,而不是我预期的num\u步数
次,其输出是Tensor(“Train/Model/TensorArrayStack/TensorArrayGatherV3:0”,shape=(?,32200),dtype=float32)
而不是我们预期的[5,32,1]
。我错过了什么