【发布时间】:2017-02-11 16:36:47
【问题描述】:
我需要将一个图表的输出用作深度学习项目的另一个图表的输入,然后优化这两个图表的所有变量。每个图的输入是一个占位符。
我的问题与这里讨论的问题非常相似:Tensorflow: How to replace a node in a calculation graph?
不幸的是,由于我当时遇到的错误以及当时指出的错误,问题没有得到解决。我会发布我的案例
这是一个实际执行简单计算的示例程序
with tf.Graph().as_default() as g_1:
input_1 = tf.placeholder(tf.float32, shape=[3,3], name="input")
weight1 = tf.Variable(tf.truncated_normal([3, 3], stddev=0.1), name="weight")
y = tf.matmul(input_1,weight1)
# NOTE: using identity to get a known name for the output tensor.
output1 = tf.identity(y, name="output")
gdef_1 = g_1.as_graph_def()
with tf.Graph().as_default() as g_2: # NOTE: g_2 not g_1
input_2 = tf.placeholder(tf.float32, shape=[3,3], name="input")
weight2 = tf.Variable(tf.truncated_normal([3, 3], stddev=0.1), name="weight")
z = tf.matmul(input_2, weight2)
output2 = tf.identity(z, name="output")
gdef_2 = g_2.as_graph_def()
然后我将这两个图导入另一个图中:
with tf.Graph().as_default() as g_combined:
x = tf.placeholder(tf.float32, shape=[3,3], name="input_matrix")
# Import gdef_1, which performs f(x).
# "input:0" and "output:0" are the names of tensors in gdef_1.
y, = tf.import_graph_def(gdef_1, input_map={"input:0": x},
return_elements=["output:0"])
# Import gdef_2, which performs g(y)
z, = tf.import_graph_def(gdef_2, input_map={"input:0": y},
return_elements=["output:0"])
cost = tf.reduce_sum(tf.square(z-x))
variables = [op.outputs[0] for op in tf.get_default_graph().get_operations() if op.type == "Variable"]
print (variables)
optimizer = tf.train.AdamOptimizer(0.01).minimize(cost,var_list = variables)
这是之前链接的问题中建议的解决方案,但它给出了以下错误:
TypeError: Argument is not a tf.Variable: Tensor("import/weight:0", dtype=float32_ref)
变量variables 包含:
[<tf.Tensor 'import/weight:0' shape=<unknown> dtype=float32_ref>, <tf.Tensor 'import_1/weight:0' shape=<unknown> dtype=float32_ref>]
有人知道如何让它工作吗?或者考虑到我需要一个中间结果来提供占位符,如何优化整个结构?
非常感谢
【问题讨论】:
-
如果您使用之前链接的问题中建议的完全相同的代码,您能否重现结果?如果是这样,那么您可以比较您的版本和工作版本以帮助确定错误原因?
-
我说我得到了和上一个链接一样的结果,它的版本不起作用(和我的完全一样)。
标签: python optimization graph tensorflow placeholder