我正在使用tensorflow的partial_run()方法围绕运行子图构建概念验证,而无需重新计算。
目前我有一个简单的小 python 脚本(见下文),它应该将两个占位符值相乘并加 1,作为部分图运行。此操作有效一次,然后失败并出现错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Must run 'setup' before performing partial runs!
任何有关调用安装程序时发生此错误的原因的帮助将不胜感激。
我使用的是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!
最佳答案
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!
关于python - Tensorflowpartial_run() "Must run ' setup’在执行部分运行之前!”尽管已经设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45142977/