Python 在递归循环期间分配给TensorFlow变量

Python 在递归循环期间分配给TensorFlow变量,python,tensorflow,Python,Tensorflow,在Tensorflow 1.9中,我想创建一个网络,然后递归地将网络的输出(预测)反馈到网络的输入中。在这个循环中,我想将网络做出的预测存储在一个列表中 以下是我的尝试: # Define the number of steps over which to loop the network num_steps = 5 # Define the network weights weights_1 = np.random.uniform(0, 1, [1, 10])

在Tensorflow 1.9中,我想创建一个网络,然后递归地将网络的输出(预测)反馈到网络的输入中。在这个循环中,我想将网络做出的预测存储在一个列表中

以下是我的尝试:

    # Define the number of steps over which to loop the network
    num_steps = 5

    # Define the network weights
    weights_1 = np.random.uniform(0, 1, [1, 10]).astype(np.float32)
    weights_2 = np.random.uniform(0, 1, [10, 1]).astype(np.float32)

    # Create a variable to store the predictions, one for each loop
    predictions = tf.Variable(np.zeros([num_steps, 1]), dtype=np.float32)

    # Define the initial prediction to feed into the loop
    initial_prediction = np.array([[0.1]], dtype=np.float32)
    x = initial_prediction

    # Loop through the predictions
    for step_num in range(num_steps):
        x = tf.matmul(x, weights_1)
        x = tf.matmul(x, weights_2)
        predictions[step_num-1].assign(x)

    # Define the final prediction
    final_prediction = x

    # Start a session
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())

    # Make the predictions
    last_pred, all_preds = sess.run([final_prediction, predictions])
    print(last_pred)
    print(all_preds)
这个打印出来:

[[48.8769]]

[[0.]
 [0.]
 [0.]
 [0.]
 [0.]]
因此,虽然
final_prediction
的值看起来是正确的,但是
predictions
的值并不是我所期望的。尽管有行
predictions[step_num-1].assign(x)
,但似乎从未实际分配到
predictions


请有人给我解释一下为什么这不起作用,我应该怎么做?谢谢

之所以发生这种情况,是因为
assign
ist与其他任何操作一样,只是一个TF op,因此只有在需要时才会执行。由于
final_prediction
路径上的任何内容都不依赖于赋值运算,并且
predictions
只是一个变量,因此永远不会执行赋值

我认为最直接的解决办法是更换线路

predictions[step_num-1].assign(x)

这是因为
assign
还返回它正在分配的值。现在,要计算
final_prediction
TF,实际上需要“通过”
assign
op,以便执行赋值

另一种选择是使用
tf.control\u dependencies
,这是一种在计算其他操作时“强制”tf计算特定操作的方法。然而,在这种情况下,它可能有点令人讨厌,因为我们想要强制的op(
赋值
)取决于循环中正在计算的值,并且我不确定TF在这种情况下的填充顺序。以下方面应起作用:

for step_num in range(num_steps):
    x = tf.matmul(x, weights_1)
    x = tf.matmul(x, weights_2)
    with tf.control_dependencies([predictions[step_num-1].assign(x)]):
        x = tf.identity(x)
我们使用
tf.identity
作为noop,只是为了用
control\u依赖项来包装一些东西。我认为这是两者之间更灵活的选择。然而,它附带了一些在中讨论的警告

for step_num in range(num_steps):
    x = tf.matmul(x, weights_1)
    x = tf.matmul(x, weights_2)
    with tf.control_dependencies([predictions[step_num-1].assign(x)]):
        x = tf.identity(x)