首页 > 解决方案 > 与图层 CNN 多类分类算法不兼容?

问题描述

我想用这个数据包brain 4 data chanel(train data(X_train): 10 * 4 - in here 1 parameter is a T1 canal data, 2 - T2, 3- T1C, 4 - Flair), X_test 是ground truth . 但我不知道如何正确设置模型。

训练数据:

X_train([[0.487, 0.453, 0.375, 0.484],
       [0.423, 0.57 , 0.289, 0.829],
       [0.359, 0.921, 0.247, 1.   ],
       [0.497, 0.509, 0.472, 0.545],
       [0.49 , 0.438, 0.435, 0.419],
       [0.304, 0.713, 0.225, 1.   ],
       [0.441, 0.566, 0.41 , 0.655],
       [0.522, 0.445, 0.435, 0.5  ],
       [0.604, 0.506, 0.55 , 0.581],
       [0.251, 0.46 , 0.461, 0.31 ]])
X_test([0., 2., 2., 0., 0., 2., 0., 0., 0., 0.])

测试数据:

y_train([[0.455, 0.377, 0.371, 0.597],
       [0.492, 0.6  , 0.487, 0.594],
       [0.407, 0.596, 0.711, 0.681],
       [0.664, 0.37 , 0.733, 0.342],
       [0.418, 0.63 , 0.504, 0.606],
       [0.366, 0.947, 0.272, 1.   ],
       [0.559, 0.442, 0.539, 0.584],
       [0.655, 0.362, 0.645, 0.594],
       [0.6  , 0.506, 0.447, 0.548],
       [0.715, 0.43 , 0.661, 0.432]])
y_test([0., 0., 2., 0., 0., 2., 0., 0., 0., 0.])

这是我的卷积神经网络:

def baseline_model():
    # Create the model
    model = Sequential()
    model.add(Conv1D(32, 10 ,input_shape=(10,4), padding='same', activation='relu'))
    model.add(Dropout(0.2))
    model.add(Conv1D(32, 10, padding='same', activation='relu'))
    model.add(MaxPooling1D(pool_size=(2, 2)))
    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4, activation='softmax'))
    
    # Compile model
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    print(model.summary())
    
    return model
#Fitt the model
h = model.fit(X_train, X_test, epochs=10, batch_size=4)

我收到以下错误

ValueError: 层序贯_74 的输入 0 与层不兼容::预期 min_ndim=3,发现 ndim=2。收到的完整形状:(无,4)

怎么了?请帮忙。

标签: pythonkerasconv-neural-networkmulticlass-classification

解决方案


推荐阅读