Tensorflow partial_run()"必须运行'设置'在进行部分跑步之前!"尽管设置了

时间:2017-07-17 11:23:36

标签: python tensorflow

我正在使用tensorflow的partial_run()方法构建关于运行子图的概念验证,而无需重新计算。

目前我有一个简单的小python脚本(见下文),它应该将两个占位符值相乘并添加1,作为部分图形运行。此操作只运行一次,然后失败并显示错误:

  

tensorflow.python.framework.errors_impl.InvalidArgumentError:必须运行' setup'在进行部分跑步之前!

有关为何在调用设置时发生此错误的任何帮助都将不胜感激。

我使用的是Ubuntu 16.10和tensorflow 1.2.1。

代码:

import tensorflow as tf

a = tf.placeholder(tf.float32, name='a')
b = tf.placeholder(tf.float32, name='b')

c = tf.multiply(a, b, name='c')

y = tf.add(c, 1, name='y')

ilist = [{a: 1, b: 1}, {a: 2, b: 2}, {a: 1}, {b: 1}, {b: 3}]

with tf.Session() as sess:
    hdle = sess.partial_run_setup([y], [a, b])

    for i, fd in enumerate(ilist):
        y_r = sess.partial_run(hdle, y, feed_dict=fd)

        eout = fd[a] * fd[b] + 1
        print("got {}, expected {}".format(y_r, eout))

完整输出:

got 2.0, expected 2
Traceback (most recent call last):
  File "merged.py", line 15, in <module>
    y_r = sess.partial_run(hdle, y, feed_dict=fd)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 844, in partial_run
    return self._run(handle, fetches, feed_dict, None, None)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 997, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1135, in _do_run
    fetch_list)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1152, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Must run 'setup' before performing partial runs!

2 个答案:

答案 0 :(得分:1)

API文档中的这个示例有效:

import tensorflow as tf
a = tf.placeholder(tf.float32, shape=[])
b = tf.placeholder(tf.float32, shape=[])
c = tf.placeholder(tf.float32, shape=[])
r1 = tf.add(a, b)
r2 = tf.multiply(r1, c)

with tf.Session() as sess:
    h = sess.partial_run_setup([r1, r2], [a, b, c])
    res = sess.partial_run(h, r1, feed_dict={a: 1, b: 2})
    res = sess.partial_run(h, r2, feed_dict={c: 2})        
    print(res) #prints 6.0

但是如果我们添加更多的调用它就不会。如果这不起作用,那么使用partial_run的要点是什么。

import tensorflow as tf
a = tf.placeholder(tf.float32, shape=[])
b = tf.placeholder(tf.float32, shape=[])
c = tf.placeholder(tf.float32, shape=[])
r1 = tf.add(a, b)
r2 = tf.multiply(r1, c)

with tf.Session() as sess:
    h = sess.partial_run_setup([r1, r2], [a, b, c])
    res = sess.partial_run(h, r1, feed_dict={a: 1, b: 2})
    res = sess.partial_run(h, r2, feed_dict={c: 2})
    res = sess.partial_run(h, r2, feed_dict={c: 3})

    print(res)
InvalidArgumentError: Must run 'setup' before performing partial runs!

答案 1 :(得分:0)

错误消息非常明确:您必须在(每次)部分运行之前运行安装程序。我认为change action from redux-form是参考 此外,您的ilist dicts列表仅适用于2次运行。在第三次运行中,您只为a提供一个值 - 这不起作用。这是一个适合我的示例循环:

ilist = [{a: 1, b: 1}, {a: 2, b: 2}, {a: 1, b: 1}, {a: 1, b: 3}]

with tf.Session() as sess:
    for i, fd in enumerate(ilist):
        hdle = sess.partial_run_setup([y], [a, b])
        y_r = sess.partial_run(hdle, y, feed_dict=fd)

        eout = fd[a] * fd[b] + 1
        print("got {}, expected {}".format(y_r, eout))