Python 有没有一种方法可以一点一点地运行tensorflow图,而不必每次都使用feed_dict?

Python 有没有一种方法可以一点一点地运行tensorflow图,而不必每次都使用feed_dict?,python,tensorflow,Python,Tensorflow,我正在用tensorflow编写一个受限Boltzmann机器,为了理解算法,我想在计算过程中打印一些东西。我在算法的第一部分做了一个简单的尝试: import tensorflow as tf import numpy as np X_train = np.genfromtxt("binMNIST_data/bindigit_trn.csv", dtype=float, delimiter=",") Y_train = np.genfromtxt("binMNIST_data/targetd

我正在用tensorflow编写一个受限Boltzmann机器,为了理解算法,我想在计算过程中打印一些东西。我在算法的第一部分做了一个简单的尝试:

import tensorflow as tf
import numpy as np

X_train = np.genfromtxt("binMNIST_data/bindigit_trn.csv", dtype=float, delimiter=",")
Y_train = np.genfromtxt("binMNIST_data/targetdigit_trn.csv", dtype=float, delimiter=",")
X_test = np.genfromtxt("binMNIST_data/bindigit_tst.csv", dtype=float, delimiter=",")
Y_test = np.genfromtxt("binMNIST_data/targetdigit_tst.csv", dtype=float, delimiter=",")

ds_train = tf.data.Dataset.from_tensor_slices((X_train, Y_train))
ds_test = tf.data.Dataset.from_tensor_slices((X_test, Y_test))

it = tf.data.Iterator.from_structure(ds_train.output_types, ds_train.output_shapes)

train_init_op = it.make_initializer(ds_train)
test_init_op = it.make_initializer(ds_test)

vb = tf.placeholder(tf.float64, [784])
hb = tf.placeholder(tf.float64, [500])

W = tf.placeholder(tf.float64, [784, 500])

# Features and labels
x, y = it.get_next()

_h = tf.nn.sigmoid(tf.matmul(tf.reshape(x, [1, 784]), W)+hb)
h = tf.nn.relu(tf.sign(_h-tf.random_uniform(tf.shape(_h), dtype=tf.float64)))

#Initial bias values
vb_init = np.zeros([784])
hb_init = np.zeros([500])
#Initial W value
W_init = np.zeros([784, 500])

with tf.Session() as sess:
    sess.run(train_init_op)
    print(sess.run(_h, feed_dict={vb:vb_init, hb:hb_init, W:W_init}))
    print(sess.run(h))
但不幸的是,程序的最后一行导致了这个错误:

Traceback (most recent call last):
  File "/Users/sahandzarrinkoub/.virtualenvs/untitled/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1361, in _do_call
    return fn(*args)
  File "/Users/sahandzarrinkoub/.virtualenvs/untitled/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1340, in _run_fn
    target_list, status, run_metadata)
  File "/Users/sahandzarrinkoub/.virtualenvs/untitled/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 516, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_2' with dtype double and shape [784,500]
     [[Node: Placeholder_2 = Placeholder[dtype=DT_DOUBLE, shape=[784,500], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/sahandzarrinkoub/Documents/Programming/ANN/lab4/notebook/RBM.py", line 41, in <module>
    print(sess.run(h))
  File "/Users/sahandzarrinkoub/.virtualenvs/untitled/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 905, in run
    run_metadata_ptr)
  File "/Users/sahandzarrinkoub/.virtualenvs/untitled/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1137, in _run
    feed_dict_tensor, options, run_metadata)
  File "/Users/sahandzarrinkoub/.virtualenvs/untitled/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1355, in _do_run
    options, run_metadata)
  File "/Users/sahandzarrinkoub/.virtualenvs/untitled/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1374, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_2' with dtype double and shape [784,500]
     [[Node: Placeholder_2 = Placeholder[dtype=DT_DOUBLE, shape=[784,500], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

Caused by op 'Placeholder_2', defined at:
  File "/Users/sahandzarrinkoub/Documents/Programming/ANN/lab4/notebook/RBM.py", line 24, in <module>
    W = tf.placeholder(tf.float64, [784, 500])
  File "/Users/sahandzarrinkoub/.virtualenvs/untitled/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 1746, in placeholder
    return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
  File "/Users/sahandzarrinkoub/.virtualenvs/untitled/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3051, in _placeholder
    "Placeholder", dtype=dtype, shape=shape, name=name)
  File "/Users/sahandzarrinkoub/.virtualenvs/untitled/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/Users/sahandzarrinkoub/.virtualenvs/untitled/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3271, in create_op
    op_def=op_def)
  File "/Users/sahandzarrinkoub/.virtualenvs/untitled/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1650, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_2' with dtype double and shape [784,500]
     [[Node: Placeholder_2 = Placeholder[dtype=DT_DOUBLE, shape=[784,500], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

简单地说,它抱怨我没有为占位符提供任何值。当然,我不希望这样做,而是让tensorflow使用我在前面的sess.run调用中给出的任何内容。这可能吗?

我自己找到了答案:

只需传入最后一次sess运行调用的提要。无需再次发送所有内容:

印刷:

with tf.Session() as sess:
    sess.run(train_init_op)
    h0 = sess.run(_h, feed_dict={vb:vb_init, hb:hb_init, W:W_init})
    print(h0)
    print(sess.run(h, feed_dict={_h:h0}))

即使你的解决方案对你的问题有效,你也可以考虑在一个调用中获得你所需要的一切更一般的方法。

sess.run(train_init_op)
print(sess.run(_h, feed_dict={vb:vb_init, hb:hb_init, W:W_init}))
print(sess.run(h))
变成

_, _h_val, h_val = sess.run([train_init_op, _h, h], feed_dict={vb:vb_init, hb:hb_init, W:W_init})
print(_h_val)
print(h_val)
因为会话是有状态的,所以不能保证连续调用是一致的。最常见的例子是每次查询时都会绘制新数字的随机生成器

当您对Session.run进行单个调用时,几乎可以确定返回的值是一致的