1
$\begingroup$

I want to input a numpy 2d array into MLP but I have an array of 50395 rows that contains many 2d array of shape (x, 129). x because some matrices have different row numbers. Here is an example :

train['spec'].shape
>>(50395,)
train['spec'][0].shape
>>(41, 129)
train['spec'][5].shape
>>(71, 129)

Here an snippet of my code :

X_train = train['spec'].values;     X_valid = valid['spec'].values
y_train = train['label'].values;    y_valid = valid['label'].values
model.add(Dense(12, input_shape=(50395, ), activation='relu'));
model.fit(X_train, y_train, validation_data=(X_valid, y_valid), epochs=500, batch_size=1);

I get this error on last line (model.fit) : ValueError: Error when checking input: expected dense_54_input to have shape (50395,) but got array with shape (1,)

How to fix this problem so that the network can take as input all 50395 matrices of shape (x, 129)?

$\endgroup$
2
  • $\begingroup$ reshape all your data in vector format, each stack row after row $\endgroup$
    – Nikos M.
    Jan 24 at 17:08
  • $\begingroup$ It will don't work! $\endgroup$ Jan 25 at 17:59
0
$\begingroup$

First, I made the matrices (x, 129) to have the same shape by padding in with the rows filled with zeros.

Second, I transformed my large matrices into dimension 3 by the following process:

train = list(train_df['spec'])
np.array(train).shape
>>> (50395, 71, 129)


X_train = list(train['spec']);     X_valid = list(valid['spec'])
y_train = train['label'].values;    y_valid = valid['label'].values
model.add(Dense(12, input_shape=(50395, 71, 129), activation='relu'));
model.fit(np.array(X_train), y_train, validation_data=(np.array(X_valid), y_valid), epochs=500, batch_size=1);
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.