# TensorFlow dataset API 使用

### 1. 从迭代器中引入数据

import numpy as np
import tensorflow as tf

def gen():
for _ in range(10):
sz = np.random.randint(3, 20, 1)[0]
yield np.random.randint(1, 100, sz), np.random.randint(0, 10, 1)[0]

dataset = tf.data.Dataset.from_generator(
iter = dataset.make_one_shot_iterator()
x, y = iter.get_next()

with tf.Session() as sess:
try:
while True:
_x, _y = sess.run([x, y])
print("x is :\n", _x)
print("y is :\n", _y)
print("*" * 50)

except tf.errors.OutOfRangeError:
print("done")
finally:
pass


x is :
[[41 57 68 84 40 72 98 71 95 50 94 17 78 60 69 29 77]
[55 44 11 70 39 39 97 86 71 20  0  0  0  0  0  0  0]
[12 36 75 49 86  0  0  0  0  0  0  0  0  0  0  0  0]]
y is :
[4 1 9]
**************************************************
x is :
[[59 33 64 47 20 53 93 68 73 57 68 59 34]
[69 39 12 83 54 11 92 89 60 21 30 30 31]
[19 32 62  9 66 34 85 86 22 33 19 79 28]]
y is :
[8 1 5]
**************************************************
x is :
[[47 24 96 38 21 53 78 52 74 15 87 37 21 29 45 61 19 56 73]
[ 1 24 32  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
[73 52 14 11 83 77 83 24 34  0  0  0  0  0  0  0  0  0  0]]
y is :
[9 4 4]
**************************************************
x is :
[[34 21 36 17 90 96 19  3 28 60 87 93  4 41 22 89 70 83 58]
[70 25 84 42 45 29 40  0  0  0  0  0  0  0  0  0  0  0  0]
[97 72 19 73  7  9 83 46 72 64 98 13 78 94 66 10 30 46 13]]
y is :
[9 9 4]
**************************************************
x is :
[[33 27 59 45 79 21 57 17 46 24 67 64 83 95 59 65  7 26 82]
[84 31 48 91  7 51 14 71 17 40 89 44 25 17 42 13 99  0  0]
[63 97 45 49 68 70 79 28 90  4 68 77 27  0  0  0  0  0  0]]
y is :
[8 1 8]
**************************************************
x is :
[[62 19 42 88  3 16 20 38  5 59]
[99 84 87 10  8 13  0  0  0  0]
[44 45 45 58 34 53  8 54  0  0]]
y is :
[1 1 4]
**************************************************
x is :
[[77 51 44 51  2 38 60 46 12 78 20 15 23 57]
[25 81 23 22  0  0  0  0  0  0  0  0  0  0]]
y is :
[4 5]
**************************************************
done

posted @ 2018-12-17 14:40  狂徒归来  阅读(634)  评论(0编辑  收藏  举报