2017-05-04 185 views
1

我已经定义了两类模型,xy如何使用Tensorflow r-1.0将一个模型的输出作为另一个模型的输入?

class x(): 
    def __init__(self, x_inp1, x_inp2): 
     # do sth... 

    def step(self, session, encoder_inputs): 
     input_feed = {} 
     for l in range(encoder_size): 
      input_feed[self.encoder_inputs[l].name] = encoder_inputs[l] 
     ... 
     output_feed = [x_output] 
     return session.run(x_output) 

class y(): 
    def __init__(self, y_inp1, y_inp2): 
     # do sth... 

    def step(self, encoder_inputs): 
     input_feed = {} 
     for l in range(encoder_size): 
      input_feed[self.encoder_inputs[l].name] = encoder_inputs[l] 
     ... 

它们有很相似的功能。然后我定义另一个班级将其分组。

class gp(): 
    def __init__(self, x_inp1, x_inp2, y_inp1, y_inp2): 
     with tf.variable_scope('x'): 
       self.x_model = x(x_inp1, x_inp2) 
     with tf.variable_scope('y'): 
       self.y_model = y(y_inp1, y_inp2) 
    def step(self, session, encoder_inputs): 
     x_output = self.x_model.step(session, encoder_inputs) 
     y_output = self.y_model.step(session, x_output) 
     ... 

请注意到y_model需要的x_model输出作为输入。我在main功能运行gp()

with tf.Session() as sess: 
    gp_m = gp(x_inp1, x_inp2, y_inp1, y_inp2) 
    gp_m.step(sess, x_inp1, x_inp2, y_inp1, y_inp2) 

和运行后x_output = self.x_model.step(encoder_inputs)并开始做y_output = self.y_model.step(x_output),我得到了这样的错误:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'x/encoder0' with dtype int32 
[[Node: x/encoder0 = Placeholder[dtype=DT_INT32, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] 

请注意此错误指向x_model连它的一步功能已经完成。我想知道如何将x_model的输出作为y_model的输入而没有任何错误?提前致谢!

回答

0

您应该将呼叫推迟到session.run以超出step功能。这里的问题是试图运行Y触发器X,因为它们连接在图中。

相反,它可能更好地完全分离您的程序的图形构建和图形运行阶段,因此您知道什么时候提供占位符。

+0

感谢您的回答!但我很抱歉,我仍然有点困惑。我应该推迟到'session.run'的调用,以在哪个'step'功能之外? 你能告诉我如何完全分离图吗?非常感谢!!! – user5779223

+0

在一个地方建立图形(即调用大多数tf函数),返回想要运行的张量,并在另一个地方调用session.run。要查看构建代码的好方法,请尝试查看tf.estimator.Estimator –

相关问题