有条件地执行Tensorflow图的一部分

时间:2019-12-11 09:34:09

标签: python tensorflow machine-learning deep-learning

我有以下情况。我有深度神经网络的一部分,在训练过程中它会像往常一样在前进和后退过程中获取其输入并执行其对应的部分。但是在测试阶段,我想以不同的输入重用此特定部分,而与到目前为止的所有先前网络无关。我提出了以下想法。假定计算图的条件部分嵌入在函数F中。我创建了一个占位符,在训练阶段使用F之前,我在其中分配了上次执行的输出:

    prev_network_output = build_prev_network()
    conditional_part_input = tf.placeholder(dtype=tf.float32)
    assign_op = tf.assign(conditional_part_input, prev_network_output)
    with tf.control_dependencies([assign_op]):
         F_output = F(conditional_part_input)
         # Use F_output for later computation

在测试阶段,我只需要使用自定义输入运行F,因此我打算使用以下方法:

    feed_dict = {conditional_part_input: custom_test_input}
    results = sess.run([F_output], feed_dict)

我的问题是,这是一种有条件地执行计算图一部分的正确方法吗?另外,由于在训练期间我需要保证conditional_part_input正确地分配了先前计算的输出,因此我使用tf.control_dependencies,但是在测试阶段,我只是直接将一个numpy数组分配给{{ 1}}。但是conditional_part_input的主体仍处于F范围内。这会造成混乱吗?

0 个答案:

没有答案
相关问题