tf.global_variables_initializer()是否还会初始化tf.data.Dataset的迭代器?

时间:2018-08-07 23:11:23

标签: python tensorflow tensorflow-datasets

我想知道tf.global_variables_initializer()是否也为iterator初始化了tf.data.Dataset,还是我需要分别将iterator初始化为:

iterator = dataset.make_initializable_iterator() sess.run(iterator.initializer)

1 个答案:

答案 0 :(得分:2)

您必须单独初始化迭代器。 没有{strong> None 变量Feed到tf.global_variables_initializer()

official example

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