csv 如何解决模型的形状不相容问题?

7gcisfzg  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(143)

这是我的训练和测试拆分形状:

print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
----------------------
(120000, 72)
(12000, 72)
(120000, 6)
(12000, 6)

我为CNN重塑了数据:

X_train = X_train.reshape(len(X_train), X_train.shape[1], 1)
X_test = X_test.reshape(len(X_test), X_test.shape[1], 1)
X_train.shape, X_test.shape
-------------------------------------------------------------------
((120000, 72, 1), (12000, 72, 1))

实现深度学习功能:

def model():
    model = Sequential()
    model.add(Conv1D(filters=64, kernel_size=6, activation='relu', 
                    padding='same', input_shape=(72, 1)))
    model.add(BatchNormalization())
    
    # adding a pooling layer
    model.add(MaxPooling1D(pool_size=(3), strides=2, padding='same'))
    
    model.add(Conv1D(filters=64, kernel_size=6, activation='relu', 
                    padding='same', input_shape=(72, 1)))
    model.add(BatchNormalization())
    model.add(MaxPooling1D(pool_size=(3), strides=2, padding='same'))
    
    model.add(Conv1D(filters=64, kernel_size=6, activation='relu', 
                    padding='same', input_shape=(72, 1)))
    model.add(BatchNormalization())
    model.add(MaxPooling1D(pool_size=(3), strides=2, padding='same'))
    
    model.add(Flatten())
    model.add(Dense(64, activation='relu'))
    model.add(Dense(64, activation='relu'))
    model.add(Dense(3, activation='softmax'))
    
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

当i拟合时,显示错误:形状(32,6)和(32,3)不兼容

model = model()
model.summary()
logger = CSVLogger('logs.csv', append=True)
his = model.fit(X_train, y_train, epochs=30, batch_size=32, 
          validation_data=(X_test, y_test), callbacks=[logger])

---------------------------------------------------------------
 ValueError: Shapes (32, 6) and (32, 3) are incompatible

什么问题,我该如何解决?

w80xi6nr

w80xi6nr1#

您的目标是6维的,但您的预测是3维的,因此不匹配。您应该将密集(6)作为模型的最后一层,而不是密集(3)

相关问题