簡體   English   中英

使用tensorflow2.0為RNN預處理不同大小的大數據集

[英]preprocessing big dataset with different size for RNN with tensorflow2.0

我有一個使用 Keras 運行 RNN 的代碼。 最近,我增加了我的樣本的大小,但是由於內存的原因,代碼在預處理階段崩潰了。 我認為這是因為我使用蠻力來制作窗口,這意味着我正在使用 for 循環並將所有窗口附加到列表中以形成輸入數據。 因此,我正在嘗試使用 tersorflow.data.Dataset 內置函數。 但是,我的原始數據集有 990 個具有不同時間步長的樣本。 我不知道如何使用這些內置函數來確保沒有跨樣本獲得窗口(窗口應該只在每個樣本內掃描)。

我的原始樣本每個都有一個大小 ((# of timesteps, 305 features), (# of timesteps, 299 features))。 我的目標是擁有大小為 (3,305) 的小窗口,並且此窗口的目標大小為 (1,299)。 即窗口有3個時間步,對應的目標來自第三個時間步。

這是一個例子。

原始數據包含(樣本和標簽)對

sample1=[[1,1],[2,2],[3,3],[4,4],[5,5]]

label1=[[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]]

sample2=[[6,6],[7,7],[8,8],[9,9],[10,10],[11,11],[12,12]]

label2=[[6,6,6],[7,7,7],[8,8,8],[9,9,9],[10,10,10],[11,11,11],[12,12,12]]

sample=[sample1,sample2]

label=[label1,label2]

我想得到這樣的訓練和目標對

[1,1],[2,2],[3,3] -> [3,3,3]

[2,2],[3,3],[4,4] -> [4,4,4]

[3,3],[4,4],[5,5] -> [5,5,5]

[6,6],[7,7],[8,8] -> [8,8,8]

[7,7],[8,8],[9,9] -> [9,9,9]

[8,8],[9,9],[10,10] -> [10,10,10]

[9,9],[10,10],[11,11] -> [11,11,11]

[10,10],[11,11],[12,12] -> [12,12,12]

請注意,如果我堆疊所有樣本,將有大約 4950000 個時間步長。 我目前的方法只是創建一個空列表並將小窗口附加到列表中。 這最終會得到一個更大的數組。

請讓我知道我應該如何在不發生內存崩潰的情況下實現這一目標。 提前致謝。

這是我使用 tersorflow.data.Dataset 內置函數的嘗試:


dataset = tf.data.Dataset.from_generator(

                   lambda: itertools.zip_longest(sample, label),

                   output_types = (tf.float32, tf.float32),

                   output_shapes = (tf.TensorShape([None,  2]),

                   tf.TensorShape([None, 3])))


dataset_window = dataset.window(3, shift=1, drop_remainder=True)

tf.print(dataset_window)

這給了我

<Da​​tasetV1Adapter 形狀:(DatasetSpec(TensorSpec(shape=(None, 2), dtype=tf.float32, name=None), TensorShape([])), DatasetSpec(TensorSpec(shape=(None, 3), dtype=tf .float32, name=None), TensorShape([]))), 類型: (DatasetSpec(TensorSpec(shape=(None, 2), dtype=tf.float32, name=None), TensorShape([])), DatasetSpec (TensorSpec(shape=(None, 3), dtype=tf.float32, name=None), TensorShape([])))>

我不知道為什么 TensorShape 是空的。 我也不知道這樣做是否會使我的代碼免於內存崩潰。

即使TensorShape為空,它也會顯示元素具有shape=(None, 2) , shape=(None, 3) 您可以打印元素以查看內部內容。

如果您使用的是 Tensorflow 2.0(或更低版本), window將為您提供DatasetV1Adapter類型,而對於更高版本, window將為您提供WindowDataset類型。

TensorFlow 1.x:

%tensorflow_version 1.x
import tensorflow as tf
import itertools
tf.compat.v1.enable_eager_execution()

sample1=[[1,1],[2,2],[3,3],[4,4],[5,5]]

label1=[[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]]

sample2=[[6,6],[7,7],[8,8],[9,9],[10,10],[11,11],[12,12]]

label2=[[6,6,6],[7,7,7],[8,8,8],[9,9,9],[10,10,10],[11,11,11],[12,12,12]]

sample=[sample1,sample2]

label=[label1,label2]

dataset = tf.data.Dataset.from_generator(

                   lambda: itertools.zip_longest(sample, label),

                   output_types = (tf.float32, tf.float32),

                   output_shapes = (tf.TensorShape([None,  2]),

                   tf.TensorShape([None, 3])))

dataset_window = dataset.window(2, shift=1, drop_remainder=True)

print(dataset_window)

for i in dataset_window:
    for j in i:
      for k in j:
        print(k)

輸出:

TensorFlow 1.x selected.
<DatasetV1Adapter shapes: (DatasetSpec(TensorSpec(shape=(?, 2), dtype=tf.float32, name=None), TensorShape([])), DatasetSpec(TensorSpec(shape=(?, 3), dtype=tf.float32, name=None), TensorShape([]))), types: (DatasetSpec(TensorSpec(shape=(?, 2), dtype=tf.float32, name=None), TensorShape([])), DatasetSpec(TensorSpec(shape=(?, 3), dtype=tf.float32, name=None), TensorShape([])))>
tf.Tensor(
[[1. 1.]
 [2. 2.]
 [3. 3.]
 [4. 4.]
 [5. 5.]], shape=(5, 2), dtype=float32)
tf.Tensor(
[[ 6.  6.]
 [ 7.  7.]
 [ 8.  8.]
 [ 9.  9.]
 [10. 10.]
 [11. 11.]
 [12. 12.]], shape=(7, 2), dtype=float32)
tf.Tensor(
[[1. 1. 1.]
 [2. 2. 2.]
 [3. 3. 3.]
 [4. 4. 4.]
 [5. 5. 5.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[ 6.  6.  6.]
 [ 7.  7.  7.]
 [ 8.  8.  8.]
 [ 9.  9.  9.]
 [10. 10. 10.]
 [11. 11. 11.]
 [12. 12. 12.]], shape=(7, 3), dtype=float32)

張量流 2.x:

%tensorflow_version 2.x
import tensorflow as tf
import itertools

sample1=[[1,1],[2,2],[3,3],[4,4],[5,5]]

label1=[[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5]]

sample2=[[6,6],[7,7],[8,8],[9,9],[10,10],[11,11],[12,12]]

label2=[[6,6,6],[7,7,7],[8,8,8],[9,9,9],[10,10,10],[11,11,11],[12,12,12]]

sample=[sample1,sample2]

label=[label1,label2]

dataset = tf.data.Dataset.from_generator(

                   lambda: itertools.zip_longest(sample, label),

                   output_types = (tf.float32, tf.float32),

                   output_shapes = (tf.TensorShape([None,  2]),

                   tf.TensorShape([None, 3])))

dataset_window = dataset.window(2, shift=1, drop_remainder=True)

print(dataset_window)

for i in dataset_window:
    for j in i:
      for k in j:
        print(k)

輸出:

<WindowDataset shapes: (DatasetSpec(TensorSpec(shape=(None, 2), dtype=tf.float32, name=None), TensorShape([])), DatasetSpec(TensorSpec(shape=(None, 3), dtype=tf.float32, name=None), TensorShape([]))), types: (DatasetSpec(TensorSpec(shape=(None, 2), dtype=tf.float32, name=None), TensorShape([])), DatasetSpec(TensorSpec(shape=(None, 3), dtype=tf.float32, name=None), TensorShape([])))>
tf.Tensor(
[[1. 1.]
 [2. 2.]
 [3. 3.]
 [4. 4.]
 [5. 5.]], shape=(5, 2), dtype=float32)
tf.Tensor(
[[ 6.  6.]
 [ 7.  7.]
 [ 8.  8.]
 [ 9.  9.]
 [10. 10.]
 [11. 11.]
 [12. 12.]], shape=(7, 2), dtype=float32)
tf.Tensor(
[[1. 1. 1.]
 [2. 2. 2.]
 [3. 3. 3.]
 [4. 4. 4.]
 [5. 5. 5.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[ 6.  6.  6.]
 [ 7.  7.  7.]
 [ 8.  8.  8.]
 [ 9.  9.  9.]
 [10. 10. 10.]
 [11. 11. 11.]
 [12. 12. 12.]], shape=(7, 3), dtype=float32)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM