Skip to main content
 首页 » 编程设计

python之Tensorflow 中的生成序列

2025年02月15日30zhujiabin

我使用下面的代码作为生成长度序列的方法 num_steps 给定 starting_point 和 starting_state 使用一个实例RNNCell,例如 cell=GRUCell(number_of_neurons)。

outputs = [starting_point] 
state = starting_state 
for time_step in range(num_steps): 
    if time_step > 0: tf.get_variable_scope().reuse_variables() 
    (cell_output, state) = cell(outputs[time_step], state) 
    outputs.append(cell_output) 

但这对于我的用例来说既慢又麻烦,其中 num_steps = 1000。即使实例化图表也需要很长时间。

这个功能是否存在于 Tensroflow 的某处,而我只是错过了它?

请注意,我要查找的内容与 tf.contrib.rnn.static_rnn 的行为相似但不同。 documentation将此函数的行为总结为简单地将 RNN 应用于序列中的每个时间步长:

state = cell.zero_state(...) 
  outputs = [] 
  for input_ in inputs: 
    output, state = cell(input_, state) 
    outputs.append(output) 
  return (outputs, state) 

但就我而言,我想将一个步骤的输出作为下一步的输入。

请您参考如下方法:

在 tensorflow 每晚构建中,请参阅 tf.contrin.seq2seq 以获取动态解码器对象。您可以使用预定的采样助手来执行您想要的操作。或者使用 tf.nn.dynamic_rnn 并将所有零作为输入。 lstm h 状态也是 lstm 输出,因此您基本上可以获得所需的相同行为。