Tensorflow:保存并恢复张量的输出

时间:2017-01-16 07:46:38

标签: tensorflow

我想使用tensorflow来实现这样的目标。

enter image description here

我只能找到有关保存和恢复变量(权重)的文档。但是,和#2-2一样,我想利用隐藏层(张量)的输出作为另一个模型的输入。可以这样做吗?

1 个答案:

答案 0 :(得分:1)

据我所知,创建它们后不可能链接不同的计算图,但是你有几个选择。

选项2:创建一个大图并使用control flow op

output_layer, placeholder = build_my_model()
something = tf.where(output_layer < 0, do_something_1(), do_something_2())

上面的所有函数调用都应返回tensorflow操作。

选项2:创建两个图形并在python

中执行条件语句
# Build the first graph
with tf.Graph().as_default() as graph:
    output_layer, placeholder = build_my_model()

# Build the second two graphs
with tf.Graph().as_default() as graph_1:
    something_1 = do_something_1()
with tf.Graph().as_default() as graph_2:
    something_2 = do_something_2()

因此,您最终会得到三个不同的会话,您需要将第一个会话的输出提供给其他两个会话中的一个

# Get the output
_output_layer = sess.run(output_layer, {placeholder: ...})
if _output_layer < 0:
    something = sess1.run(something_1, {...})
else:
    something = sess2.run(something_2, {...})

正如您所看到的,如果您可以使用控制流操作,您的代码将变得非常简单。在一个图形中包含所有内容的另一个优点是整个图形是可微分的,您可以根据后期的损失来训练模型第一阶段的参数。