[英]ValueError: Input 0 of layer sequential_32 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 256]
I am building a Conv1D model with train data (28659, 257) and test data (5053, 257), but I am facing a value error that says: expected min_ndim=3, found ndim=2.我正在构建一个带有训练数据(28659、257)和测试数据(5053、257)的 Conv1D model,但我遇到了一个值错误,上面写着:预期 min_ndim=3,发现 ndim=2。 Full shape received: [None, 256]收到的完整形状:[无,256]
print(train_data.shape)
print(test_data.shape)
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=5, activation='relu', input_shape=(256,1)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(8, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])
model.summary()
opt = keras.optimizers.Adam(learning_rate=0.01)
model.compile(loss='CategoricalCrossentropy', optimizer=opt, metrics=['accuracy'])
history = model.fit(train_data.values[:, 0:256], to_categorical(train_data.values[:, 256]), epochs=180, batch_size=500)
y_pred = model.predict(test_data.values[:, 0:256])
y_pred = model.predict(test_data.values[:,0:256])
y_pred = (y_pred > 0.5)
accuracy = metrics.accuracy_score(to_categorical(test_data.values[:,256]),y_pred)
print(f'Testing accuracy of the model is {accuracy*100:.4f}%')
The error is from the fit(), but I cannot figure my mistake with the calculation!错误来自 fit(),但我无法计算出我的计算错误! Any help is appreciated!任何帮助表示赞赏!
try to reshape your train and test data:尝试重塑您的训练和测试数据:
X_train=np.reshape(train_data,(train_data.shape[0], train_data.shape[1],1))
X_test=np.reshape(test_data,(test_data.shape[0], test_data.shape[1],1))
You can't feed values like: [1,2,3,4]
, you have to feed your values like [[1],[2],[3],[4]]
您不能提供诸如[1,2,3,4]
类的值,您必须提供诸如[[1],[2],[3],[4]]
类的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.