【问题标题】:ValueError: Input 0 of layer sequential_32 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 256]ValueError:layersequential_32 的输入 0 与 layer 不兼容::预期 min_ndim=3,发现 ndim=2。收到的完整形状:[无,256]
【发布时间】:2021-03-16 18:33:05
【问题描述】:

我正在使用训练数据 (28659, 257) 和测试数据 (5053, 257) 构建 Conv1D 模型,但我遇到了一个值错误,提示:预期 min_ndim=3,发现 ndim=2。收到的完整形状:[无,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}%')

错误来自 fit(),但我无法计算出我的错误!任何帮助表示赞赏!

【问题讨论】:

    标签: python tensorflow keras deep-learning conv-neural-network


    【解决方案1】:

    尝试重塑您的训练和测试数据:

    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))
    

    您不能输入像 [1,2,3,4] 这样的值,您必须输入像 [[1],[2],[3],[4]] 这样的值

    【讨论】:

    • 当我这样做时,我得到了这个错误 ValueError: Must pass 2-d input。形状=(28659, 257, 1)
    • 你将 257 输入到 256 输入(第一个卷积层)
    • 我不明白,但我仍然尝试这样做,面临同样的错误。
    猜你喜欢
    • 2021-03-10
    • 2021-01-16
    • 2021-11-13
    • 2021-08-12
    • 2021-06-17
    • 2022-12-10
    • 2021-09-23
    • 2021-08-10
    • 2020-11-15
    相关资源
    最近更新 更多