Search code examples
pythontensorflowtime-seriesconv-neural-networklstm

ValueError: cannot reshape array of size 78543360 into shape (51135,4,32,32)


Following this tutorial from https://machinelearningmastery.com/how-to-develop-rnn-models-for-human-activity-recognition-time-series-classification/

I’m trying to implement CNN-LSTM Network Model on my time series data.

verbose, epochs, batch_size = 0, 25, 64
n_timesteps, n_features, n_outputs = 
X_train.shape[1], X_train.shape[2], y_train.shape[1]
# reshape data into time steps of sub-sequences
n_steps, n_length = 4, 32
X_train = X_train.reshape((X_train.shape[0], n_steps, 
n_length, n_features))
X_test = X_test.reshape((X_test.shape[0], n_steps, 
n_length, n_features))
# define model
model = Sequential()
model.add(TimeDistributed(Conv1D(filters=64, 
kernel_size=3, activation='relu'), input_shape= 
(None,n_length,n_features)))
model.add(TimeDistributed(Conv1D(filters=64, 
kernel_size=3, activation='relu')))
model.add(TimeDistributed(Dropout(0.5)))
model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(100))
model.add(Dropout(0.5))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='sigmoid'))
model.compile(loss='binary_crossentropy', 
optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, epochs=epochs, 
batch_size=batch_size, verbose=verbose)

Here are Shapes before using reshape:

print(X_train.shape, y_train.shape, X_test.shape, 
y_test.shape)
(51135, 128, 12) (51135, 1) (21915, 128, 12) (21915, 1)

Here is the section of the code I am having an error with: ValueError: cannot reshape array of size 78543360 into shape (51135, 4,32,32).

How do I fix this?


Solution

  • I did not have access to your data so I created dummy np.zero arrays for the data with the dimensions you specified. Ran it to see what was happening to the dimensions. Did not encounter the error you describe. Code I used is below. Runs and trains so the dimensions appear to be correct.

    X_train=np.zeros((51135, 128, 12))
    y_train=np.zeros((51135, 1))
    X_test=np.zeros((21915, 128, 12))
    y_test=np.zeros((21915, 1))
    print(X_train.shape, y_train.shape, X_test.shape,y_test.shape)
    verbose, epochs, batch_size = 0, 25, 64
    n_timesteps, n_features, n_outputs = X_train.shape[1], X_train.shape[2], y_train.shape[1]
    print(n_timesteps, n_features, n_outputs)
    # reshape data into time steps of sub-sequences
    n_steps, n_length = 4, 32
    X_train = X_train.reshape((X_train.shape[0], n_steps, n_length, n_features))
    X_test = X_test.reshape((X_test.shape[0], n_steps,n_length, n_features))
    print (X_train.shape, X_test.shape)
    # define model
    model = Sequential()
    model.add(TimeDistributed(Conv1D(filters=64, 
    kernel_size=3, activation='relu'), input_shape= 
    (None,n_length,n_features)))
    model.add(TimeDistributed(Conv1D(filters=64, 
    kernel_size=3, activation='relu')))
    model.add(TimeDistributed(Dropout(0.5)))
    model.add(TimeDistributed(MaxPooling1D(pool_size=2)))
    model.add(TimeDistributed(Flatten()))
    model.add(LSTM(100))
    model.add(Dropout(0.5))
    model.add(Dense(100, activation='relu'))
    model.add(Dense(n_outputs, activation='sigmoid'))
    model.compile(loss='binary_crossentropy',optimizer='adam', metrics=['accuracy'])
    model.summary()
    model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=1)
    

    Here is the data that gets printed out. Of course with all zeros training data is meaningless but the dimensions are right for model.fit to execute

    (51135, 128, 12) (51135, 1) (21915, 128, 12) (21915, 1)
    128 12 1
    (51135, 4, 32, 12) (21915, 4, 32, 12)
    Epoch 1/25
    799/799 [==============================] - 9s 11ms/step - loss: 0.6051 - accuracy: 1.0000
    Epoch 2/25
    799/799 [==============================] - 5s 6ms/step - loss: 0.3479 - accuracy: 1.0000