有没有办法逐步运行tensorflow图,而不必每次都使用feed_dict?

问题描述 投票:0回答:2

我正在张力流中编写一个受限制的玻尔兹曼机器,为了理解算法,我想在计算它们的过程中打印东西。我在算法的第一部分做了一个简单的尝试:

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"]()]]

简而言之,它抱怨我没有为占位符提供任何值。当然我不想这样做,而是使用张量流来使用我在之前的sess.run()调用中给出的任何东西。这可能吗?

python tensorflow
2个回答
1
投票

即使您的解决方案适用于您的问题,您也可以考虑采用更通用的方法在一次通话中获取所需的一切。

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是有状态的,所以你无法保证连续的调用是一致的。最常见的示例是每次查询时绘制新数字的随机生成器。

当您对Session.run进行一次调用时,您(几乎)确定返回的值是一致的。


0
投票

我自己找到了答案:

只需将_h传递给最后一个feed_dict电话的sess_run()。无需再次发送所有内容:

印刷:

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}))
© www.soinside.com 2019 - 2024. All rights reserved.