How exactly does Keras accept dimension arguments for LSTM / time series tasks?

I can not find a specific answer to the question of how to submit data to Keras. Most examples seem to work with images / text data and have clearly defined data points.

I am trying to stream music to the LSTM neural network. I want the network to take ~ 3 seconds of music and assign the next 2 seconds. I have music prepared in .wav files and divided into 5 second intervals, which I laid out on my X (first 3 seconds) and Y (last two seconds). I tried my music at 44,100 Hz, so my X is 132,300 observations "long" and my Y is "88,200" observations.

But I can't figure out exactly how to reduce Keras to my data structure. I use the Tensorflow backing.

In the interest of summarizing the problem and the answer, I will use A, B, C to refer to measurements. The only difference between this example data and my real data is that these are random values ​​distributed from 0 to 1, and my data is an array of integers.

import numpy as np
#using variables to make it easy to generalize the answer

#a = the number of observations I have
a       = 411

#b = the duration of the sample, 44.1k observations per second of music
b_train = 132300
b_test  = 88200

#c = the number of channels in the music, this is 2 channel stereo
c       = 2

#now create sample data with the dimensionality given above:
X = np.random.rand(a,b_train,c)
y = np.random.rand(a,b_test ,c)

#split the data
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.20, random_state=42)    

However, I don’t know how to adjust the model to understand that the “first” (A) dimension contains observations and that I want to more or less break the music (B) into channel (C).

I know that it would be easier to convert this to mono (and the 2d problem), but I am very curious to find out if this has a “simple” solution - it mainly depends on what I have below or if I should think about the model differently.

The main question is: how would I build a model that would allow me to convert my X data to my Y data?

, , .

import keras
import math, time
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.recurrent import LSTM
from keras.models import load_model

def build_model(layers):
    d = 0.3
    model = Sequential()

    model.add(LSTM(256, input_shape=(layers), return_sequences=True))
    model.add(Dropout(d))

    model.add(LSTM(256, input_shape=(layers), return_sequences=False))
    model.add(Dropout(d))

    model.add(Dense(32,kernel_initializer="uniform",activation='relu'))        
    model.add(Dense(1,kernel_initializer="uniform",activation='linear'))


    start = time.time()
    model.compile(loss='mse',optimizer='adam', metrics=['accuracy'])
    print("Compilation Time : ", time.time() - start)
    return model


#build model... 
model = build_model([328,132300,2])

model.fit(X_train,y_train,batch_size=512,epochs=30,validation_split=0.1,verbose=1)

( model =...):

 ValueError: Input 0 is incompatible with layer lstm_2: expected ndim=3, found ndim=4

, Keras ndim = 4 . , , , , "" A, B- C-.

- , . 17 , , /.

!

+6
1

Keras , input_shape. guide:

input_shape . ( None, None , ). input_shape .

model = build_model([132300,2]) .

+1

All Articles