Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

python - Keras CNN Error: expected Sequence to have 3 dimensions, but got array with shape (500, 400)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...