我想知道tf.global_variables_initializer()
是否也为iterator
初始化了tf.data.Dataset
,还是我需要分别将iterator
初始化为:
iterator = dataset.make_initializable_iterator()
sess.run(iterator.initializer)
答案 0 :(得分:2)
您必须单独初始化迭代器。
没有{strong> None 变量Feed到tf.global_variables_initializer()
max_value = tf.placeholder(tf.int64, shape=[])
dataset = tf.data.Dataset.range(max_value)
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()
# Initialize an iterator over a dataset with 10 elements.
sess.run(iterator.initializer, feed_dict={max_value: 10})
for i in range(10):
value = sess.run(next_element)
assert i == value
# Initialize the same iterator over a dataset with 100 elements.
sess.run(iterator.initializer, feed_dict={max_value: 100})
for i in range(100):
value = sess.run(next_element)
assert i == value