0

I'm having trouble setting up the inputs for stateful=True LSTM layer in Keras. This is what I have so far:

    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)

This seems to run one training set, then fails when doing the test set; I'm assuming because I don't have the samples/batch numbers set up correctly. I'm just using random numbers to try to get a stateful LSTM layer to work, then I'll replace it with my actual data, which consists of two audio samples loaded from .wav files. The size of the audio data is approximately (8000000, 1), which I will want to set up in smaller batches to feed to the stateful LSTM. What should my "batch_input_shape" and input data tensors look like?

Thanks!

***Update: The error I was seeing was caused by another section of the code, the above code does work.

Follow up question, if I have two audio signals, for example shape (8000000,1), how would I set up batches of data into the stateful LSTM? I'm trying to train a model that fits the first audio signal to the second audio signal (basically like an audio filter).


***2nd Update: I seem to be getting training to work with my audio data, but after the first epoch I get this error:

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

And here is my code:

    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)  
Keith
  • 47
  • 4

1 Answers1

0

I got it working, I had to make sure the total number of samples is divisible by the batch size. I thought Keras handled that automatically, but I guess not in this case.

Keith
  • 47
  • 4