1
我已经定义了两类模型,x
和y
。如何使用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
的输入而没有任何错误?提前致谢!
感谢您的回答!但我很抱歉,我仍然有点困惑。我应该推迟到'session.run'的调用,以在哪个'step'功能之外? 你能告诉我如何完全分离图吗?非常感谢!!! – user5779223
在一个地方建立图形(即调用大多数tf函数),返回想要运行的张量,并在另一个地方调用session.run。要查看构建代码的好方法,请尝试查看tf.estimator.Estimator –