如何在任意位置从from_tensor_slices获取数据集(Tensorflow)。

问题描述 投票:0回答:1

我需要从数据集中获取任意位置的数据。

看起来是这样的。

tf.Tensor([77 78 79 80 81 82 83 84 85 86], shape=(10,), dtype=int32)
tf.Tensor([ 7  8  9 10 11 12 13 14 15 16], shape=(10,), dtype=int32)
tf.Tensor([35 36 37 38 39 40 41 42 43 44], shape=(10,), dtype=int32)
tf.Tensor([55 56 57 58 59 60 61 62 63 64], shape=(10,), dtype=int32)
tf.Tensor([67 68 69 70 71 72 73 74 75 76], shape=(10,), dtype=int32)
tf.Tensor([ 3  4  5  6  7  8  9 10 11 12], shape=(10,), dtype=int32)
tf.Tensor([ 2  3  4  5  6  7  8  9 10 11], shape=(10,), dtype=int32)
tf.Tensor([64 65 66 67 68 69 70 71 72 73], shape=(10,), dtype=int32)
tf.Tensor([26 27 28 29 30 31 32 33 34 35], shape=(10,), dtype=int32)
tf.Tensor([17 18 19 20 21 22 23 24 25 26], shape=(10,), dtype=int32)
tf.Tensor([79 80 81 82 83 84 85 86 87 88], shape=(10,), dtype=int32)
tf.Tensor([ 5  6  7  8  9 10 11 12 13 14], shape=(10,), dtype=int32)

但是... .batch() 函数不能给出一个随机的位置。

a = tf.data.Dataset.from_tensor_slices(np.arange(100))
b = a.batch(10).shuffle(100).repeat()

for c in b.take(12):
    print(c)

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

我怎么才能得到任意位置的数据?

tensorflow tensorflow-datasets tensorflow2.0
1个回答
0
投票

在这里提供解决方案(答案部分),尽管它存在于评论部分,以利于社区。

要随机开始数据,但后续的数据是连续的。

a = tf.data.Dataset.from_tensor_slices(np.arange(100))
b = a.window(10, shift=1, drop_remainder=True).flat_map(lambda window: window.batch(10)).shuffle(100)

for c in b.take(12):
    print(c)

输出。

tf.Tensor([21 22 23 24 25 26 27 28 29 30], shape=(10,), dtype=int64)
tf.Tensor([ 8  9 10 11 12 13 14 15 16 17], shape=(10,), dtype=int64)
tf.Tensor([84 85 86 87 88 89 90 91 92 93], shape=(10,), dtype=int64)
tf.Tensor([55 56 57 58 59 60 61 62 63 64], shape=(10,), dtype=int64)
tf.Tensor([ 4  5  6  7  8  9 10 11 12 13], shape=(10,), dtype=int64)
tf.Tensor([15 16 17 18 19 20 21 22 23 24], shape=(10,), dtype=int64)
tf.Tensor([25 26 27 28 29 30 31 32 33 34], shape=(10,), dtype=int64)
tf.Tensor([47 48 49 50 51 52 53 54 55 56], shape=(10,), dtype=int64)
tf.Tensor([13 14 15 16 17 18 19 20 21 22], shape=(10,), dtype=int64)
tf.Tensor([17 18 19 20 21 22 23 24 25 26], shape=(10,), dtype=int64)
tf.Tensor([61 62 63 64 65 66 67 68 69 70], shape=(10,), dtype=int64)
tf.Tensor([62 63 64 65 66 67 68 69 70 71], shape=(10,), dtype=int64)

开始时数据是随机的 但随后的数据不是连续的

a = tf.data.Dataset.from_tensor_slices(np.arange(100))
b = a.shuffle(100).batch(10).repeat()

for c in b.take(12):
    print(c)

产出:

tf.Tensor([ 2 65 77 48 39 62 88 92 98 10], shape=(10,), dtype=int64)
tf.Tensor([75 13 21 26 87 86  3 71 45 57], shape=(10,), dtype=int64)
tf.Tensor([ 4 90 43 58 93 31 17  9 72 42], shape=(10,), dtype=int64)
tf.Tensor([36 37 83 19 11 99 52 89 56 82], shape=(10,), dtype=int64)
tf.Tensor([22 94 68 60 12 74 73 61 29 84], shape=(10,), dtype=int64)
tf.Tensor([40 69 64 70 32  8 54 80 15 67], shape=(10,), dtype=int64)
tf.Tensor([55  7 47 51 28 18 27 30 95  5], shape=(10,), dtype=int64)
tf.Tensor([50 63 81 85 97 59 20 24 14 25], shape=(10,), dtype=int64)
tf.Tensor([66 78 79 23 38 76 46 44  6 35], shape=(10,), dtype=int64)
tf.Tensor([34 41  1  0 49 53 96 16 91 33], shape=(10,), dtype=int64)
tf.Tensor([33 51 41  5 27 49 34 32 22 68], shape=(10,), dtype=int64)
tf.Tensor([65 28 85  7 42 39 91 57  1 72], shape=(10,), dtype=int64)
© www.soinside.com 2019 - 2024. All rights reserved.