Python 如何在Tensorflow中使用输入和输出变量构建可重用图?

Python 如何在Tensorflow中使用输入和输出变量构建可重用图?,python,tensorflow,Python,Tensorflow,我正在尝试从Python代码构建TF图,保存它,然后导入 来自另一个程序的图形,在该程序中它将与实际 数据为了使代码保持简单,我将显示 使用计算常用二次函数迭代次数的图的问题 Mandelbrot集合的函数 我得到了这段Python代码,并产生了我期望的结果: def mandelbrot(x, y): """ Run the TF graph returned by mandelbrot_() """ g, in_, out_ = mandelbrot_()

我正在尝试从Python代码构建TF图,保存它,然后导入 来自另一个程序的图形,在该程序中它将与实际 数据为了使代码保持简单,我将显示 使用计算常用二次函数迭代次数的图的问题 Mandelbrot集合的函数

我得到了这段Python代码,并产生了我期望的结果:

def mandelbrot(x, y):
    """
    Run the TF graph returned by mandelbrot_()
    """
    g, in_, out_ = mandelbrot_()
    x_in, y_in = in_
    n_out, x_out, y_out = out_
    with tf.Session(graph=g).as_default() as session:
        # (a)
        # run the graph at the chosen point
        feed = { x_in:x, y_in:y }
        n_out, x_out, y_out = session.run(out_, feed)
        print("({0},{1}): {2}".format(x, y, [n_out, x_out, y_out]))

def mandelbrot_(maxiter=255):
    """
    Return graph computing the Mandelbrot set at (x,y).
    """
    graph = tf.Graph()
    with graph.as_default():
        # input placeholders
        x = tf.placeholder(tf.float32, shape=[], name='x_in')
        y = tf.placeholder(tf.float32, shape=[], name='y_in')

        # output variables
        n_ = tf.Variable(0, tf.int32,   name='n')
        x_ = tf.Variable(x, tf.float32, name='x')
        y_ = tf.Variable(y, tf.float32, name='y')

        # main loop
        i_ = tf.constant(0)
        def cond(i_, z_re_, z_im_):
            return tf.logical_and(
                tf.less(i_, maxiter),
                (z_re_*z_re_ + z_im_*z_im_) < 4)
        def body(i_, z_re_, z_im_):
            return [
                i_+1,                          # iteration count
                z_re_*z_re_ - z_im_*z_im_ + x, # real part of z
                2*z_re_*z_im_ + y,             # imag part of z
            ]
        l = tf.while_loop(cond, body, [i_, x, y],
                          parallel_iterations=1)
        n_, x_, y_ = l  # (b)

    return (
        graph,       # graph
        (x, y),      # inputs
        (n_, x_, y_) # outputs
    )

if __name__ == '__main__':
    mandelbrot(0.25, -0.15)
现在,如果我试图保存图形,tf.Saver会抱怨 没有输出变量和中止。因此,我尝试捕获 由mandelbrot_uu生成的图形转换为输出变量,并使用 他们简而言之,这是代码,它与前一个代码不同,因为在标记为a和b的点中进行了编辑:

通过这些编辑,输出变量始终为空:

(0.25,-0.15): [0, 0.0, 0.0]
完整的非工作代码在中

我做错了什么?如何确保变量保存
图形的最终计算?

赋值操作返回一个张量,您需要使用它,否则您从TF的角度对赋值本身不感兴趣

通过以下方式更改mandelbrot_uuu代码:

mandelbrot_uu函数末尾的部分代码 对于tf.control_dependenciesl: n_uuu=n_uuu.assignl[0] x_uuu=x_uuu.assignl[1] y_uuu=y_uuu.assignl[2] 回来 图,图 x、 y,输入 n,x,y,输出 n,x,y变量 它现在返回赋值和变量的输出结果

还更改了mandelbrot函数以初始化附加元组中返回的变量:

def mandelbrotx,y: 计算Mandelbrot函数在x,y处的迭代次数。 g、 输入,输出,变量=mandelbrot_ x_in,y_in=in_ n_out,x_out,y_out=out_ n_var,x_var,y_var=vars_ 如果tf.Sessiongraph=g.as_默认为会话: 使用空值初始化变量 feed0={x_in:0.0,y_in:0.0} session.runn_变量初始值设定项,feed0 session.runx_var.initializer,feed0 session.runy_变量初始值设定项,feed0 在所选点运行图形 feed={x_in:x,y_in:y} n_out,x_out,y_out=session.runout,feed 打印{0},{1}:{2}.formatx,y[n_out,x_out,y_out]
现在运行mandelbrot0.25,-0.15会产生0.25,-0.15:[255,0.22613873,-0.2738613]输出。

事实上,这很简单:-非常感谢!
def mandelbrot(x, y):
    """
    Compute number of iterations of the Mandelbrot function at (x,y).
    """
    g, in_, out_ = mandelbrot_()
    # ...
    with tf.Session(graph=g).as_default() as session:
        # (a)  *** code added below this line ***
        # initialize vars with null values
        feed0 = { x_in:0.0, y_in:0.0 }
        session.run(n_out.initializer, feed0)
        session.run(x_out.initializer, feed0)
        session.run(y_out.initializer, feed0)
        # run the graph at the chosen point
        # ... (see previous code sample) ...

def mandelbrot_(maxiter=255):
    """
    Return graph computing the Mandelbrot set at (x,y).
    """
    graph = tf.Graph()
    with graph.as_default():
        # ... (see previous code sample) ...
        l = tf.while_loop(cond, body, [i_, x, y],
                          parallel_iterations=1)
        # (b)  *** code added below ***
        with tf.control_dependencies(l):
            n_.assign(l[0])
            x_.assign(l[1])
            y_.assign(l[2])
        # it works if I use this line instead:
        #n_, x_, y_ = l

    return (
        # ...
    )
(0.25,-0.15): [0, 0.0, 0.0]