There are questions answering parts of my question but I can't connect the pieces together.
(有一些问题可以回答我的部分问题,但我无法将各个部分连接在一起。)
Suppose I have a graph that operates on a 1d array of just 2 elements (假设我有一个仅对2个元素的1d数组进行操作的图形)
input = tf.placeholder(tf.float32, [2], name="input")
I want to build a graph that can receive an arbitrary long 2d array of such elements and run the first graph on it
(我想构建一个可以接收此类元素的任意长2d数组的图形,并在其上运行第一个图形)
x = tf.placeholder(tf.float32, [None, 2], name = 'x')
I know how to import the first graph (tf.import_graph_def) and how to run some operation on an array using tf.map_fn
.
(我知道如何导入第一个图形(tf.import_graph_def)以及如何使用tf.map_fn
在数组上运行某些操作。)
But how can I combine the two? (但是,如何将两者结合起来呢?)
For each run of the network I need to pass it a different input. (对于网络的每次运行,我都需要传递一个不同的输入。)
But the mapping is done inside tf.import_graph_def. (但是映射是在tf.import_graph_def内部完成的。)
Should I do the import each time in the function called in the loop? (我是否应该每次在循环中调用的函数中进行导入?)
Sounds wrong ... (听起来不对...)
The code below works, but I believe there is a better way:
(下面的代码有效,但是我相信有更好的方法:)
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()
Update: I tried to achieve the same result, but to keep the imported graph trainable, but fail to do so.
(更新:我试图获得相同的结果,但保持导入的图形可训练,但未能做到。)
The return_elements argument to import_meta_graph
seems to be just ignored and only the saver is returned. (该return_elements参数import_meta_graph
似乎只是忽略,只返回保护程序。)
Then a call to restore fails with the error (然后,调用恢复失败并显示错误)
Tensor Tensor("map/while/save/Const:0", shape=(), dtype=string) may not be fed I'm using the code below:
(我无法使用Tensor 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]]}))
ask by Moshe Kravchik translate from so