2017-04-06 64 views
1

如果我有两个神经网络A和B,并且我使用网络A的输出来馈送网络B的输入(占位符)并且我使用优化器来最小化网络B的损失,那么网络A的参数是通过反向传播更新的?关于张量逆向传播

回答

1

是的,如果“feed”在TensorFlow中完成;不,如果你手动做。

具体而言,如果您评估A,然后用手动输入的那些输出(例如,作为饲料字典)训练B,则A不会改变,因为它不涉及训练阶段。

如果将B网络的输入设置为A中op的输出(例如,而不是tf.Placeholder),则可以训练组合网络,它将更新A的参数。但是,在这种情况下,您实际上只是在训练一个联合网络“AB”,而不是两个单独的网络。

一个具体的例子:

import numpy as np 
import tensorflow as tf 

# A network 
A_input = tf.placeholder(tf.float32, [None,100]) 
A_weights = tf.Variable(tf.random_normal([100,10])) 
A_output = tf.matmul(A_input,A_weights) 

# B network 
B_input = tf.placeholder(tf.float32, [None,10]) 
B_weights = tf.Variable(tf.random_normal([10,5])) 
B_output = tf.matmul(B_input,B_weights) 

# AB network 
AB_input = A_output 
AB_weights = tf.Variable(tf.random_normal([10,5])) 
AB_output = tf.matmul(AB_input,AB_weights) 

test_inputs = np.random.rand(17,100) 
sess = tf.Session() 
sess.run(tf.global_variables_initializer()) 
A_out = sess.run(A_output, feed_dict={A_input: test_inputs}) 
print 'A output shape:',A_out.shape 
B_out = sess.run(B_output, feed_dict={B_input: A_out}) 
print 'B output shape:',B_out.shape 

AB_out = sess.run(AB_output, feed_dict={A_input: test_inputs}) 
print 'AB output shape:',AB_out.shape 

在第一种情况中,我们通过使用feed_dict已经与从网络A的输出馈网络B。这是在tensorflow中评估网络A,将结果拉回到python中,然后评估张量流中的网络B.如果尝试以这种方式训练网络B,则只会更新网络B中的参数。

在第二种情况下,我们通过将网络A的输出直接连接到网络AB的“B”部分到网络AB的输入。评估网络AB永远不会将网络A的中间结果恢复为python,因此如果以这种方式训练网络AB,则可以更新组合网络的参数。 (注意:你的训练输入被馈送到网络AB的A_input而不是中间张量AB_input)

+0

占位符可以被馈送,所以我认为A的参数不能被更新,因为A的输出只是提供值B网络的输入。窦你觉得呢?我不明白,你可以给网络喂食。 – Gauss

+0

我认为你的理解是正确的。我添加了一个可能有所帮助的具体示例。通过“向网络馈送op”,我的意思是将一个张量的输出连接到另一个张量的输出,完全绕过馈送字典(示例中的行“AB_input = A_output”)。 – rdadolf

+0

是的,完美的答案。 – Gauss