首页 > 解决方案 > 如何为 Keras 有状态 LSTM 层设置输入?

问题描述

我在 Keras 中为 stateful=True LSTM 层设置输入时遇到问题。这是我到目前为止所拥有的:

    clear_session()
    model = Sequential()
    model.add(LSTM(hidden_units, batch_input_shape=(1,1,1), return_sequences=False, stateful=True))
    model.add(Dense(1, activation=None))


    X = np.random.rand(100, 1, 1)
    Y = np.random.rand(100, 1, 1)

    model.fit(X,Y, epochs=epochs, batch_size=batch_size, shuffle=False, validation_split=0.2)

这似乎运行一个训练集,然后在做测试集时失败;我假设是因为我没有正确设置样品/批号。我只是使用随机数来尝试让有状态的 LSTM 层工作,然后我将用我的实际数据替换它,其中包含从 .wav 文件加载的两个音频样本。音频数据的大小约为 (8000000, 1),我希望将其设置为较小的批次以提供给有状态的 LSTM。我的“batch_input_shape”和输入数据张量应该是什么样的?

谢谢!

***更新:我看到的错误是由代码的另一部分引起的,上面的代码确实有效。

后续问题,如果我有两个音频信号,例如形状 (8000000,1),我将如何将批量数据设置到有状态 LSTM 中?我正在尝试训练一个适合第一个音频信号到第二个音频信号的模型(基本上就像一个音频滤波器)。


***第二次更新:我似乎正在接受训练以处理我的音频数据,但在第一个时代之后,我收到了这个错误:

Epoch 1/10
3206/3217 [============================>.] - ETA: 0s - loss: 0.1254 - error_to_signal: 0.1254Traceback (most recent call last):
  File "train.py", line 279, in <module>
    main(args)
  File "train.py", line 177, in main
    model.fit(X,Y, epochs=epochs, batch_size=batch_size, validation_split=0.2, shuffle=True)
  File "C:\Users\KBloemer\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1100, in fit
    tmp_logs = self.train_function(iterator)
  File "C:\Users\KBloemer\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\eager\def_function.py", line 828, in __call__
    result = self._call(*args, **kwds)
  File "C:\Users\KBloemer\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\eager\def_function.py", line 855, in _call
    return self._stateless_fn(*args, **kwds)  # pylint: disable=not-callable
  File "C:\Users\KBloemer\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\eager\function.py", line 2943, in __call__
    filtered_flat_args, captured_inputs=graph_function.captured_inputs)  # pylint: disable=protected-access
  File "C:\Users\KBloemer\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\eager\function.py", line 1919, in _call_flat
    ctx, args, cancellation_manager=cancellation_manager))
  File "C:\Users\KBloemer\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\eager\function.py", line 560, in call
    ctx=ctx)
  File "C:\Users\KBloemer\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\eager\execute.py", line 60, in quick_execute
    inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError:    Specified a list with shape [2048,1] from a tensor with shape [1539,1]
         [[{{node TensorArrayUnstack/TensorListFromTensor}}]]
         [[sequential/lstm/PartitionedCall]] [Op:__inference_train_function_2597]

Function call stack:
train_function -> train_function -> train_function

这是我的代码:

    batch_size=2048
    epochs=10
    clear_session()
    model = Sequential()

    model.add(LSTM(hidden_units, batch_input_shape=(batch_size, 1, 1), return_sequences=False, stateful=True))
    model.add(Dense(1, activation=None))
    model.compile(optimizer=Adam(learning_rate=learning_rate), loss=error_to_signal, metrics=[error_to_signal])

 # Then I load the audio data, which is two arrays of shape  (8234884, 1, 1) 
 #   after adding an extra dimension to (8234884, 1).

model.fit(X,Y, epochs=epochs, batch_size=batch_size, validation_split=0.2, shuffle=True)  

标签: tensorflowkeraslstmlstm-stateful

解决方案


我让它工作了,我必须确保样本总数可以被批量大小整除。我认为 Keras 会自动处理这个问题,但我想在这种情况下不会。


推荐阅读