3

I'm getting this error:

ValueError: Error when checking input: expected Sequence to have 3 dimensions, but got array with shape (500, 400)

These are the below codes that I'm using.

print(X1_Train.shape)
print(X2_Train.shape)
print(y_train.shape)

Output (here I've 500 rows in each):

(500, 400)
(500, 1500)
(500,)

400 => timesteps (below)
1500 => n (below)

Code:

timesteps = 50 * 8
n = 50 * 30

def createClassifier():
    sequence = Input(shape=(timesteps, 1), name='Sequence')
    features = Input(shape=(n,), name='Features')

    conv = Sequential()
    conv.add(Conv1D(10, 5, activation='relu', input_shape=(timesteps, 1)))
    conv.add(Conv1D(10, 5, activation='relu'))
    conv.add(MaxPool1D(2))
    conv.add(Dropout(0.5))

    conv.add(Conv1D(5, 6, activation='relu'))
    conv.add(Conv1D(5, 6, activation='relu'))
    conv.add(MaxPool1D(2))
    conv.add(Dropout(0.5))
    conv.add(Flatten())
    part1 = conv(sequence)

    merged = concatenate([part1, features])

    final = Dense(512, activation='relu')(merged)
    final = Dropout(0.5)(final)
    final = Dense(num_class, activation='softmax')(final)

    model = Model(inputs=[sequence, features], outputs=[final])
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

model = createClassifier()
# print(model.summary())
history = model.fit([X1_Train, X2_Train], y_train, epochs =5)

Any insights?

1 Answer 1

4

Two things -

Conv1D layer expects input to be in the shape (batch_size, x, filters), in your case (500,400,1).
You need to reshape your input layer, add another axis, of size 1. (this does not change anything in your data).

You are trying to use multiple inputs, Sequential API is not the best choice for that. I would recommend using the Functional API

Edit: Regarding your comment, not sure what you did wrong, but this is a working version of your code (with fake data), with a reshape:

import keras

import numpy as np



X1_Train = np.ones((500,400))
X2_Train = np.ones((500,1500))
y_train = np.ones((500))
print(X1_Train.shape)
print(X2_Train.shape)
print(y_train.shape)

num_class = 1


timesteps = 50 * 8
n = 50 * 30

def createClassifier():
    sequence = keras.layers.Input(shape=(timesteps, 1), name='Sequence')
    features = keras.layers.Input(shape=(n,), name='Features')

    conv = keras.Sequential()
    conv.add(keras.layers.Conv1D(10, 5, activation='relu', input_shape=(timesteps, 1)))
    conv.add(keras.layers.Conv1D(10, 5, activation='relu'))
    conv.add(keras.layers.MaxPool1D(2))
    conv.add(keras.layers.Dropout(0.5))

    conv.add(keras.layers.Conv1D(5, 6, activation='relu'))
    conv.add(keras.layers.Conv1D(5, 6, activation='relu'))
    conv.add(keras.layers.MaxPool1D(2))
    conv.add(keras.layers.Dropout(0.5))
    conv.add(keras.layers.Flatten())
    part1 = conv(sequence)

    merged = keras.layers.concatenate([part1, features])

    final = keras.layers.Dense(512, activation='relu')(merged)
    final = keras.layers.Dropout(0.5)(final)
    final = keras.layers.Dense(num_class, activation='softmax')(final)

    model = keras.Model(inputs=[sequence, features], outputs=[final])
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

model = createClassifier()
# print(model.summary())
X1_Train = X1_Train.reshape((500,400,1))
history = model.fit([X1_Train, X2_Train], y_train, epochs =5)

With output:

Using TensorFlow backend.
(500, 400)
(500, 1500)
(500,)
Epoch 1/5
500/500 [==============================] - 1s 3ms/step - loss: 1.1921e-07 - acc: 1.0000
Epoch 2/5
500/500 [==============================] - 0s 160us/step - loss: 1.1921e-07 - acc: 1.0000
Epoch 3/5
500/500 [==============================] - 0s 166us/step - loss: 1.1921e-07 - acc: 1.0000
Epoch 4/5
500/500 [==============================] - 0s 154us/step - loss: 1.1921e-07 - acc: 1.0000
Epoch 5/5
500/500 [==============================] - 0s 157us/step - loss: 1.1921e-07 - acc: 1.0000
Sign up to request clarification or add additional context in comments.

2 Comments

changed accordingly to (500, 400, 1). But it didn't work. I'm still getting an error. I changed the CNN layer as per functional API. But, I'm getting the below error. tensorflow.python.framework.errors_impl.InvalidArgumentError: indices[22,330] = -1 is not in [0, 400)
Not sure what you did wrong with the reshape, edited and added a working version of your code, with fake data, note that I only added the reshape line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.