TensorFlow 基本问题思考
大家觉得这两段简单的代码有什么本质不同吗?
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(0,10,2):
print(sess.run(next_element))
0
1
2
3
4
max_value =20
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)
for i in range(0,10,2):
print(sess.run(next_element))
0
1
2
3
4
这似乎可以传入参数了,与feed的方式有点像了,这也提示了对一些问题的理解。
def data_iterator(max_value):
dataset = tf.data.Dataset.range(max_value)
iterator = dataset.make_initializable_iterator()
return iterator
iterator = data_iterator(10)
next_element = iterator.get_next()
Initialize an iterator over a dataset with 10 elements.
sess.run(iterator.initializer)
for i in range(0,10,2):
print(sess.run(next_element))
0
1
2
3
4