gpt4 book ai didi

python - Keras fit_generator() - 时间序列的批处理如何工作?

转载 作者:太空狗 更新时间:2023-10-29 18:12:37 28 4
gpt4 key购买 nike

上下文:

我目前正在使用带有 Tensorflow 后端的 Keras 进行时间序列预测,因此研究了提供的教程 here .

按照本教程,我来到了 fit_generator() 的生成器的位置。方法进行了说明。
此生成器生成的输出如下(左样本,右目标):

[[[10. 15.]
[20. 25.]]] => [[30. 35.]] -> Batch no. 1: 2 Samples | 1 Target
---------------------------------------------
[[[20. 25.]
[30. 35.]]] => [[40. 45.]] -> Batch no. 2: 2 Samples | 1 Target
---------------------------------------------
[[[30. 35.]
[40. 45.]]] => [[50. 55.]] -> Batch no. 3: 2 Samples | 1 Target
---------------------------------------------
[[[40. 45.]
[50. 55.]]] => [[60. 65.]] -> Batch no. 4: 2 Samples | 1 Target
---------------------------------------------
[[[50. 55.]
[60. 65.]]] => [[70. 75.]] -> Batch no. 5: 2 Samples | 1 Target
---------------------------------------------
[[[60. 65.]
[70. 75.]]] => [[80. 85.]] -> Batch no. 6: 2 Samples | 1 Target
---------------------------------------------
[[[70. 75.]
[80. 85.]]] => [[90. 95.]] -> Batch no. 7: 2 Samples | 1 Target
---------------------------------------------
[[[80. 85.]
[90. 95.]]] => [[100. 105.]] -> Batch no. 8: 2 Samples | 1 Target

在教程中 TimeSeriesGenerator已使用,但对于我的问题,如果使用自定义生成器或此类,则是次要的。
关于数据,我们有 8 个 steps_per_epoch 和一个形状为 (8, 1, 2, 2) 的样本。
生成器被馈送到由 LSTM 实现的循环神经网络。

我的问题
fit_generator()每批只允许一个目标,如 TimeSeriesGenerator 输出的那样.
当我第一次阅读 fit() 的批处理选项时,我认为我可以有多个样本和相应数量的目标(它们是批量处理的,意思是逐行处理)。但这是 fit_generator() 不允许的因此,显然是错误的。
这看起来像这样:
[[[10. 15. 20. 25.]]] => [[30. 35.]]     
[[[20. 25. 30. 35.]]] => [[40. 45.]]
|-> Batch no. 1: 2 Samples | 2 Targets
---------------------------------------------
[[[30. 35. 40. 45.]]] => [[50. 55.]]
[[[40. 45. 50. 55.]]] => [[60. 65.]]
|-> Batch no. 2: 2 Samples | 2 Targets
---------------------------------------------
...

其次,我认为,例如 [10, 15] 和 [20, 25] 被连续用作目标 [30, 35] 的 RNN 的输入,这意味着这类似于输入 [10, 15, 20 , 25]。由于使用第二种方法(我测试过),RNN 的输出不同,所以这也一定是一个错误的结论。

因此,我的问题是:
  • 为什么每批只允许一个目标(我知道有一些
    解决方法,但必须有一个原因)?
  • 我怎么能理解
    一批的计算?意思是,一些输入如何 [[[40,
    45], [50, 55]]] => [[60, 65]]
    处理,为什么它不是模拟[[[40, 45, 50, 55]]] => [[60, 65]]


  • 根据今天的回答编辑
    由于我对样本和目标的定义存在一些误解 - 我遵循我理解的 Keras 在说时试图告诉我的内容:

    ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 2 target samples.



    发生此错误,例如,当我创建一个如下所示的批处理时:
    #This is just a single batch - Multiple batches would be fed to fit_generator()
    (array([[[0, 1, 2, 3, 4],
    [5, 6, 7, 8, 9]]]),
    array([[ 5, 6, 7, 8, 9],
    [10, 11, 12, 13, 14]]))

    这应该是包含两个长度为 5 的时间序列(5 个连续数据点/时间步长)的单个批次,其目标也是两个相应的序列。 [ 5, 6, 7, 8, 9][0, 1, 2, 3, 4]的目标和 [10, 11, 12, 13, 14][5, 6, 7, 8, 9]的对应目标.
    这里的样本形状将是 shape(number_of_batches, number_of_elements_per_batch, sequence_size)和目标形状 shape(number_of_elements_per_batch, sequence_size) .
    Keras 看到 2 个目标样本(在 ValueError 中),因为我有两个提供 3D 样本作为输入和 2D 目标作为输出(也许我只是不知道如何提供 3D 目标......)。

    无论如何,根据@todays 的回答/评论,这被 Keras 解释为两个时间步长和五个特征。关于我的第一个问题(我仍然看到一个序列作为我的序列的目标,就像在这个编辑示例中一样),我寻求信息如何/如果我能做到这一点以及这样的批次会是什么样子(就像我试图在问题)。

    最佳答案

    简答:

    Why is only one target per batch allowed (I know there are some workarounds, but there has to be a reason)?



    事实并非如此。批次中的目标 sample 数量没有限制。唯一的要求是每个批次中的输入样本和目标样本的数量应该相同。阅读长答案以获得进一步的说明。

    How may I understand the calculation of one batch? Meaning, how is some input like [[[40,
    45], [50, 55]]] => [[60, 65]]
    processed and why is it not analog to [[[40, 45, 50, 55]]] => [[60, 65]]?



    第一个是多变量时间序列(即每个时间步有多个特征),第二个是单变量时间序列(即每个时间步有一个特征)。所以它们不是等价的。阅读长答案以获得进一步的说明。

    长答案:

    我将给出我在评论部分中提到的答案,并尝试使用示例对其进行详细说明:

    我认为您正在混合样本、时间步长、特征和目标。让我描述一下我的理解:在您提供的第一个示例中,似乎每个输入样本都包含 2 个时间步长,例如 [10, 15][20, 25] ,其中每个时间步长由两个特征组成,例如10 和 15 或 20 和 25。此外,相应的目标由一个时间步组成,例如 [30, 35] ,它还有两个特点。换句话说,批次中的每个输入样本 必须有相应的目标。然而,每个输入样本的形状及其对应的目标可能不一定相同。

    例如,考虑一个输入和输出都是时间序列的模型。如果我们表示 的形状每个输入样本(input_num_timesteps, input_num_features)的形状每个目标(即输出)数组 (output_num_timesteps, output_num_features) ,我们会有以下情况:

    1) 输入和输出时间步数相同(即 input_num_timesteps == output_num_timesteps )。举个例子,下面的模型可以实现这一点:
    from keras import layers
    from keras import models

    inp = layers.Input(shape=(input_num_timesteps, input_num_features))

    # a stack of RNN layers on top of each other (this is optional)
    x = layers.LSTM(..., return_sequences=True)(inp)
    # ...
    x = layers.LSTM(..., return_sequences=True)(x)

    # a final RNN layer that has `output_num_features` unit
    out = layers.LSTM(output_num_features, return_sequneces=True)(x)

    model = models.Model(inp, out)

    2)输入和输出时间步数不同(即 input_num_timesteps ~= output_num_timesteps )。这通常是通过首先使用一个或多个 LSTM 层的堆栈将输入时间序列编码为一个向量来实现的,然后重复该向量 output_num_timesteps次以获得所需长度的时间序列。对于重复操作,我们可以轻松使用 RepeatVector Keras 中的图层。同样,仅作为示例,以下模型可以实现这一点:
    from keras import layers
    from keras import models

    inp = layers.Input(shape=(input_num_timesteps, input_num_features))

    # a stack of RNN layers on top of each other (this is optional)
    x = layers.LSTM(..., return_sequences=True)(inp)
    # ...
    x = layers.LSTM(...)(x) # The last layer ONLY returns the last output of RNN (i.e. return_sequences=False)

    # repeat `x` as needed (i.e. as the number of timesteps in output timseries)
    x = layers.RepeatVector(output_num_timesteps)(x)

    # a stack of RNN layers on top of each other (this is optional)
    x = layers.LSTM(..., return_sequences=True)(x)
    # ...
    out = layers.LSTM(output_num_features, return_sequneces=True)(x)

    model = models.Model(inp, out)

    作为一种特殊情况,如果输出时间步长的数量为 1(例如,网络试图在给定最后 t 时间步长的情况下预测下一个时间步长),我们可能不需要使用 repeat 而是我们可以使用 Dense层(在这种情况下,模型的输出形状将是 (None, output_num_features) ,而不是 (None, 1, output_num_features) ):
    inp = layers.Input(shape=(input_num_timesteps, input_num_features))

    # a stack of RNN layers on top of each other (this is optional)
    x = layers.LSTM(..., return_sequences=True)(inp)
    # ...
    x = layers.LSTM(...)(x) # The last layer ONLY returns the last output of RNN (i.e. return_sequences=False)

    out = layers.Dense(output_num_features, activation=...)(x)

    model = models.Model(inp, out)

    请注意,上面提供的架构仅用于说明,您可能需要调整或调整它们,例如通过添加更多层,例如 Dense层,基于您的用例和您要解决的问题。

    更新:问题是你看书的时候注意力不够,包括我的评论和回答,还有Keras提出的错误。该错误明确指出:

    ... Found 1 input samples and 2 target samples.



    所以,仔细阅读本文后,如果我是你,我会对自己说:“好吧,Keras 认为输入批次有 1 个输入样本,但我认为我提供了两个样本!!因为我是一个很好的人(! ),我认为我很可能会比 Keras 错,所以让我们找出我做错了什么!”。一个简单而快速的检查就是检查输入数组的形状:
    >>> np.array([[[0, 1, 2, 3, 4],
    [5, 6, 7, 8, 9]]]).shape
    (1,2,5)

    “哦,它说 (1,2,5) !所以这意味着 一个 样本​​有 两个 时间步长,每个时间步长有五个特征!!!所以我错误地认为这个数组由两个长度为 5 的样本,其中每个时间步的长度为 1 !!那么我现在该怎么办???”好吧,您可以逐步修复它:
    # step 1: I want a numpy array
    s1 = np.array([])

    # step 2: I want it to have two samples
    s2 = np.array([
    [],
    []
    ])

    # step 3: I want each sample to have 5 timesteps of length 1 in them
    s3 = np.array([
    [
    [0], [1], [2], [3], [4]
    ],
    [
    [5], [6], [7], [8], [9]
    ]
    ])

    >>> s3.shape
    (2, 5, 1)

    瞧!我们做到了!这是输入数组;现在检查目标数组,它必须有两个长度为 5 的目标样本,每个样本具有一个特征,即形状为 (2, 5, 1) :
    >>> np.array([[ 5,  6,  7,  8,  9],
    [10, 11, 12, 13, 14]]).shape
    (2,5)

    几乎!最后一个维度(即 1 )丢失( 注意: 取决于您的模型架构,您可能需要也可能不需要最后一个轴)。所以我们可以使用上面的一步一步的方法来找到我们的错误,或者我们可以聪明一点,在最后添加一个轴:
    >>> t = np.array([[ 5,  6,  7,  8,  9],
    [10, 11, 12, 13, 14]])
    >>> t = np.expand_dims(t, axis=-1)
    >>> t.shape
    (2, 5, 1)

    对不起,我无法解释得比这更好!但无论如何,当您看到某些内容(即输入/目标数组的形状)在我的评论和答案中一遍又一遍地重复时,请假设它一定是重要的东西,应该进行检查。

    关于python - Keras fit_generator() - 时间序列的批处理如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56229630/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com