繁体   English   中英

Tensorflow:将导入的图形操作应用于 2d 张量的每个元素

[英]Tensorflow: applying an imported graph operation to each element of 2d tensor

有些问题回答了我的问题的一部分,但我无法将这些部分连接在一起。 假设我有一个图形,它对只有 2 个元素的一维数组进行操作

input = tf.placeholder(tf.float32, [2], name="input")

我想构建一个图形,该图形可以接收此类元素的任意长二维数组并在其上运行第一个图形

 x = tf.placeholder(tf.float32, [None, 2], name = 'x')

我知道如何导入第一个图 (tf.import_graph_def) 以及如何使用tf.map_fn对数组运行一些操作。 但是我怎样才能将两者结合起来呢? 对于网络的每次运行,我需要向它传递不同的输入。 但是映射是在 tf.import_graph_def 内部完成的。 我应该每次都在循环中调用的函数中进行导入吗? 听起来不对...

下面的代码有效,但我相信有更好的方法:

with tf.Graph().as_default() as g_1:
input = tf.placeholder(tf.float32, [2], name="input")
y = tf.add(input[0], input[1])
output = tf.identity(y, name="output")

gdef_1 = g_1.as_graph_def()

tf.reset_default_graph()
with tf.Graph().as_default() as g_combined:
    x = tf.placeholder(tf.float32, [None, 2], name = 'x')

    def calc_z(el):
        y, = tf.import_graph_def(gdef_1, input_map={"input:0": el},
                               return_elements=["output:0"])
        return y

    final_result = tf.map_fn(calc_z, x)

    init = tf.global_variables_initializer()

with tf.Session(graph=g_combined) as sess:
    # For tensorboard
    # run it as tensorboard --logdir=graphs
    writer = tf.summary.FileWriter('./graphs', sess.graph)
    # Run the initializer
    sess.run(init)
    print(sess.run([final_result], feed_dict = {x:[[1,2],[3,4],[5,6]]}))
    writer.close()

更新:我试图达到相同的结果,但为了保持导入的图形可训练,但没有这样做。 该return_elements参数import_meta_graph似乎只是忽略,只返回保护程序。 然后调用恢复失败并出现错误

张量 Tensor("map/while/save/Const:0", shape=(), dtype=string) 可能无法输入我正在使用以下代码:

tf.reset_default_graph()
xx = tf.placeholder(tf.float32, [2], name="xx")
yy = tf.add(xx[0], xx[1])
yy = tf.identity(yy, name = 'yy')
#need at least 1 varaible to save the graph
_ = tf.Variable(initial_value='fake_variable')

config = tf.ConfigProto(log_device_placement=False)
config.gpu_options.allow_growth = True

with tf.Session(config=config) as sess:    
    saver = tf.train.Saver()
    sess.run(tf.initialize_all_variables())
    saver.save(sess, "./model_ex2")

tf.reset_default_graph()
with tf.Session() as sess:
    x = tf.placeholder(tf.float32, [None, 2], name = 'x')

    def calc_z(el):
#         saver, yy  = tf.train.import_meta_graph("./model_ex2.meta", 
#                                            input_map={"xx:0": el}, return_elements=["yy:0"])
#         saver.restore(sess, "./model_ex2")
#         return yy
        # return_elements argument seems to be ignored and only the saver is returned.
        saver = tf.train.import_meta_graph("./model_ex2.meta", 
                                           input_map={"xx:0": el})
        saver.restore(sess, "./model_ex2")
        return yy

    final_result = tf.map_fn(calc_z, x)

init = tf.global_variables_initializer()
with tf.Session(config=config) as sess:
    sess.run(init)
    print(sess.run([final_result, op], feed_dict = {x:[[1,2],[3,4],[5,6]]}))

您当前的解决方案实际上已经很好了。 该图仅在g_combined构建时导入一次,而不是x每个元素一次,因此它可以执行您想要的操作。

如果您有一个元图,它应该与tf.train.import_meta_graph类似地工作,因为input_mapreturn_elements也应该与它一起使用(但请注意,此函数也返回导入的保护程序)。 但是,您也可以在不同的图形中导入元图,将其冻结(例如使用tf.graph_util.convert_variables_to_constants ),然后将该图形 def 导入到最终图形中。

import tensorflow as tf

meta_graph_path = ...
meta_graph_save_path = ...
with tf.Graph().as_default() as g_meta_import, tf.Session() as sess:
    saver = tf.train.import_meta_graph(meta_graph_path)
    saver.restore(sess, meta_graph_save_path)
    frozen_graph = tf.graph_util.convert_variables_to_constants(
        sess, tf.get_default_graph().as_graph_def(), 'output')

with tf.Graph().as_default() as g_combined:
    x = tf.placeholder(tf.float32, [None, 2], name = 'x')
    def calc_z(el):
        y, = tf.import_graph_def(frozen_graph, input_map={'input:0': el},
                                 return_elements=['output:0'])
        return y
    final_result = tf.map_fn(calc_z, x)
    init = tf.global_variables_initializer()

这种解决方案的唯一问题是,进口部分显然会被冻结并且无法训练。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM