0

我具有以下形狀的列車數據集:(300, 5, 720)卷積神經網絡input_shape尺寸誤差(KERAS,PYTHON)

[[[ 6. 11. 389. ..., 0. 0. 0.] 
    [ 2. 0. 0. ..., 62. 0. 0.] 
    [ 0. 0. 18. ..., 0. 0. 0.] 
    [ 38. 201. 47. ..., 0. 108. 0.] 
    [ 0. 0. 1. ..., 0. 0. 0.]] 

    [[ 136. 95. 0. ..., 0. 0. 0.] 
    [ 85. 88. 85. ..., 0. 31. 0.] 
    [ 0. 0. 0. ..., 0. 0. 0.] 
    [ 0. 0. 0. ..., 0. 0. 0.] 
    [ 13. 19. 0. ..., 0. 0. 0.]]] 

我試圖通過每個樣品輸入到CNN模型中,每個輸入是大小(5,720) ,我使用在keras以下模型:

cnn = Sequential() 

    cnn.add(Conv2D(64, (5, 50), 
    padding="same", 
    activation="relu",data_format="channels_last", 
    input_shape=in_shape)) 

    cnn.add(MaxPooling2D(pool_size=(2,2),data_format="channels_last")) 

    cnn.add(Flatten()) 
    cnn.add(Dropout(0.5)) 

    cnn.add(Dense(number_of_classes, activation="softmax")) 

    cnn.compile(loss="categorical_crossentropy", optimizer="adam", metrics= 
    ['accuracy']) 

    cnn.fit(x_train, y_train, 
    batch_size=batch_size, 
    epochs=epochs, 
    validation_data=(x_test, y_test), 
    shuffle=True) 

我使用輸入的形狀爲:

rows,cols=x_train.shape[1:] 
    in_shape=(rows,cols,1) 

,但我收到以下錯誤:

ValueError: Error when checking model input: expected conv2d_1_input to have 4 dimensions, but got array with shape (300, 5, 720)

如何解決這個問題?

回答

1

這是凱拉斯卷積中最經典的錯誤之一。它的起源在於當使用channels_last輸入尺寸時,即使在只有一個通道的情況下,您也需要輸入尺寸爲(height, width, channels)。所以基本上重塑:

x_train = x_train.reshape((x_train.shape[0], 5, 720, 1) 

應該解決您的問題。