我试图在tensorflow 1.0中编写一个简单的数据生成器来解决图像分类问题。我有一个图像路径列表和相应的标签,如2个列表:路径和标签。
我正在使用以下代码来获取数据对象和迭代器。
dataset = (
tf.data.Dataset.from_tensor_slices((paths, labels))
.shuffle(buffer_size = len(paths))
.map(parse_fn, num_parallel_calls = 4)
.batch(32)
.prefetch(1)
)
train_iter = dataset.make_initializable_iterator()
train_next = train_iter.get_next()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(train_iter)
x, y = sess.run(train_next)
但是我遇到以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-14-139e601c664d> in <module>()
25 with tf.Session() as sess:
26 sess.run(tf.global_variables_initializer())
---> 27 sess.run(train_iter)
28 x, y = sess.run(train_next)
29 print(x.shape, y.shape)
/home/surya/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
927 try:
928 result = self._run(None, fetches, feed_dict, options_ptr,
--> 929 run_metadata_ptr)
930 if run_metadata:
931 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/home/surya/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
1135 # Create a fetch handler to take care of the structure of fetches.
1136 fetch_handler = _FetchHandler(
-> 1137 self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
1138
1139 # Run request and get response.
/home/surya/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in __init__(self, graph, fetches, feeds, feed_handles)
469 """
470 with graph.as_default():
--> 471 self._fetch_mapper = _FetchMapper.for_fetch(fetches)
472 self._fetches = []
473 self._targets = []
/home/surya/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in for_fetch(fetch)
269 if isinstance(fetch, tensor_type):
270 fetches, contraction_fn = fetch_fn(fetch)
--> 271 return _ElementFetchMapper(fetches, contraction_fn)
272 # Did not find anything.
273 raise TypeError('Fetch argument %r has invalid type %r' % (fetch,
/home/surya/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in __init__(self, fetches, contraction_fn)
302 raise TypeError('Fetch argument %r has invalid type %r, '
303 'must be a string or Tensor. (%s)' %
--> 304 (fetch, type(fetch), str(e)))
305 except ValueError as e:
306 raise ValueError('Fetch argument %r cannot be interpreted as a '
TypeError: Fetch argument <tensorflow.python.data.ops.iterator_ops.Iterator object at 0x7fe5326ebf90> has invalid type <class 'tensorflow.python.data.ops.iterator_ops.Iterator'>, must be a string or Tensor. (Can not convert a Iterator into a Tensor or Operation.)
如果将迭代器更改为
,则不会收到此错误data_iter = dataset.make_one_shot_iterator()
为什么会出现此错误,以及如何解决?谢谢!
答案 0 :(得分:0)
只需更改行:
sess.run(train_iter)
收件人:
sess.run(train_iter.initializer)
这是因为您要执行迭代器的初始化程序,而不是迭代器本身。