Keras CNN错误:预期序列具有3维,但数组的形状为(500,400) [英] Keras CNN Error: expected Sequence to have 3 dimensions, but got array with shape (500, 400)
问题描述
我收到此错误.我是ML的新手.
I'm getting this error. I'm quite new to ML.
ValueError:检查输入时出错:预期序列具有3个维度,但数组的形状为(500,400)
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)
====================================
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 insight please ? Thanks in advance.
推荐答案
两件事-
Conv1D层期望输入的形状为(batch_size, x, filters)
,在您的情况下为(500,400,1)
.
您需要重塑输入层的形状,添加另一个尺寸为1的轴.(这不会更改数据中的任何内容).
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).
您正尝试使用多个输入,顺序API并非最佳选择.我建议使用功能性API
You are trying to use multiple inputs, Sequential API is not the best choice for that. I would recommend using the Functional API
修改: 关于您的评论,不确定您做错了什么,但这是代码的工作版本(带有伪数据),并经过了调整:
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)
有输出:
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
这篇关于Keras CNN错误:预期序列具有3维,但数组的形状为(500,400)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!